| |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
-> 移动开发 -> android 三级级联筛选列表,阿里、百度等大厂技术面试题汇总 -> 正文阅读 |
|
[移动开发]android 三级级联筛选列表,阿里、百度等大厂技术面试题汇总 |
mFirstAdapter = new TargetCategoryFirstAdapter(this, firstBeans); mFirstAdapter.setSelectedBackgroundResource(R.drawable.select_white);//选中时 mFirstAdapter.setHasDivider(false); mFirstAdapter.setNormalBackgroundResource(R.color.target_bg_gray);//未选中 mFirstListView.setAdapter(mFirstAdapter); views.add(view1); views.add(view2);//加载了一二级菜单 mViewPager.setAdapter(new MyPagerAdapter(views)); 初始化数据及点击事件部分 private void initSelectCategory(CategoryBean bean) { if (bean != null && bean.getData() != null) { firstBeans = bean.getData(); } FirstBean fBean = new FirstBean(); fBean.setName(“全部”); fBean.setType(“0”); fBean.setItems(new ArrayList()); fBean.setSelect(true); firstBeans.add(0, fBean); if (mFirstAdapter == null) { mFirstAdapter = new TargetCategoryFirstAdapter(TargetFilterActivity.this, firstBeans); mSecondListView.setAdapter(mSecondAdapter); } else { mFirstAdapter.setData(firstBeans); } mFirstListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { FirstBean firstBean = firstBeans.get(position); for (int i = 0; i < firstBeans.size(); i++) { if (i == position) { firstBeans.get(i).setSelect(true); } else { firstBeans.get(i).setSelect(false); } } if (mFirstAdapter == null) { mFirstAdapter = new TargetCategoryFirstAdapter(TargetFilterActivity.this, firstBeans); mSecondListView.setAdapter(mSecondAdapter); } else { mFirstAdapter.setData(firstBeans); } if (firstBean.getType().equals(“0”)) {//不限 if (mSecondAdapter != null) { mSecondAdapter.setData(new ArrayList()); mSecondAdapter.notifyDataSetChanged(); } } else { mSecondList = firstBean.getItems(); for (int i = 0; i < mSecondList.size(); i++) { mSecondList.get(i).setIsSelect(false); for (int j = 0; j < mSecondList.get(i).getItems().size(); j++) { mSecondList.get(i).getItems().get(j).setIsSelect(false); } } if (mSecondAdapter == null) { mSecondAdapter = new TargetCategorySecondAdapter(TargetFilterActivity.this, mSecondList); mSecondListView.setAdapter(mSecondAdapter); } else { mSecondAdapter.setData(mSecondList); } String secondType = firstBean.getType(); String secondName = firstBean.getName(); if (secondType.equals("-1")) { mProjectType = mFirstProjectType; mProjectTypeName = mFirstProjectTypeName; } else { mProjectType = mFirstProjectType + “/” + secondType; mProjectTypeName = mFirstProjectTypeName + “/” + secondName; } //默认全部展开 for (int i = 0; i < mSecondList.size(); i++) { if (mSecondListView != null) { mSecondListView.expandGroup(i); } } } } }); //二级 mSecondListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) { for (int i = 0; i < mSecondList.size(); i++) { if (i == groupPosition) { mSecondList.get(i).setIsSelect(true); } else { mSecondList.get(i).setIsSelect(false); } for (int j = 0; j < mSecondList.get(i).getItems().size(); j++) { mSecondList.get(i).getItems(
).get(j).setIsSelect(false); } } String thirdType = mSecondList.get(groupPosition).getType(); String thirdName = mSecondList.get(groupPosition).getName(); mProjectType = mFirstProjectType + “/” + thirdType; mProjectTypeName = mFirstProjectTypeName + “/” + thirdName; mSecondAdapter.setData(mSecondList); return true; } }); mSecondListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) { for (int i = 0; i < mSecondList.size(); i++) { if (i == groupPosition) { mSecondList.get(i).setIsSelect(true); } else { mSecondList.get(i).setIsSelect(false); } List<SecondBean.ThirdBean> thirdList = mSecondList.get(i).getItems(); for (int j = 0; j < thirdList.size(); j++) { if (i == groupPosition && j == childPosition) { thirdList.get(j).setIsSelect(true); } else { thirdList.get(j).setIsSelect(false); } } } String thirdType = mSecondList.get(groupPosition).getType(); String thirdName = mSecondList.get(groupPosition).getName(); String fourType = mSecondList.get(groupPosition).getItems().get(childPosition).getType(); String fourName = mSecondList.get(groupPosition).getItems().get(childPosition).getName(); mProjectType = mFirstProjectType + “/” + thirdType + “/” + fourType;//工程类型id mProjectTypeName = mFirstProjectTypeName + “/” + thirdName + “/” + fourName;//工程名称 前端显示 mSecondAdapter.setData(mSecondList); return false; } }); } MyViewPager package com.jky.mobile_gczjjc.widget; import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; /**
*/ public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } public MyViewPager(Context context) { super(context); } //判断menu在x,y的位置 public void scrollTo(int x,int y){ if(getAdapter()==null||x>getWidth()*(getAdapter().getCount()-2)){ return; } super.scrollTo(x,y); } } TargetCategoryFirstAdapter package com.jky.mobile_gczjjc.adapter; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.jky.mobile_gczjjc.R; import com.jky.mobile_gczjjc.bean.CategoryBean.FirstBean; import java.util.ArrayList; import java.util.List; /**
*/ public class TargetCategoryFirstAdapter extends BaseAdapter { private Context mContext; private List firstBeans =new ArrayList<>(); private int mSelectedBackgroundResource;//选中item时的背景颜色 private int mNormalBackgroundResource;//为选中的背景颜色 private boolean hasDivider = true; public void setSelectedBackgroundResource(int mSelectedBackgroundResource) { this.mSelectedBackgroundResource = mSelectedBackgroundResource; } public void setNormalBackgroundResource(int mNormalBackgroundResource) { this.mNormalBackgroundResource = mNormalBackgroundResource; } public void setHasDivider(boolean hasDivider) { this.hasDivider = hasDivider; } public TargetCategoryFirstAdapter(Context mContext, List firstBeans) { this.mContext = mContext; this.firstBeans = firstBeans; } public void setData(List data) { this.firstBeans = data; notifyDataSetChanged(); } @Override public int getCount() { if (firstBeans == null) { return 0; } return firstBeans.size(); } @Override public Object getItem(int position) { if (firstBeans == null) { return null; } return firstBeans.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder vh = null; if (convertView == null) { vh = new ViewHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.item_target_category_first, null); vh.itemLayout = convertView.findViewById(R.id.menu_item_ly); vh.nameText = convertView.findViewById(R.id.name_rb); vh.dividerTextView = convertView.findViewById(R.id.menu_item_divider); convertView.setTag(vh); } else { vh = (ViewHolder) convertView.getTag(); } FirstBean firstBean = firstBeans.get(position); vh.nameText.setText(firstBean.getName());//设置标题 if (firstBean.isSelect){ vh.nameText.setTextColor(mContext.getResources().getColor(R.color.common_blue)); vh.itemLayout.setBackgroundResource(mSelectedBackgroundResource); }else{ vh.nameText.setTextColor(Color.BLACK); vh.itemLayout.setBackgroundResource(mNormalBackgroundResource); } //隐藏view vh.dividerTextView.setVisibility(hasDivider ? View.VISIBLE : View.INVISIBLE); return convertView; } private class ViewHolder{ private View itemLayout; private TextView nameText; private TextView dividerTextView; } } TargetCateGorySecondAdapter package com.jky.mobile_gczjjc.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; import com.jky.mobile_gczjjc.R; import com.jky.mobile_gczjjc.bean.CategoryBean.FirstBean.SecondBean; import com.jky.mobile_gczjjc.bean.CategoryBean.FirstBean.SecondBean.ThirdBean; import java.util.ArrayList; import java.util.List; /**
*/ public class TargetCategorySecondAdapter extends BaseExpandableListAdapter { private Context mContext; private List secondBeans= new ArrayList<>(); private int selectedPos = -1; public TargetCategorySecondAdapter(Context mContext, List menuDatas) { this.mContext = mContext; this.secondBeans = menuDatas; } //选中的position,及时更新数据 public void setSelectedPos(int selectedPos) { this.selectedPos = selectedPos; notifyDataSetChanged(); } public void setData(List data) { this.secondBeans = data; notifyDataSetChanged(); } @Override public int getGroupCount() { return secondBeans.size(); } @Override public int getChildrenCount(int groupPosition) { return secondBeans.get(groupPosition).getItems().size(); } @Override public Object getGroup(int groupPosition) { return secondBeans.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return secondBeans.get(groupPosition).getItems().get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewHolder vh = null; if (convertView == null) { vh = new ViewHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.item_target_category_second, null); vh.menu_item_ly = convertView.findViewById(R.id.menu_item_ly); vh.nameText = convertView.findViewById(R.id.name_rb); convertView.setTag(vh); } else { vh = (ViewHolder) convertView.getTag(); } SecondBean secondBean = secondBeans.get(groupPosition); if (secondBean.isSelect){ vh.nameText.setTextColor(mContext.getResources().getColor(R.color.red_text_color)); }else{ vh.nameText.setTextColor(mContext.getResources().getColor(R.color.gray_text_color)); } vh.nameText.setText(secondBean.getName());//设置标题 return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = convertView; ChildHolder holder = null; if(view == null){ holder = new ChildHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.item_target_category_third, null); holder.menu_item_ly = convertView.findViewById(R.id.menu_item_ly); holder.nameText = convertView.findViewById(R.id.name_rb); convertView.setTag(holder); }else{ holder = (ChildHolder)convertView.getTag(); } ThirdBean thirdBean = secondBeans.get(groupPosition).getItems().get(childPosition); holder.nameText.setText(thirdBean.getName());//设置标题 if (thirdBean.isSelect){ holder.nameText.setTextColor(mContext.getResources().getColor(R.color.red_text_color)); }else{ holder.nameText.setTextColor(mContext.getResources().getColor(R.color.gray_text_color)); } return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class ChildHolder{ private View menu_item_ly; private TextView nameText; } private class ViewHolder { private View menu_item_ly; private TextView nameText; } } CategoryBean package com.jky.mobile_gczjjc.bean; import com.google.gson.Gson; import java.util.List; /**
*/ public class CategoryBean { /**
*/ private int errorCode; private List data; public List getData() { return data; } public void setData(List data) { this.data = data; } public static class FirstBean { /**
*/ private String name; private String type; public boolean isSelect; private List items; |
|
移动开发 最新文章 |
Vue3装载axios和element-ui |
android adb cmd |
【xcode】Xcode常用快捷键与技巧 |
Android开发中的线程池使用 |
Java 和 Android 的 Base64 |
Android 测试文字编码格式 |
微信小程序支付 |
安卓权限记录 |
知乎之自动养号 |
【Android Jetpack】DataStore |
|
上一篇文章 下一篇文章 查看所有文章 |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 | -2024/11/24 8:45:13- |
|
网站联系: qq:121756557 email:121756557@qq.com IT数码 |