码迷,mamicode.com
首页 > 其他好文 > 详细

ExpandableListView二级分栏详解完美用法

时间:2016-05-12 12:55:40      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:

对ExpandableListView控件进行封装(未自定义)直接上代码:

通用ViewHolder类,仅在setImageResource中添加代码

package com.svp.haoyan.expandablelistview.tool;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by haoyan on 2016/3/24.
 */
public class ViewHolder {
    //存储数据
    private SparseArray<View> mViews;

    private  int mPosition;
    private View mConvertView;
    private boolean misExpanded;
    public ViewHolder(Context context, ViewGroup parent, int layoutId, int position,boolean isExpanded) {

        this.mPosition=position;
        this.mViews= new SparseArray<View>();
        this.misExpanded=isExpanded;
        //三
        mConvertView= LayoutInflater.from(context).inflate(layoutId,parent,false);
        //一
        mConvertView.setTag(this);
    }
    public static ViewHolder get(Context context,View convertView,
                                 ViewGroup parent,int layoutId,int position,boolean isExpanded){

        if (convertView==null){
            return new ViewHolder(context,parent,layoutId,position,isExpanded);
        }else {
            //五 六
            ViewHolder holder=(ViewHolder)convertView.getTag();
            holder.mPosition=position;
            return holder;
        }
    }
    //通过viewId获取控件
    //泛型T返回子类
    public <T extends View> T getView(int viewId){

        View view =mViews.get(viewId);
        if (view==null){
            //四
            view=mConvertView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T)view;
    }
    //八
    public View getmConvertView() {
        return mConvertView;
    }

    /**
     * 设置TextView的值
     * @param viewId
     * @param text
     * @return
     */
    //四 七 九
    public ViewHolder setText(int viewId,String text){
        TextView tv= getView(viewId);
        tv.setText(text);
        return this;
    }
    /**
     * 一参传id,二参传图片地址,三参为true则调用点击变化效果,默认为false.
     * */
    public ViewHolder setImageResource(int viewId,int resId,boolean expanded){
        ImageView view= getView(viewId);
        view.setImageResource(resId);
        if (expanded){
            if (misExpanded){
                view.setVisibility(View.GONE);
            }else {
                view.setVisibility(View.VISIBLE);
            }
        }
        return this;
    }
    public ViewHolder setImageBitamp(int viewId,Bitmap bitmap){
        ImageView view= getView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }
    public ViewHolder setImageURI(int viewId,String uri){
        ImageView view= getView(viewId);
//        Imageloader.getInstance().loadImg(view,uri);
        return this;
    }

}

实体类Expand_DTO:

package com.svp.haoyan.expandablelistview.dto;

/**
 * Created by haoyan on 2016/5/7.
 */
public class Expand_DTO {

    private String name;
    private String contents;
    private int pic;
    private int photo;

    public static class Expand_child {
        private String childname;
        private String childcont;

        public String getChildname() {
            return childname;
        }

        public void setChildname(String childname) {
            this.childname = childname;
        }

        public String getChildcont() {
            return childcont;
        }

        public void setChildcont(String childcont) {
            this.childcont = childcont;
        }
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContents() {
        return contents;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }

    public int getPic() {
        return pic;
    }

    public void setPic(int pic) {
        this.pic = pic;
    }

    public int getPhoto() {
        return photo;
    }

    public void setPhoto(int photo) {
        this.photo = photo;
    }


}

接下来是最主要的Adapter:

package com.svp.haoyan.expandablelistview;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;

import com.svp.haoyan.expandablelistview.tool.ViewHolder;

import java.util.List;
import java.util.Map;

/**
 * Created by haoyan on 2016/4/28.
 */
public abstract class MyExpandableListAdapter<T,F>implements ExpandableListAdapter {
    private Context mcontext;
    private List<T> mexpand_dtos;
    private Map<Integer, List<F>> mmap = null;
    public MyExpandableListAdapter(Context context, List<T> expand_dtos,Map<Integer, List<F>> map) {
        this.mcontext = context;
        this.mexpand_dtos = expand_dtos;
        this.mmap=map;
    }
    /*注册一个观察者(observer),当此适配器数据修改时即调用此观察者。*/
    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }
    /*取消先前通过registerDataSetObserver(DataSetObserver)方式注册进该适配器中的观察者对象*/
    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }
    /*获取组的个数*/
    @Override
    public int getGroupCount() {
        return mexpand_dtos.size();
    }
    /*返回在指定Group的Child数目。*/
    @Override
    public int getChildrenCount(int groupPosition) {
        return mmap.get(groupPosition).size();
    }
    //获取当前父item的数据
    @Override
    public T getGroup(int groupPosition) {
        return mexpand_dtos.get(groupPosition);
    }
    /*获取与在指定group给予child相关的数据。*/
    @Override
    public F getChild(int groupPosition, int childPosition) {
        return (mmap.get(groupPosition).get(childPosition));
    }
    /*获取指定组的ID*/
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    //得到子item的ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    //组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。(没效果)
    @Override
    public boolean hasStableIds() {
        return true;
    }
    //设置父item组件
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(mcontext, convertView, parent, R.layout.expand_groupview, groupPosition,isExpanded);
        convert(viewHolder, getGroup(groupPosition));
        return viewHolder.getmConvertView();
    }
    //自己写
    public abstract void convert(ViewHolder viewHolder, T t);

    public abstract void convertchild(ViewHolder viewHolder, F f);
    //设置子item的组件
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(mcontext, convertView, parent, R.layout.expand_childview, groupPosition,isLastChild);
        convertchild(viewHolder, getChild(groupPosition,childPosition));
        return viewHolder.getmConvertView();
        /*子元素是否处于组中的最后一个(对每个组的最后一个进行操作)*/
        //boolean isLastChild
        /*下边是未封装代码,提供参考*/
//        String Childname = (mmap.get(groupPosition).get(childPosition)).getChildname();
//        LayoutInflater inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        convertView = inflater.inflate(R.layout.expand_childview, null);
//        TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child);
//        tv_child.setText(Childcont);
    }
    /*是否选中指定位置上的子元素。*/
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    /*true所有条目可以选择和点击*/
    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }
    /*如果当前适配器不包含任何数据则返回True。经常用来决定一个空视图是否应该被显示。
    一个典型的实现将返回表达式getCount() == 0的结果,但是由于getCount()包含了头部和尾部,适配器可能需要不同的行为。*/
    @Override
    public boolean isEmpty() {
        return false;
    }
    /*当组展开状态的时候此方法被调用。*/
    @Override
    public void onGroupExpanded(int groupPosition) {

    }
    /*当组收缩状态的时候此方法被调用。*/
    @Override
    public void onGroupCollapsed(int groupPosition) {

    }
    /*根据所给的子ID号和组ID号返回唯一的ID。此外,若hasStableIds()是true,那么必须要返回稳定的ID。*/
    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }
    /*同上*/
    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }
}

最后是调用类Activity:

package com.svp.haoyan.expandablelistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ExpandableListView;

import com.svp.haoyan.expandablelistview.dto.Expand_DTO;
import com.svp.haoyan.expandablelistview.tool.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ExpandableListView elistview;
    private List<Expand_DTO> expanddata;
    private HashMap<Integer, List<Expand_DTO.Expand_child>> map = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dataView();
        expandView();
    }

    private void expandView() {
        elistview = (ExpandableListView) findViewById(R.id.expandable_lv);
        elistview.setAdapter(new MyExpandableListAdapter<Expand_DTO,Expand_DTO.Expand_child>(this, expanddata,map) {
            @Override
            public void convert(ViewHolder viewHolder, Expand_DTO expand_dto) {
                viewHolder.setText(R.id.tv_expand, expand_dto.getName())
                        .setText(R.id.tv_expand_two, expand_dto.getContents())
                        .setImageResource(R.id.img_expand, expand_dto.getPic(),false)
                        .setImageResource(R.id.img_expand_two, expand_dto.getPhoto(),false);
            }

            @Override
            public void convertchild(ViewHolder viewHolder, Expand_DTO.Expand_child expand_child) {
                viewHolder.setText(R.id.tv_child,expand_child.getChildname())
                          .setText(R.id.tv_child2,expand_child.getChildcont());
            }
        });
        //去掉自带箭头
        elistview.setGroupIndicator(null);
    }

    private void dataView() {
        expanddata = new ArrayList<Expand_DTO>();
        Expand_DTO bean_zero = new Expand_DTO();
        bean_zero.setName("android新技能1");
        bean_zero.setContents("1");
        bean_zero.setPic(R.drawable.icon_check);
        bean_zero.setPhoto(R.drawable.travel_airfare_cn_position);
        expanddata.add(bean_zero);

        Expand_DTO bean_one = new Expand_DTO();
        bean_one.setName("android新技能2");
        bean_one.setContents("2");
        bean_one.setPic(R.drawable.icon_check);
        bean_one.setPhoto(R.drawable.travel_airfare_cn_position);
        expanddata.add(bean_one);

        Expand_DTO bean_two = new Expand_DTO();
        bean_two.setName("android新技能3");
        bean_two.setContents("3");
        bean_two.setPic(R.drawable.icon_check);
        bean_two.setPhoto(R.drawable.travel_airfare_cn_position);
        expanddata.add(bean_two);

        //这里可以使用SparseArray优化性能
        map = new HashMap<Integer, List<Expand_DTO.Expand_child>>();
        List<Expand_DTO.Expand_child> expand_children_zero=new ArrayList<Expand_DTO.Expand_child>();
        Expand_DTO.Expand_child child1=new Expand_DTO.Expand_child();
        child1.setChildname("11111");
        child1.setChildcont("22222");
        expand_children_zero.add(child1);

        Expand_DTO.Expand_child child2=new Expand_DTO.Expand_child();
        child2.setChildname("3333333");
        child2.setChildcont("4444444");
        expand_children_zero.add(child2);
        map.put(0,expand_children_zero);

        List<Expand_DTO.Expand_child> expand_children_one=new ArrayList<Expand_DTO.Expand_child>();
        Expand_DTO.Expand_child child3=new Expand_DTO.Expand_child();
        child3.setChildname("55555");
        child3.setChildcont("66666");
        expand_children_one.add(child3);

        Expand_DTO.Expand_child child4=new Expand_DTO.Expand_child();
        child4.setChildname("77777");
        child4.setChildcont("88888");
        expand_children_one.add(child4);
        map.put(1,expand_children_one);

        List<Expand_DTO.Expand_child> children_data_two=new ArrayList<Expand_DTO.Expand_child>();
        Expand_DTO.Expand_child child5=new Expand_DTO.Expand_child();
        child5.setChildname("99999");
        child5.setChildcont("00000");
        children_data_two.add(child5);

        Expand_DTO.Expand_child child6=new Expand_DTO.Expand_child();
        child6.setChildname("12345");
        child6.setChildcont("54321");
        children_data_two.add(child6);
        map.put(2,children_data_two);

    }
}

完活布局很简单就不贴出来了,加上图片有图有真相:

技术分享

附上写的很好的链接:

ExpandableListView基础写法(数组数据)

ExpandableListView(列表数据)

ExpandableListView二级列表(理论知识)

ExpandableListView实现三级扩展列表

下面是源码链接:

源码链接




ExpandableListView二级分栏详解完美用法

标签:

原文地址:http://blog.csdn.net/emperor_rock/article/details/51366938

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!