一:ExpandableListView 雙重列表,就像QQ好友同樣,點擊好友,就顯示好友的列表android
1:主佈局文件:ide
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ExpandableListView
android :id="@+id/activity_main_expandablelistview"
android :layout_width="match_parent"
android :layout_height="wrap_content" />
佈局
</RelativeLayout >this
2:父列表佈局文件spa
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android :id="@+id/activity_listview_showtext"
android :layout_width="wrap_content"
android :layout_height="wrap_content"
android :layout_marginLeft="30dp"
android :textColor="#FF0000"
android :textSize="30sp"></ TextView>xml
</LinearLayout >繼承
3:子列表佈局文件utf-8
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android :id="@+id/activity_item_childtext"
android :layout_width="wrap_content"
android :layout_height="wrap_content"
android :textColor="#ff2bfe"
android :textSize="20sp"></ TextView>
get
</LinearLayout >it
4:Activity
public class MainActivity extends AppCompatActivity {
public ExpandableListView mainlistview = null;
public List<String> parent=null ;
public Map<String, List<String>> map = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainlistview = (ExpandableListView) findViewById(R.id.activity_main_expandablelistview );
initData();
MyAdapter adapter=new MyAdapter(parent,map,MainActivity. this);
mainlistview .setAdapter(adapter);
}
public void initData() {
parent = new ArrayList<String>();
parent .add("parent1");
parent .add("parent2");
parent .add("parent3");
map = new HashMap<String, List<String>>();
List<String> list1 = new ArrayList<String>();
list1.add("child1-1");
list1.add("child1-2");
list1.add("child1-3");
map .put("parent1", list1);
List<String> list2 = new ArrayList<String>();
list2.add("child2-1");
list2.add("child2-2");
list2.add("child2-3");
map .put("parent2", list2);
List<String> list3 = new ArrayList<String>();
list3.add("child3-1");
list3.add("child3-2");
list3.add("child3-3");
map .put("parent3", list3);
}
}
5:適配器(雙重列表是繼承於BaseExpandableListAdapter )
public class MyAdapter extends BaseExpandableListAdapter {
public List<String> first=new ArrayList<>();
public Map<String, List<String>> map =new HashMap<String, List<String>>();
private Context context;
public MyAdapter(List<String> first, Map<String, List<String>> map, Context context) {
this .first =first;
this .map = map;
this .context = context;
}
//獲得子item須要關聯的數據
@Override
public Object getChild(int groupPosition, int childPosition) {
String key = first.get(groupPosition);
return (map .get(key).get(childPosition));
}
//獲得子item的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//設置子item的組件
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String key =first.get(groupPosition);
String info = map.get(key).get(childPosition);
if (convertView == null) {
convertView=LayoutInflater.from(context).inflate(R.layout. item_childlistview,parent, false);
}
TextView tv = (TextView) convertView
.findViewById(R.id.activity_item_childtext);
tv.setText(info);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "點擊了子View", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
//獲取當前父item下的子item的個數
@Override
public int getChildrenCount(int groupPosition) {
String key = first.get(groupPosition);
int size=map.get(key).size();
return size;
}
//獲取當前父item的數據
@Override
public Object getGroup(int groupPosition) {
return first.get(groupPosition);
}
@Override
public int getGroupCount() {
return first.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//設置父item組件
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView=LayoutInflater.from(context).inflate(R.layout. item_listview,parent, false);
}
TextView tv = (TextView) convertView
.findViewById(R.id.activity_listview_showtext);
tv.setText(first.get(groupPosition));
return convertView;
}
@Override
public boolean hasStableIds() {
return true ;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true ;
}
}