ExpandableListView的简单使用
来源:互联网 发布:mac 网络测速 编辑:程序博客网 时间:2024/06/08 09:52
1,xml中引入布局ExpandableListView
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"> <ExpandableListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/elv_main"></ExpandableListView></LinearLayout>
2,Adapter的编写,继承BaseExpandableListAdapter并实现抽象方法,并对应各个方法填充对应参数。注意一般前面一个id为parentId,后面一个为childId
public class MyExpandableListViewAdapter extends BaseExpandableListAdapter{ private Map<String,List<String>>data=new HashMap<>(); private String[] parentList=new String[]{}; private Context context; public MyExpandableListViewAdapter(Context context, Map<String, List<String>> data, String[] parentList) { this.context=context; this.data=data; this.parentList=parentList; } // 获得父项的数量 @Override public int getGroupCount() { return data.size(); } // 获得某个父项的子项数目 @Override public int getChildrenCount(int parentPos) { return data.get(parentList[parentPos]).size(); } // 获得某个父项 @Override public Object getGroup(int parentPos) { return data.get(parentList[parentPos]); } // 获得某个父项的某个子项 @Override public Object getChild(int parentPos, int childPos) { return data.get(parentList[parentPos]).get(childPos); } // 获得某个父项的id @Override public long getGroupId(int parentPos) { return parentPos; } // 获得某个父项的某个子项的id @Override public long getChildId(int parentPos, int childPos) { return childPos; } // 按函数的名字来理解应该是是否具有稳定的id,这个函数目前一直都是返回false,没有去改动过 @Override public boolean hasStableIds() { return false; } // 获得父项显示的view @Override public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) { if (view==null){ LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view=inflater.inflate(R.layout.parent_item,null); } view.setTag(R.layout.parent_item,parentPos); view.setTag(R.layout.child_item,-1); TextView text = (TextView) view.findViewById(R.id.parent_title); text.setText(parentList[parentPos]); return view; } @Override public View getChildView(int parentPos, int childPos, boolean b, View view, ViewGroup viewGroup) { if (view == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.child_item, null); } view.setTag(R.layout.parent_item, parentPos); view.setTag(R.layout.child_item, childPos); TextView text = (TextView) view.findViewById(R.id.child_title); text.setText(data.get(parentList[parentPos]).get(childPos)); text.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context, "点到了内置的textview", Toast.LENGTH_SHORT).show(); } }); return view; } // 子项是否可选中,如果需要设置子项的点击事件,需要返回true @Override public boolean isChildSelectable(int i, int i1) { return true; }}3,Activity中引用
public class MainActivity extends AppCompatActivity { private ExpandableListView elv_main; private MyExpandableListViewAdapter adapter; private Map<String,List<String>> data=new HashMap<>(); private String[] parentList=new String[]{"first","second","third"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); elv_main=findViewById(R.id.elv_main); initData(); adapter=new MyExpandableListViewAdapter(this,data,parentList); elv_main.setAdapter(adapter); } private void initData() { for (int i=0;i<parentList.length;i++){ List<String> list=new ArrayList<>(); for (int j=0;j<8;j++){ list.add("item"+j); } data.put(parentList[i],list); } }}到这里就没了,很简单
阅读全文