热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android仿QQ分组实现二级菜单展示

这篇文章主要为大家详细介绍了Android仿QQ分组实现二级菜单展示,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android仿QQ分组实现二级菜单展示的具体代码,供大家参考,具体内容如下

首先展示下要实现的效果

动态查看请看链接

1.首先要定义item,也就是二级展示的item

child_item.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

 

 

 

 

效果如下图所示:

2. 其次,设置分组item

groupitem.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

 

 

效果下图所示:

3. 创建相应的数据对象

添加分组父菜单Group

Group.class

package com.example.m1.QQGroup;

public class Group {
 private int mGroupImage;
 private String mGroupName; //分组名
 private String mGroupNum; //分组人数
 private boolean isDown;

 public Group(int mGroupImage, String mGroupName, String mGroupNum) {
 this.mGroupImage = mGroupImage;
 this.mGroupName = mGroupName;
 this.mGroupNum = mGroupNum;
 }

 public Group() {
 this.isDown = false;
 }

 public void changeDownStatus(){
 isDown = !isDown;
 }

 public boolean isDown() {
 return isDown;
 }

 public void setDown(boolean down) {
 isDown = down;
 }

 public int getmGroupImage() {
 return mGroupImage;
 }

 public void setmGroupImage(int mGroupImage) {
 this.mGroupImage = mGroupImage;
 }

 public String getmGroupName() {
 return mGroupName;
 }

 public void setmGroupName(String mGroupName) {
 this.mGroupName = mGroupName;
 }

 public String getmGroupNum() {
 return mGroupNum;
 }

 public void setmGroupNum(String mGroupNum) {
 this.mGroupNum = mGroupNum;
 }
}

4. 添加子菜单Item

Item.class

package com.example.m1.QQGroup;

public class Item {
 private String mName;//人名
 private String mMotto; //签名
 private int mPhoto; //头像

 public Item() {

 }

 public Item(String mName, String mMotto) {
 this.mName = mName;
 this.mMotto = mMotto;

 }

 public Item(String mName, String mMotto, int mPhoto) {
 this.mName = mName;
 this.mMotto = mMotto;
 this.mPhoto = mPhoto;
 }

 public String getmName() {
 return mName;
 }

 public void setmName(String mName) {
 this.mName = mName;
 }

 public String getmMotto() {
 return mMotto;
 }

 public void setmMotto(String mMotto) {
 this.mMotto = mMotto;
 }

 public int getmPhoto() {
 return mPhoto;
 }

 public void setmPhoto(int mPhoto) {
 this.mPhoto = mPhoto;
 }
}

5. 添加适配器

MyBaseExpandableListAdapter.class

package com.example.m1.QQGroup;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.m1.R;

import java.util.ArrayList;

public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {

 private ArrayList gData; //分组
 private ArrayList> iData; //长链表
 private Context mContext;

 public MyBaseExpandableListAdapter(ArrayList gData, ArrayList> iData, Context mContext) {
 this.gData = gData;
 this.iData = iData;
 this.mCOntext= mContext;
 }

 @Override
 public int getGroupCount() {
 return gData.size();
 }

 @Override
 public int getChildrenCount(int i) {
 return iData.get(i).size();
 }

 @Override
 public Object getGroup(int i) {
 return gData.get(i);
 }

 @Override
 public Object getChild(int i, int i1) {
 return iData.get(i).get(i1);
 }

 @Override
 public long getGroupId(int i) {
 return i;
 }

 @Override
 public long getChildId(int i, int i1) {
 return i1;
 }

 @Override
 public boolean hasStableIds() {
 return false;
 }

 /**
 * 取得用于显示给定分组的视图,这个方法仅返回分组的试图对象
 * @param i
 * @param b
 * @param view
 * @param viewGroup
 * @return
 */
 @Override
 public View getGroupView(final int i, boolean b, View view, final ViewGroup viewGroup) {
 final ViewHolderGroup groupHolder;
 if (view == null){
  view = LayoutInflater.from(mContext).inflate(R.layout.groupitem,viewGroup,false);
  groupHolder = new ViewHolderGroup();
  groupHolder.mGroupImage = view.findViewById(R.id.triangle_right);
  groupHolder.mGroupName = view.findViewById(R.id.headtext);
  groupHolder.mGroupNum = view.findViewById(R.id.online_people_num);
  view.setTag(groupHolder);
 }else{
  groupHolder = (ViewHolderGroup) view.getTag();
 }
 //groupHolder.mGroupImage.setImageResource(gData.get(i).getmGroupImage());
 Log.d("gData",gData.get(i).getmGroupImage()+"");
 Log.d("gData",gData.get(i).getmGroupName()+"");
 groupHolder.mGroupName.setText(gData.get(i).getmGroupName());
 groupHolder.mGroupNum.setText(gData.get(i).getmGroupNum());

 return view;
 }

 @Override
 public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
 ViewHolderItem itemHolder;
 if (view == null){
  view = LayoutInflater.from(mContext).inflate(R.layout.child_item,viewGroup,false);
  itemHolder = new ViewHolderItem();
  itemHolder.mPhoto = view.findViewById(R.id.iv);
  itemHolder.mMotto = view.findViewById(R.id.motto);
  itemHolder.mName = view.findViewById(R.id.friendname);
  view.setTag(itemHolder);
 }else{
  itemHolder = (ViewHolderItem) view.getTag();
 }

 itemHolder.mPhoto.setImageResource(iData.get(i).get(i1).getmPhoto());
 itemHolder.mName.setText(iData.get(i).get(i1).getmName());
 itemHolder.mMotto.setText(iData.get(i).get(i1).getmMotto());
 return view;
 }

 /**
 * 设置子列表是否可以选中
 * @param i
 * @param i1
 * @return
 */
 @Override
 public boolean isChildSelectable(int i, int i1) {
 return true;
 }

 private static class ViewHolderGroup{
 private ImageView mGroupImage;
 private TextView mGroupName; //分组名
 private TextView mGroupNum; //分组人数
 private boolean isDown;

 public ViewHolderGroup() {
  isDown = false;
 }
 }
 private static class ViewHolderItem{
 private TextView mName;//人名
 private TextView mMotto; //签名
 private ImageView mPhoto; //头像
 }
}

6. Main5Activity中填充数据

Main5Activity.class

package com.example.m1.QQGroup;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.example.m1.R;

import java.util.ArrayList;

public class Main5Activity extends AppCompatActivity {

 private ArrayList gData = null; //存储所有的分组信息
 private ArrayList> iData = null; //每个分组的子信息
 private ArrayList lData = null;
 private Context mContext;
 private ExpandableListView mQQlist;
 private MyBaseExpandableListAdapter myAdapter = null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main5);
 mCOntext= Main5Activity.this;
 mQQlist = findViewById(R.id.QQList);
 //数据准备
 gData = new ArrayList();
 iData = new ArrayList>();
 gData.add(new Group(R.drawable.triangle_right,"小学同学","1/7"));
 gData.add(new Group(R.drawable.triangle_right,"初中同学","2/7"));
 gData.add(new Group(R.drawable.triangle_down,"高中同学","3/7"));
 gData.add(new Group(R.drawable.triangle_right,"大学同学","4/7"));

 lData =new ArrayList();

 //小学组
 lData.add(new Item("朋友1","有志者事竟成",R.drawable.f015));
 lData.add(new Item("朋友2","有志者事竟成",R.drawable.f015));
 lData.add(new Item("朋友3","有志者事竟成",R.drawable.f015));
 lData.add(new Item("朋友4","有志者事竟成",R.drawable.f040));
 lData.add(new Item("朋友5","有志者事竟成",R.drawable.f015));
 lData.add(new Item("朋友6","有志者事竟成",R.drawable.f015));
 lData.add(new Item("朋友7","有志者事竟成",R.drawable.f040));
 iData.add(lData);
 //初中组
 lData =new ArrayList();
 lData.add(new Item("朋友1","我爱你,不是说说而已",R.drawable.f015));
 lData.add(new Item("朋友2","我爱你,不是说说而已",R.drawable.f015));
 lData.add(new Item("朋友3","我爱你,不是说说而已",R.drawable.f040));
 lData.add(new Item("朋友4","我爱你,不是说说而已",R.drawable.f015));
 lData.add(new Item("朋友5","我爱你,不是说说而已",R.drawable.f040));
 lData.add(new Item("朋友6","我爱你,不是说说而已",R.drawable.f015));
 lData.add(new Item("朋友7","我爱你,不是说说而已",R.drawable.f040));
 iData.add(lData);
 //高中组
 lData =new ArrayList();
 lData.add(new Item("朋友1","为赋新词强说愁",R.drawable.f015));
 lData.add(new Item("朋友2","为赋新词强说愁",R.drawable.f040));
 lData.add(new Item("朋友3","为赋新词强说愁",R.drawable.f015));
 lData.add(new Item("朋友4","为赋新词强说愁",R.drawable.f040));
 lData.add(new Item("朋友5","为赋新词强说愁",R.drawable.f015));
 lData.add(new Item("朋友6","为赋新词强说愁",R.drawable.f040));
 lData.add(new Item("朋友7","为赋新词强说愁",R.drawable.f015));
 iData.add(lData);
 //大学组
 lData =new ArrayList();
 lData.add(new Item("朋友1","I love you ",R.drawable.f015));
 lData.add(new Item("朋友2","I love you ",R.drawable.f015));
 lData.add(new Item("朋友3","I love you ",R.drawable.f040));
 lData.add(new Item("朋友4","I love you ",R.drawable.f015));
 lData.add(new Item("朋友5","I love you ",R.drawable.f040));
 lData.add(new Item("朋友6","I love you ",R.drawable.f015));
 lData.add(new Item("朋友7","I love you ",R.drawable.f015));
 iData.add(lData);
 myAdapter = new MyBaseExpandableListAdapter(gData,iData,mContext);
 mQQlist.setAdapter(myAdapter);

 mQQlist.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  @Override
  public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
  Toast.makeText(mContext, "你点击了:" + iData.get(groupPosition).get(childPosition).getmName(), Toast.LENGTH_SHORT).show();
  return true;
  }
 });

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
author-avatar
手机用户2502916411
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有