ExpandableListView分类
来源:互联网 发布:集思宝数据导出 编辑:程序博客网 时间:2024/06/06 08:28
资源文件xml:
<ExpandableListView android:id="@+id/expandable_view_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" />
初始化:
private void init() { super.onContentChanged(); listView = (ExpandableListView) findViewById(R.id.expandable_view_id); listView.setDivider(null); // 每个分组下的每个子项的 GridView 数据集合 itemList.add(mDatas_jiazheng); itemList.add(mDatas_jiaoyu); itemList.add(mDatas_liren); itemList.add(mDatas_xiuxian); itemList.add(mDatas_yundong); itemList.add(mDatas_meishi); itemList.add(mDatas_hua); itemList.add(mDatas_shi); // 创建适配器 MyExpandableListViewAdapter adapter = new MyExpandableListViewAdapter(ClassifydetilActivity.this, mclassify, itemList); listView.setAdapter(adapter); // 隐藏分组指示器 listView.setGroupIndicator(null); // 默认展开第一组 listView.expandGroup(0);}
private void initData() { mclassify = new ArrayList<Classify>(); mclassify.add(new Classify("蔬菜", R.mipmap.sucai)); mclassify.add(new Classify("水果", R.mipmap.shuiguo)); mclassify.add(new Classify("海鲜", R.mipmap.haixian)); mclassify.add(new Classify("特价区", R.mipmap.tejiaqu)); mclassify.add(new Classify("蛋类", R.mipmap.danlei)); mclassify.add(new Classify("肉禽", R.mipmap.rouqing)); mclassify.add(new Classify("积分商城", R.mipmap.jifengshangcheng)); mclassify.add(new Classify("畅销区", R.mipmap.changxiaoqu));}
数据封装:
private List<String> mDatas_jiazheng = new ArrayList<>(Arrays.asList("土豆", "黄瓜", "小白菜", "茄子", "豆芽", "豆腐", "葱花", "小米"));private List<String> mDatas_jiaoyu = new ArrayList<>(Arrays.asList("苹果", "鸭梨", "葡萄", "哈密瓜", "红提子", "青提子", "驴蹄子", "马蹄子"));private List<String> mDatas_liren = new ArrayList<>(Arrays.asList("大鱼", "小鱼", "海带", "带鱼", "虾米", "螃蟹", "娃娃鱼", "鲤鱼"));private List<String> mDatas_xiuxian = new ArrayList<>();private List<String> mDatas_yundong = new ArrayList<>();private List<String> mDatas_meishi = new ArrayList<>();private List<String> mDatas_hua = new ArrayList<>();private List<String> mDatas_shi = new ArrayList<>();
加载数据:
public class ClassifydetilActivity extends AppCompatActivity { private ExpandableListView listView; public static List<Classify> mclassify; /** * 所有分组的所有子项的 GridView 数据集合 */ public static List<List<String>> itemList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_classifydetil); initData(); init(); }
数据源:
public class MyExpandableListViewAdapter extends BaseExpandableListAdapter { private Context mContext; /** * 每个分组的名字的集合 */ private List<Classify> groupList; /** * 所有分组的所有子项的 GridView 数据集合 */ private List<List<String>> itemList; private NoScrollGridView gridView; public MyExpandableListViewAdapter(Context context, List<Classify> groupList, List<List<String>> itemList) { mContext = context; this.groupList = groupList; this.itemList = itemList; } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return 1; } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return itemList.get(groupPosition).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 false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (null == convertView) { convertView = View.inflate(mContext, R.layout.classify_list_ltem, null); } ImageView ivGroup = (ImageView) convertView.findViewById(R.id.imageView2); ImageView ivGroupHead = (ImageView) convertView.findViewById(R.id.classify_head); TextView tvGroup = (TextView) convertView.findViewById(R.id.classify_text); // 如果是展开状态,就显示展开的箭头,否则,显示折叠的箭头 if (isExpanded) { ivGroup.setImageResource(R.mipmap.icon_xiangxia); } else { ivGroup.setImageResource(R.mipmap.icon_xiangshang); } // 设置分组组名 tvGroup.setText(groupList.get(groupPosition).name); //设置分组图标 ivGroupHead.setImageResource(groupList.get(groupPosition).headId); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (null == convertView) { convertView = View.inflate(mContext, R.layout.gradview, null); } // 因为 convertView 的布局就是一个 GridView, // 所以可以向下转型为 GridView gridView = (NoScrollGridView) convertView; // 创建 GridView 适配器 MyGridViewAdapter gridViewAdapter = new MyGridViewAdapter(mContext, itemList.get (groupPosition)); gridView.setAdapter(gridViewAdapter); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(mContext, "点击了第" + (groupPosition + 1) + "组,第" + (position + 1) + "项", Toast.LENGTH_SHORT).show(); } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; }
item_lsit:
<RelativeLayout android:padding="8dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/classify_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="家政服务" android:layout_centerVertical="true" android:layout_toRightOf="@+id/classify_head" android:layout_toEndOf="@+id/classify_head" android:layout_marginLeft="12dp" android:layout_marginStart="12dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/icon_xiangxia" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="11dp" android:layout_marginEnd="11dp" android:id="@+id/imageView2" /> <ImageView android:id="@+id/classify_head" android:layout_width="35dp" android:layout_height="35dp" android:src="@mipmap/ic_launcher" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /></RelativeLayout>
/** * 首页分类model * zhangy * Created by Administrator on 2017/5/15. */public class Classify { public String name; //用户名 public int headId; //用用户头像资源 public Classify(String name, int headId) { super(); this.name = name; this.headId = headId; } public static class ClassifyBaby1 { public String id; public String name; public String icon; public List<ClassifyBaby2> good_class_two; } public static class ClassifyBaby2 { public String id; public String name; public String icon; }}
阅读全文
0 0
- ExpandableListView分类
- [Android基础] 使用 ExpandableListView 展现分类数据
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- expandableListView
- ExpandableListView
- expandablelistview
- ExpandableListview
- ExpandableListView
- ExpandableListView
- ExpandableListView
- Expandablelistview
- ExpandableListView
- ExpandableListView
- java高并发之CompletionService优化多线程并发
- 人体运动状态预测-实例分析
- MyReport报表引擎2.7.10.0发布
- ActiveMQ入门实例
- Jenkins执行shell重启tomcat
- ExpandableListView分类
- 使用Redis分区将数据分割到多个Redis实例
- Eclipse快捷键
- Android开源项目以及开源库集合(持续更新中)
- opengles加载xml格式的三维模型
- (130)固定光源
- 000043:在主方法中定义一维数组,并用foreach语句遍历该数组
- 数据存储BigDecimal形式
- nth-of-type和nth-child的区别