BaseExpandableRecyclerViewAdapter 实现二级列表

BaseExpandableRecyclerViewAdapter 实现二级列表

    • 效果图

效果图

BaseExpandableRecyclerViewAdapter 实现二级列表_第1张图片
依赖

implementation 'com.hgDendi:expandable-recyclerview-adapter:1.0.1'

xml-main




    


xml-child




    


xml-group




    

    


代码

package com.example.recyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hgdendi.expandablerecycleradapter.BaseExpandableRecyclerViewAdapter;

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

public class MainActivity extends AppCompatActivity {
    private List  listDate;
    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        ExpandableListView expandableListView=findViewById(R.id.expand_listView);
        listDate=new ArrayList();
        MyChildBean iron=new MyChildBean("钢铁侠");
        MyChildBean meidui=new MyChildBean("美国队长");
        MyChildBean leishen=new MyChildBean("雷神");
        MyChildBean zhizhuxia=new MyChildBean("蜘蛛侠");
        List ban1704bData=new ArrayList<>();
        ban1704bData.add(iron);
        ban1704bData.add(meidui);
        ban1704bData.add(leishen);
        ban1704bData.add(zhizhuxia);
        MyGroup ban1704bGroup=new MyGroup(ban1704bData,"复仇者联盟");


        MyChildBean yaoxinhe=new MyChildBean("橘子");
        MyChildBean yangjian=new MyChildBean("西瓜");
        MyChildBean huosiyuan=new MyChildBean("香蕉");
        List ban1705aData=new ArrayList<>();
        ban1705aData.add(yaoxinhe);
        ban1705aData.add(yangjian);
        ban1705aData.add(huosiyuan);
        MyGroup ban1705aGroup=new MyGroup(ban1705aData,"水果");
        listDate.add(ban1704bGroup);
        listDate.add(ban1705aGroup);

        RecyclerView view =findViewById(R.id.expand_listView);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        view.setLayoutManager(layoutManager);
        view.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        view.setItemAnimator(new DefaultItemAnimator());
        view.setAdapter(new Myadapeter(listDate));
    }

    public class MyChildBean {
        private String name;
        MyChildBean(String name){
            this.name=name;
        }

        public String getName() {
            return name;
        }
    }

    static class GroupVH extends BaseExpandableRecyclerViewAdapter.BaseGroupViewHolder {
        public TextView groupTitle;
        public TextView state;
        GroupVH(View itemView) {
            super(itemView);
            groupTitle=itemView.findViewById(R.id.group);
            state=itemView.findViewById(R.id.right_state);
        }

        // this method is used for partial update.Which means when expand status changed,only a part of this view need to invalidate
        @Override
        protected void onExpandStatusChanged(RecyclerView.Adapter relatedAdapter, boolean isExpanding) {
            if(isExpanding){
                state.setText("展开");
            }else{
                state.setText("收缩");
            }
            relatedAdapter.notifyItemChanged(getAdapterPosition());
        }
    }

    static class ChildVH extends RecyclerView.ViewHolder {
        public TextView childTitle;
        ChildVH(View itemView) {
            super(itemView);
            childTitle=itemView.findViewById(R.id.child);
        }
    }
    public class Myadapeter extends BaseExpandableRecyclerViewAdapter{
        private List   data;
        public Myadapeter(List  da){
            data=da;
        }
        @Override
        public int getGroupCount() {
            return data.size();
        }

        @Override
        public MyGroup getGroupItem(int groupIndex) {
            return data.get(groupIndex);
        }

        @Override
        public GroupVH onCreateGroupViewHolder(ViewGroup parent, int groupViewType) {
            LayoutInflater inflater= LayoutInflater.from(parent.getContext());
            View view=inflater.inflate(R.layout.group,parent,false);
            return new GroupVH(view);
        }

        @Override
        public void onBindGroupViewHolder(GroupVH holder, MyGroup groupBean, boolean isExpand) {
            holder.groupTitle.setText(groupBean.getTitle());
        }

        @Override
        public ChildVH onCreateChildViewHolder(ViewGroup parent, int childViewType) {
            LayoutInflater inflater=LayoutInflater.from(parent.getContext());
            View view=inflater.inflate(R.layout.child,parent,false);
            return new ChildVH(view);
        }

        @Override
        public void onBindChildViewHolder(ChildVH holder, MyGroup groupBean, MyChildBean myChildBean) {
            holder.childTitle.setText(myChildBean.getName());
        }
    }

    public class MyGroup implements BaseExpandableRecyclerViewAdapter.BaseGroupBean {
        private List subData;
        private String title;
        public MyGroup(List sub,String title ){
            subData=sub;
            this.title=title;
        }
        public String getTitle(){
            return title;
        }

        @Override
        public int getChildCount() {
            return subData.size();
        }

        @Override
        public MyChildBean getChildAt(int childIndex) {
            return subData.get(childIndex);
        }

        @Override
        public boolean isExpandable() {
            return true;
        }
    }
}

你可能感兴趣的:(Studio方法)