仿QQ分组折叠菜单ExpandableListView

来源:互联网 发布:钉钉阿里云code机器人 编辑:程序博客网 时间:2024/05/16 17:27

public class MainActivity extends AppCompatActivity { private ExpandableListView el_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final String items[] = new String[]{"one", "two", "three","four","five"}; final Map<String, List<String>> map = new HashMap<String, List<String>>(); List<String> list1 = new ArrayList<>(); List<String> list2 = new ArrayList<>(); List<String> list3 = new ArrayList<>(); List<String> list4 = new ArrayList<>(); List<String> list5 = new ArrayList<>(); list1.add("一"); list1.add("二"); list1.add("三"); list2.add("四"); list2.add("五"); list2.add("六"); list3.add("七"); list3.add("八"); list3.add("九"); list4.add("十"); list4.add("十一"); list4.add("十二"); list5.add("十三"); list5.add("十四"); list5.add("十五"); map.put(items[0], list1); map.put(items[1], list2); map.put(items[2], list3); map.put(items[3], list4); map.put(items[4], list5); el_list = findViewById(R.id.el_list); el_list.setAdapter(new MyAdapter(this,map,items)); el_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) { Toast.makeText(MainActivity.this,map.get(items[i]).get(i1),Toast.LENGTH_LONG); return true; } }); } public class MyAdapter extends BaseExpandableListAdapter { private final Map<String, List<String>> mMap; private final String[] mIems; public MyAdapter(Context context, Map<String, List<String>> map, String[] items) { mMap = map; mIems = items; } // 获得父项的数量 @Override public int getGroupCount() { return mMap.size(); } // 获得某个父项的子项数目 @Override public int getChildrenCount(int i) { return mMap.get(mIems[i]).size(); } // 获得某个父项 @Override public Object getGroup(int i) { return mMap.get(mIems[i]); } // 获得某个父项的某个子项 @Override public Object getChild(int i, int i1) { return mMap.get(mIems[i]).get(i1); } // 获得某个父项的id @Override public long getGroupId(int i) { return 0; } // 获得某个父项的某个子项的id @Override public long getChildId(int i, int i1) { return 0; } // 按函数的名字来理解应该是是否具有稳定的id,这个方法目前一直都是返回false,没有去改动过 @Override public boolean hasStableIds() { return false; } // 获得父项显示的view @Override public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_parent, null); TextView tv_1 = view.findViewById(R.id.tv_parent); tv_1.setText(mIems[i]); return view; } // 获得子项显示的view @Override public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) { view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_child, null); TextView tv_child = view.findViewById(R.id.tv_child); tv_child.setText(mMap.get(mIems[i]).get(i1)); tv_child.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"点击子条目",Toast.LENGTH_LONG).show(); } }); return view; } // 子项是否可选中,如果需要设置子项的点击事件,需要返回true @Override public boolean isChildSelectable(int i, int i1) { return true; } }

}

主界面布局

<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"    tools:context="cn.qun.expandablelistview.MainActivity">    <ExpandableListView        android:id="@+id/el_list"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/tv_parent"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:layout_marginBottom="5dp"        android:layout_marginTop="5dp"        android:textColor="#f00"        android:textSize="20sp" /></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/tv_child"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:textColor="#000"        android:textSize="20sp"/></LinearLayout>



阅读全文
0 0
原创粉丝点击