android 三级级联筛选列表

举报
再见孙悟空_ 发表于 2022/01/13 01:08:12 2022/01/13
【摘要】         三级级联关系的列表,上面是我项目做出来的效果图,这个是单选的效果。 实现的思路:左边这个是listView ,右边是两级的expandListView  将这两个view 动态放到viewpager里面 设置好比例,剩下的就是处理点击联动事件了。 大体代码如下...

   

    三级级联关系的列表,上面是我项目做出来的效果图,这个是单选的效果。

实现的思路:左边这个是listView ,右边是两级的expandListView 

将这两个view 动态放到viewpager里面 设置好比例,剩下的就是处理点击联动事件了。

大体代码如下:

activity中 放viewpager的xml


  
  1. <LinearLayout
  2. android:id="@+id/category_ll"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_above="@+id/button_ll"
  6. android:layout_below="@+id/title_ll"
  7. android:orientation="vertical"
  8. android:visibility="gone">
  9. <com.jky.mobile_gczjjc.widget.MyViewPager
  10. android:id="@+id/category_viewpager"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:overScrollMode="never" />
  14. </LinearLayout>

target_first_parger.xml


  

  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/target_divider_color"
  6. android:orientation="vertical">
  7. <ListView
  8. android:id="@+id/target_first_lv"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:background="@color/white"
  12. android:cacheColorHint="@android:color/transparent"
  13. android:divider="@null"
  14. android:scrollbars="none" />

</LinearLayout>

target_second_parger.xml


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/target_divider_color"
  6. android:orientation="vertical">
  7. <ExpandableListView
  8. android:id="@+id/target_second_exl"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:background="@color/white"
  12. android:cacheColorHint="@android:color/transparent"
  13. android:divider="@null"
  14. android:groupIndicator="@null"
  15. android:scrollbars="none" />
  16. </LinearLayout>

activity中 核心代码


  
  1. //一级
  2. mViewPager = findViewById(R.id.category_viewpager);
  3. LayoutInflater inflater = LayoutInflater.from(this);
  4. view1 = inflater.inflate(R.layout.target_first_parger, null);
  5. view2 = inflater.inflate(R.layout.target_second_parger, null);
  6. mFirstListView = view1.findViewById(R.id.target_first_lv);
  7. mSecondListView = view2.findViewById(R.id.target_second_exl);
  8. mFirstAdapter = new TargetCategoryFirstAdapter(this, firstBeans);
  9. mFirstAdapter.setSelectedBackgroundResource(R.drawable.select_white);//选中时
  10. mFirstAdapter.setHasDivider(false);
  11. mFirstAdapter.setNormalBackgroundResource(R.color.target_bg_gray);//未选中
  12. mFirstListView.setAdapter(mFirstAdapter);
  13. views.add(view1);
  14. views.add(view2);//加载了一二级菜单
  15. mViewPager.setAdapter(new MyPagerAdapter(views));

初始化数据及点击事件部分


  
  1. private void initSelectCategory(CategoryBean bean) {
  2. if (bean != null && bean.getData() != null) {
  3. firstBeans = bean.getData();
  4. }
  5. FirstBean fBean = new FirstBean();
  6. fBean.setName("全部");
  7. fBean.setType("0");
  8. fBean.setItems(new ArrayList<SecondBean>());
  9. fBean.setSelect(true);
  10. firstBeans.add(0, fBean);
  11. if (mFirstAdapter == null) {
  12. mFirstAdapter = new TargetCategoryFirstAdapter(TargetFilterActivity.this, firstBeans);
  13. mSecondListView.setAdapter(mSecondAdapter);
  14. } else {
  15. mFirstAdapter.setData(firstBeans);
  16. }
  17. mFirstListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  18. @Override
  19. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  20. FirstBean firstBean = firstBeans.get(position);
  21. for (int i = 0; i < firstBeans.size(); i++) {
  22. if (i == position) {
  23. firstBeans.get(i).setSelect(true);
  24. } else {
  25. firstBeans.get(i).setSelect(false);
  26. }
  27. }
  28. if (mFirstAdapter == null) {
  29. mFirstAdapter = new TargetCategoryFirstAdapter(TargetFilterActivity.this, firstBeans);
  30. mSecondListView.setAdapter(mSecondAdapter);
  31. } else {
  32. mFirstAdapter.setData(firstBeans);
  33. }
  34. if (firstBean.getType().equals("0")) {//不限
  35. if (mSecondAdapter != null) {
  36. mSecondAdapter.setData(new ArrayList<SecondBean>());
  37. mSecondAdapter.notifyDataSetChanged();
  38. }
  39. } else {
  40. mSecondList = firstBean.getItems();
  41. for (int i = 0; i < mSecondList.size(); i++) {
  42. mSecondList.get(i).setIsSelect(false);
  43. for (int j = 0; j < mSecondList.get(i).getItems().size(); j++) {
  44. mSecondList.get(i).getItems().get(j).setIsSelect(false);
  45. }
  46. }
  47. if (mSecondAdapter == null) {
  48. mSecondAdapter = new TargetCategorySecondAdapter(TargetFilterActivity.this, mSecondList);
  49. mSecondListView.setAdapter(mSecondAdapter);
  50. } else {
  51. mSecondAdapter.setData(mSecondList);
  52. }
  53. String secondType = firstBean.getType();
  54. String secondName = firstBean.getName();
  55. if (secondType.equals("-1")) {
  56. mProjectType = mFirstProjectType;
  57. mProjectTypeName = mFirstProjectTypeName;
  58. } else {
  59. mProjectType = mFirstProjectType + "/" + secondType;
  60. mProjectTypeName = mFirstProjectTypeName + "/" + secondName;
  61. }
  62. //默认全部展开
  63. for (int i = 0; i < mSecondList.size(); i++) {
  64. if (mSecondListView != null) {
  65. mSecondListView.expandGroup(i);
  66. }
  67. }
  68. }
  69. }
  70. });
  71. //二级
  72. mSecondListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
  73. @Override
  74. public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
  75. for (int i = 0; i < mSecondList.size(); i++) {
  76. if (i == groupPosition) {
  77. mSecondList.get(i).setIsSelect(true);
  78. } else {
  79. mSecondList.get(i).setIsSelect(false);
  80. }
  81. for (int j = 0; j < mSecondList.get(i).getItems().size(); j++) {
  82. mSecondList.get(i).getItems().get(j).setIsSelect(false);
  83. }
  84. }
  85. String thirdType = mSecondList.get(groupPosition).getType();
  86. String thirdName = mSecondList.get(groupPosition).getName();
  87. mProjectType = mFirstProjectType + "/" + thirdType;
  88. mProjectTypeName = mFirstProjectTypeName + "/" + thirdName;
  89. mSecondAdapter.setData(mSecondList);
  90. return true;
  91. }
  92. });
  93. mSecondListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  94. @Override
  95. public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
  96. for (int i = 0; i < mSecondList.size(); i++) {
  97. if (i == groupPosition) {
  98. mSecondList.get(i).setIsSelect(true);
  99. } else {
  100. mSecondList.get(i).setIsSelect(false);
  101. }
  102. List<SecondBean.ThirdBean> thirdList = mSecondList.get(i).getItems();
  103. for (int j = 0; j < thirdList.size(); j++) {
  104. if (i == groupPosition && j == childPosition) {
  105. thirdList.get(j).setIsSelect(true);
  106. } else {
  107. thirdList.get(j).setIsSelect(false);
  108. }
  109. }
  110. }
  111. String thirdType = mSecondList.get(groupPosition).getType();
  112. String thirdName = mSecondList.get(groupPosition).getName();
  113. String fourType = mSecondList.get(groupPosition).getItems().get(childPosition).getType();
  114. String fourName = mSecondList.get(groupPosition).getItems().get(childPosition).getName();
  115. mProjectType = mFirstProjectType + "/" + thirdType + "/" + fourType;//工程类型id
  116. mProjectTypeName = mFirstProjectTypeName + "/" + thirdName + "/" + fourName;//工程名称 前端显示
  117. mSecondAdapter.setData(mSecondList);
  118. return false;
  119. }
  120. });
  121. }

MyViewPager


  
  1. package com.jky.mobile_gczjjc.widget;
  2. import android.content.Context;
  3. import android.support.v4.view.ViewPager;
  4. import android.util.AttributeSet;
  5. /**
  6. * Created by LaiYingtang on 2016/5/22.
  7. * 主页面左右滑动
  8. */
  9. public class MyViewPager extends ViewPager {
  10. public MyViewPager(Context context, AttributeSet attrs) {
  11. super(context, attrs);
  12. }
  13. public MyViewPager(Context context) {
  14. super(context);
  15. }
  16. //判断menu在x,y的位置
  17. public void scrollTo(int x,int y){
  18. if(getAdapter()==null||x>getWidth()*(getAdapter().getCount()-2)){
  19. return;
  20. }
  21. super.scrollTo(x,y);
  22. }
  23. }

TargetCategoryFirstAdapter


  
  1. package com.jky.mobile_gczjjc.adapter;
  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.TextView;
  9. import com.jky.mobile_gczjjc.R;
  10. import com.jky.mobile_gczjjc.bean.CategoryBean.FirstBean;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. /**
  14. * 指标 建筑分类 第一级适配器
  15. * @author zlw
  16. */
  17. public class TargetCategoryFirstAdapter extends BaseAdapter {
  18. private Context mContext;
  19. private List<FirstBean> firstBeans =new ArrayList<>();
  20. private int mSelectedBackgroundResource;//选中item时的背景颜色
  21. private int mNormalBackgroundResource;//为选中的背景颜色
  22. private boolean hasDivider = true;
  23. public void setSelectedBackgroundResource(int mSelectedBackgroundResource) {
  24. this.mSelectedBackgroundResource = mSelectedBackgroundResource;
  25. }
  26. public void setNormalBackgroundResource(int mNormalBackgroundResource) {
  27. this.mNormalBackgroundResource = mNormalBackgroundResource;
  28. }
  29. public void setHasDivider(boolean hasDivider) {
  30. this.hasDivider = hasDivider;
  31. }
  32. public TargetCategoryFirstAdapter(Context mContext, List<FirstBean> firstBeans) {
  33. this.mContext = mContext;
  34. this.firstBeans = firstBeans;
  35. }
  36. public void setData(List<FirstBean> data) {
  37. this.firstBeans = data;
  38. notifyDataSetChanged();
  39. }
  40. @Override
  41. public int getCount() {
  42. if (firstBeans == null) {
  43. return 0;
  44. }
  45. return firstBeans.size();
  46. }
  47. @Override
  48. public Object getItem(int position) {
  49. if (firstBeans == null) {
  50. return null;
  51. }
  52. return firstBeans.get(position);
  53. }
  54. @Override
  55. public long getItemId(int position) {
  56. return position;
  57. }
  58. @Override
  59. public View getView(int position, View convertView, ViewGroup parent) {
  60. ViewHolder vh = null;
  61. if (convertView == null) {
  62. vh = new ViewHolder();
  63. convertView = LayoutInflater.from(mContext).inflate(R.layout.item_target_category_first, null);
  64. vh.itemLayout = convertView.findViewById(R.id.menu_item_ly);
  65. vh.nameText = convertView.findViewById(R.id.name_rb);
  66. vh.dividerTextView = convertView.findViewById(R.id.menu_item_divider);
  67. convertView.setTag(vh);
  68. } else {
  69. vh = (ViewHolder) convertView.getTag();
  70. }
  71. FirstBean firstBean = firstBeans.get(position);
  72. vh.nameText.setText(firstBean.getName());//设置标题
  73. if (firstBean.isSelect){
  74. vh.nameText.setTextColor(mContext.getResources().getColor(R.color.common_blue));
  75. vh.itemLayout.setBackgroundResource(mSelectedBackgroundResource);
  76. }else{
  77. vh.nameText.setTextColor(Color.BLACK);
  78. vh.itemLayout.setBackgroundResource(mNormalBackgroundResource);
  79. }
  80. //隐藏view
  81. vh.dividerTextView.setVisibility(hasDivider ? View.VISIBLE : View.INVISIBLE);
  82. return convertView;
  83. }
  84. private class ViewHolder{
  85. private View itemLayout;
  86. private TextView nameText;
  87. private TextView dividerTextView;
  88. }
  89. }

TargetCateGorySecondAdapter


  
  1. package com.jky.mobile_gczjjc.adapter;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseExpandableListAdapter;
  7. import android.widget.TextView;
  8. import com.jky.mobile_gczjjc.R;
  9. import com.jky.mobile_gczjjc.bean.CategoryBean.FirstBean.SecondBean;
  10. import com.jky.mobile_gczjjc.bean.CategoryBean.FirstBean.SecondBean.ThirdBean;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. /**
  14. * 指标 建筑分类 第二级适配器
  15. * @author zlw
  16. */
  17. public class TargetCategorySecondAdapter extends BaseExpandableListAdapter {
  18. private Context mContext;
  19. private List<SecondBean> secondBeans= new ArrayList<>();
  20. private int selectedPos = -1;
  21. public TargetCategorySecondAdapter(Context mContext, List<SecondBean> menuDatas) {
  22. this.mContext = mContext;
  23. this.secondBeans = menuDatas;
  24. }
  25. //选中的position,及时更新数据
  26. public void setSelectedPos(int selectedPos) {
  27. this.selectedPos = selectedPos;
  28. notifyDataSetChanged();
  29. }
  30. public void setData(List<SecondBean> data) {
  31. this.secondBeans = data;
  32. notifyDataSetChanged();
  33. }
  34. @Override
  35. public int getGroupCount() {
  36. return secondBeans.size();
  37. }
  38. @Override
  39. public int getChildrenCount(int groupPosition) {
  40. return secondBeans.get(groupPosition).getItems().size();
  41. }
  42. @Override
  43. public Object getGroup(int groupPosition) {
  44. return secondBeans.get(groupPosition);
  45. }
  46. @Override
  47. public Object getChild(int groupPosition, int childPosition) {
  48. return secondBeans.get(groupPosition).getItems().get(childPosition);
  49. }
  50. @Override
  51. public long getGroupId(int groupPosition) {
  52. return groupPosition;
  53. }
  54. @Override
  55. public long getChildId(int groupPosition, int childPosition) {
  56. return childPosition;
  57. }
  58. @Override
  59. public boolean hasStableIds() {
  60. return true;
  61. }
  62. @Override
  63. public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  64. ViewHolder vh = null;
  65. if (convertView == null) {
  66. vh = new ViewHolder();
  67. convertView = LayoutInflater.from(mContext).inflate(R.layout.item_target_category_second, null);
  68. vh.menu_item_ly = convertView.findViewById(R.id.menu_item_ly);
  69. vh.nameText = convertView.findViewById(R.id.name_rb);
  70. convertView.setTag(vh);
  71. } else {
  72. vh = (ViewHolder) convertView.getTag();
  73. }
  74. SecondBean secondBean = secondBeans.get(groupPosition);
  75. if (secondBean.isSelect){
  76. vh.nameText.setTextColor(mContext.getResources().getColor(R.color.red_text_color));
  77. }else{
  78. vh.nameText.setTextColor(mContext.getResources().getColor(R.color.gray_text_color));
  79. }
  80. vh.nameText.setText(secondBean.getName());//设置标题
  81. return convertView;
  82. }
  83. @Override
  84. public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  85. View view = convertView;
  86. ChildHolder holder = null;
  87. if(view == null){
  88. holder = new ChildHolder();
  89. convertView = LayoutInflater.from(mContext).inflate(R.layout.item_target_category_third, null);
  90. holder.menu_item_ly = convertView.findViewById(R.id.menu_item_ly);
  91. holder.nameText = convertView.findViewById(R.id.name_rb);
  92. convertView.setTag(holder);
  93. }else{
  94. holder = (ChildHolder)convertView.getTag();
  95. }
  96. ThirdBean thirdBean = secondBeans.get(groupPosition).getItems().get(childPosition);
  97. holder.nameText.setText(thirdBean.getName());//设置标题
  98. if (thirdBean.isSelect){
  99. holder.nameText.setTextColor(mContext.getResources().getColor(R.color.red_text_color));
  100. }else{
  101. holder.nameText.setTextColor(mContext.getResources().getColor(R.color.gray_text_color));
  102. }
  103. return convertView;
  104. }
  105. @Override
  106. public boolean isChildSelectable(int groupPosition, int childPosition) {
  107. return true;
  108. }
  109. class ChildHolder{
  110. private View menu_item_ly;
  111. private TextView nameText;
  112. }
  113. private class ViewHolder {
  114. private View menu_item_ly;
  115. private TextView nameText;
  116. }
  117. }

CategoryBean


  
  1. package com.jky.mobile_gczjjc.bean;
  2. import com.google.gson.Gson;
  3. import java.util.List;
  4. /**
  5. * Created by lenovo on 2018/3/13.
  6. */
  7. public class CategoryBean {
  8. /**
  9. * errorCode : 1
  10. * data : [{"name":"居住建筑","type":"","items":[{"name":"普通住宅","type":"","items":[{"name":"保障性住房","type":""}]}]}]
  11. */
  12. private int errorCode;
  13. private List<FirstBean> data;
  14. public List<FirstBean> getData() {
  15. return data;
  16. }
  17. public void setData(List<FirstBean> data) {
  18. this.data = data;
  19. }
  20. public static class FirstBean {
  21. /**
  22. * name : 居住建筑
  23. * type :
  24. * items : [{"name":"普通住宅","type":"","items":[{"name":"保障性住房","type":""}]}]
  25. */
  26. private String name;
  27. private String type;
  28. public boolean isSelect;
  29. private List<SecondBean> items;
  30. public boolean isSelect() {
  31. return isSelect;
  32. }
  33. public void setSelect(boolean select) {
  34. isSelect = select;
  35. }
  36. public String getName() {
  37. return name;
  38. }
  39. public void setName(String name) {
  40. this.name = name;
  41. }
  42. public String getType() {
  43. return type;
  44. }
  45. public void setType(String type) {
  46. this.type = type;
  47. }
  48. public List<SecondBean> getItems() {
  49. return items;
  50. }
  51. public void setItems(List<SecondBean> items) {
  52. this.items = items;
  53. }
  54. public class SecondBean {
  55. /**
  56. * name : 普通住宅
  57. * type :
  58. * items : [{"name":"保障性住房","type":""}]
  59. */
  60. public String name;
  61. public String type;
  62. private List<ThirdBean> items;
  63. public boolean isSelect;
  64. public boolean getIsSelect() {
  65. return isSelect;
  66. }
  67. public void setIsSelect(boolean isSelect) {
  68. this.isSelect = isSelect;
  69. }
  70. public String getName() {
  71. return name;
  72. }
  73. public void setName(String name) {
  74. this.name = name;
  75. }
  76. public String getType() {
  77. return type;
  78. }
  79. public void setType(String type) {
  80. this.type = type;
  81. }
  82. public List<ThirdBean> getItems() {
  83. return items;
  84. }
  85. public void setItems(List<ThirdBean> items) {
  86. this.items = items;
  87. }
  88. public class ThirdBean {
  89. /**
  90. * name : 保障性住房
  91. * type :
  92. */
  93. private String name;
  94. private String type;
  95. public boolean isSelect;
  96. public boolean getIsSelect() {
  97. return isSelect;
  98. }
  99. public void setIsSelect(boolean isSelect) {
  100. this.isSelect = isSelect;
  101. }
  102. public ThirdBean objectFromData(String str) {
  103. return new Gson().fromJson(str, ThirdBean.class);
  104. }
  105. public String getName() {
  106. return name;
  107. }
  108. public void setName(String name) {
  109. this.name = name;
  110. }
  111. public String getType() {
  112. return type;
  113. }
  114. public void setType(String type) {
  115. this.type = type;
  116. }
  117. }
  118. }
  119. }
  120. }

涉及到的所有核心源码都提供了,参照上面的代码应该就可以实现,由于时间关系暂时没有时间整理demo了。希望可以帮助到大家,如果大家还有其他问题欢迎加入我的qq群讨论交流:

开发一群:454430053开发二群:537532956
 

文章来源: wukong.blog.csdn.net,作者:再见孙悟空_,版权归原作者所有,如需转载,请联系作者。

原文链接:wukong.blog.csdn.net/article/details/79728281

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。