ExpandableListView实现侧页导航栏目

项目中需要一个需求就是在侧页中显示分类。但是这个侧页的内容是根据后台的数据会进行变更的。第一种思路就是使用textview动态加载布局,比较容易实现;后面发现可以使用ExpandableListView进行实现,比较happy啊。先上效果图

这里写图片描述

好了。实现它。。。

一、首先是适配器,大量的注释来袭



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.coofond.carservice.R;
import com.coofond.carservice.shop.bean.GroupBean;

import java.util.List;

/**
 * ExpandableListAdapter 左侧侧滑页面
 * Created by IBM on 2016/10/17.
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    Context mContext;
    List<GroupBean> mGroupBeans;

    //构造函数
    public MyExpandableListAdapter(Context mContext, List<GroupBean> mGroupBeans) {
        this.mContext = mContext;
        this.mGroupBeans = mGroupBeans;
    }

    //获取组中元素的个数
    @Override
    public int getGroupCount() {
        return mGroupBeans.size();
    }

    //获取指定组中的元素个数
    @Override
    public int getChildrenCount(int groupPosition) {
        if(mGroupBeans.get(groupPosition).getChildLists()==null){
            return 0;
        }
        return mGroupBeans.get(groupPosition).getChildLists().size();
    }

    //获取指定组中的数据
    @Override
    public GroupBean getGroup(int groupPosition) {
        return mGroupBeans.get(groupPosition);
    }

    /**
     * 获取指定组中的指定元素
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public String getChild(int groupPosition, int childPosition) {
        return mGroupBeans.get(groupPosition).getChildLists().get(childPosition);
    }

    //获取指定组的id,这个id必须唯一
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /**
     * 获取指定组中的指定元素id
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    /**
     * 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
     */
    @Override
    public boolean hasStableIds() {
        return false;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition
     *            组位置
     * @param isExpanded
     *            该组是展开状态还是伸缩状态
     * @param convertView
     *            重用已有的视图对象
     * @param parent
     *            返回的视图对象始终依附于的视图组
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        convertView= LayoutInflater.from(mContext).inflate(R.layout.item_group,null);
        TextView tvGroup= (TextView) convertView.findViewById(R.id.tv_item);
        tvGroup.setTextSize(18);
        tvGroup.setPadding(20,10,0,10);
      tvGroup.setText(getGroup(groupPosition).getGroupName()+"");
        return convertView;
    }
    /**
     * 获取一个视图对象,显示指定组中的指定子元素数据。
     *
     * @param groupPosition
     *            组位置
     * @param childPosition
     *            子元素位置
     * @param isLastChild
     *            子元素是否处于组中的最后一个
     * @param convertView
     *            重用已有的视图(View)对象
     * @param parent
     *            返回的视图(View)对象始终依附于的视图组
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.item_group,null);
        }
        TextView tvChild= (TextView) convertView.findViewById(R.id.tv_item);
        tvChild.setTextSize(13);
        tvChild.setPadding(100,10,0,10);
        tvChild.setText(getChild(groupPosition,childPosition));
        return convertView;
    }
    /**
     * 判断子项是否可以选中,如果不写这个,子项点击事件不触发
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

二、xml文件就不写了。然后上核心代码

 @Override
    protected void initData() {
        final List<GroupBean> list = new ArrayList<>();
        List<String> listchlid = new ArrayList<>();
        List<String> listchlid1 = new ArrayList<>();
        listchlid.clear();
        listchlid1.clear();
        listchlid.add("特色产品");
        listchlid.add("母婴用品");
        listchlid.add("食品饮料");
        listchlid.add("家居用品");
        listchlid.add("营养保健");
        listchlid1.add("保  养");
        listchlid1.add("钣  喷");
        listchlid1.add("美  容");
        listchlid1.add("精  品");
        GroupBean bean = new GroupBean();
        bean.setChildLists(listchlid);
        bean.setGroupName("全球海淘");
        GroupBean bean1 = new GroupBean();
        bean1.setChildLists(listchlid1);
        bean1.setGroupName("售后服务");
        GroupBean bean2 = new GroupBean();
        bean2.setChildLists(null);
        bean2.setGroupName("新车销售");
        GroupBean bean3 = new GroupBean();
        bean3.setChildLists(null);
        bean3.setGroupName("一元夺宝");
        list.clear();
        list.add(bean);
        list.add(bean1);
        list.add(bean2);
        list.add(bean3);
        MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(this, list);
        exLeftMenu.setAdapter(mAdapter);
        //默认展开
        for (int i = 0; i < list.size(); i++) {
            exLeftMenu.expandGroup(i);
        }
        //组点击事件
        exLeftMenu.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                ToastUtil.toastCenter2(MainAct.this, list.get(groupPosition).getGroupName());
                return true;
                //把他的组点击事件屏蔽,那么就不会触发展开的事件了
            }
        });

        //子项点击事件
        exLeftMenu.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                ToastUtil.toastCenter2(MainAct.this, list.get(groupPosition).getChildLists().get(childPosition));
                return false;
            }
        });
    }

三、算了,贴个ExpandableListView的xml文件吧。。。主要是解释下属性, android:groupIndicator=”@null”是因为默认会给group级别的item有个向下的箭头。。。不要了。android:listSelector=”@color/textcolorgray”是可以设置选中item后的item的背景颜色。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dl_left"
    android:orientation="vertical">
    <!--主视图-->
    <include layout="@layout/act_main"/>
    <!--侧滑菜单-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:layout_gravity="start">
        <ExpandableListView
            android:id="@+id/lv_leftmenu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:listSelector="@color/textcolorgray"
            android:cacheColorHint="@color/textcolorgray"
            android:groupIndicator="@null"
            >
        </ExpandableListView>
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

总结:我们用了ExpandableListview,它是extendsListView的,有了适配器,那么 我们通过mAdapter.notifyDataSetChanged();后台数据更新,我们也可以通过适配器通知这个列表的更新。比较方便,再有就是取消ExpandableListview的收缩,只要

       //绘制界面过程中默认展开
        for (int i = 0; i < list.size(); i++) {
            exLeftMenu.expandGroup(i);
        }
      //然后再屏蔽group点击事件
        //组点击事件
        exLeftMenu.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                ToastUtil.toastCenter2(MainAct.this, list.get(groupPosition).getGroupName());
                return true;
                //把他的组点击事件屏蔽,那么就不会触发收缩的事件了
            }
        });

那么我们的这个导航栏目的条目就比较清晰了。顺便推荐下侧页的这种覆盖效果。。。本来打算用popwindow的,后来发现android.support.v4.widget.DrawerLayout简直是契合的不得了。相当好用。。。

四、番外,DrawerLayout的简单使用,感觉巨好用啊。你的主界面完全不用动,只要include进来就可以了。要注意下,侧页的布局的开始绘制是android:layout_gravity=”start”就可以了。
then获取这个控件对象,点击按钮,注册事件,就是这么简单~自带手势滑动显示侧页以及关闭侧页。

//打开侧滑页
mDrawerLayout.openDrawer(GravityCompat.START);

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dl_left"
    android:orientation="vertical">
    <!--主视图-->
    <include layout="@layout/act_main"/>
    <!--侧滑菜单-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:layout_gravity="start">
        <ExpandableListView
            android:id="@+id/lv_leftmenu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:listSelector="@color/textcolorgray"
            android:cacheColorHint="@color/textcolorgray"
            android:groupIndicator="@null"
            >
        </ExpandableListView>
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

五、好了,have a nice day!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用ExpandableListView可以实现QQ好友分组列表。首先需要创建一个ExpandableListView控件,并为其设置适配器。适配器需要继承BaseExpandableListAdapter,并实现以下方法: 1. getGroupCount():获取分组的数量。 2. getChildrenCount():获取某个分组下项的数量。 3. getGroup():获取某个分组的数据。 4. getChild():获取某个分组下某个项的数据。 5. getGroupId():获取某个分组的ID。 6. getChildId():获取某个分组下某个项的ID。 7. hasStableIds():判断分组和项的ID是否稳定。 8. getGroupView():获取分组的视图。 9. getChildView():获取项的视图。 10. isChildSelectable():判断项是否可选中。 在实现适配器的过程中,需要根据数据源的结构来设置分组和项的数据。例如,可以使用一个List<List<String>>来存储分组和项的数据,其中外层List表示分组,内层List表示项。在getGroup()和getChild()方法中,需要根据groupPosition和childPosition来获取对应的数据。 最后,需要为ExpandableListView设置分组的展开和收起事件。可以通过设置OnGroupClickListener和OnChildClickListener来实现。在OnGroupClickListener中,需要根据groupPosition来判断当前分组是否已经展开,如果已经展开则返回false,否则返回true。在OnChildClickListener中,可以根据childPosition来获取对应的数据,并进行相应的操作。 通过以上步骤,就可以实现一个简单的QQ好友分组列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值