仿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
- 仿QQ分组折叠菜单ExpandableListView
- 仿QQ折叠菜单代码
- Android仿QQ通讯录分组展示ExpandableListView
- 仿QQ好友列表分组折叠效果
- Android 仿QQ好友分组列表、ExpandableListView的使用详解
- android RecyclerView一步步打造分组效果、类似QQ分组、折叠菜单、分组效果(一)
- android RecyclerView一步步打造分组效果、类似QQ分组、折叠菜单、分组效果(二)
- 详细讲解ExpandableListView显示和查询仿QQ分组列表用户信息
- Android ExpandableListView简单使用(一)之仿QQ添加分组功能
- ExpandableListView仿QQ好友列表
- ExpandableListView仿QQ好友列表
- 仿QQ好友列表 ExpandableListView
- android 仿qq分组
- 折叠菜单效果,ExpandableListView用法(精)
- 代码经过精简后的仿QQ折叠菜单-滑动门选项卡
- 仿QQ导航菜单
- 仿QQ导航菜单
- 仿QQ导航菜单
- 基于opencv的数学形态学的操作,涉及数学形态学细化、击中击不中变换
- 用js内置对象XMLHttpRequest 来用ajax
- Spring Framework灰度发布
- GDB、DDD-linux下程序调试
- SDNU:1040 导弹拦截 第二问关于“最大上升序列”做法的思考
- 仿QQ分组折叠菜单ExpandableListView
- 机器学习基石-10-2-Logistic Regression Error
- Node+vue实现视频中的人脸识别
- struts 跨站点脚本漏洞
- dlib实现人脸识别-python
- 35 个 Java 代码性能优化总结
- 如何在Ubuntu下为SI安装Fixedsys字体
- grok在python 中的使用
- Altium Designer 9 / 10 / 15 系列 不能正常启动的解决办法。