ExpandRecyclerView使用demo

作者: Ad大成 | 来源:发表于2019-10-17 10:39 被阅读0次

    效果


    效果

    导入依赖

      implementation 'com.thoughtbot:expandablerecyclerview:1.0'
        implementation 'com.android.support:recyclerview-v7:28.0.0'
    

    父级类名GroupText 和xml名group_view_holder

    @SuppressLint("ParcelCreator")
    public class GroupText extends ExpandableGroup<ChildText> {
        public GroupText(String title, List<ChildText> items) {
            super(title, items);
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black">
    
        <TextView
            android:id="@+id/mobile_os"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:drawablePadding="5dp"
            android:drawableRight="@mipmap/ic_launcher"
            android:gravity="left|center"
            android:padding="8dp"
            android:textColor="#e6e600" />
    
    </RelativeLayout>
    

    子级类名ChildText 和xml名 child_view_holder

    public class ChildText implements Parcelable {
        private String name;
    
        public ChildText(Parcel in) {
            name = in.readString();
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public ChildText(String name) {
            this.name = name;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        public static final Creator<ChildText> CREATOR = new Creator<ChildText>() {
            @Override
            public ChildText createFromParcel(Parcel in) {
                return new ChildText(in);
            }
    
            @Override
            public ChildText[] newArray(int size) {
                return new ChildText[size];
            }
        };
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/phone_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:padding="10dp"/>
    </RelativeLayout>
    

    适配器RecyclerAdapter

    public class RecyclerAdapter extends ExpandableRecyclerViewAdapter <RecyclerAdapter.GroupContentViewHolder, RecyclerAdapter.ChildContentViewHolder>{
    
        private Activity activity;
    
        public RecyclerAdapter(Activity activity, List<? extends ExpandableGroup> groups) {
            super(groups);
            this.activity = activity;
        }
    
        @Override
        public GroupContentViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.group_view_holder, parent, false);
            return new GroupContentViewHolder(view);
        }
    
        @Override
        public ChildContentViewHolder onCreateChildViewHolder(ViewGroup parent, final int viewType) {
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.child_view_holder, parent, false);
            return new ChildContentViewHolder(view);
        }
    
        @Override
        public void onBindChildViewHolder(ChildContentViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
            final ChildText child = ((GroupText) group).getItems().get(childIndex);
            holder.onBind(child,group);
        }
    
        @Override
        public void onBindGroupViewHolder(GroupContentViewHolder holder, int flatPosition, ExpandableGroup group) {
            holder.setGroupName(group);
        }
        public class GroupContentViewHolder extends GroupViewHolder {
    
            private TextView name;
    
            public GroupContentViewHolder(View itemView) {
                super(itemView);
                name = (TextView) itemView.findViewById(R.id.mobile_os);
            }
    
            @Override
            public void expand() {
                name.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_launcher, 0);
            }
    
            @Override
            public void collapse() {
                name.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_launcher, 0);
            }
    
            public void setGroupName(ExpandableGroup group) {
                name.setText(group.getTitle());
            }
        }
        public class ChildContentViewHolder extends ChildViewHolder {
            private TextView name;
            public ChildContentViewHolder(View itemView) {
                super(itemView);
                name = (TextView) itemView.findViewById(R.id.phone_name);
            }
    
            public void onBind(ChildText child, ExpandableGroup group) {
                name.setText(child.getName());
                if (group.getTitle().equals("水果")) {
                    name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher_background, 0, 0, 0);
                } else if (group.getTitle().equals("球类")) {
                    name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher_background, 0, 0, 0);
                } else {
                    name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher_background, 0, 0, 0);
                }
            }
        }
    
    }
    

    界面ExpandRecyclerViewActivity

    public class ExpandRecyclerViewActivity extends AppCompatActivity {
    
        private RecyclerView recyclerView;
        private ArrayList<GroupText> mobileOSes;
        private RecyclerAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_expand_recyclerview_layout);
            recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
            mobileOSes = new ArrayList<>();
    
            setData();
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
    
            adapter = new RecyclerAdapter(this, mobileOSes);
            recyclerView.setAdapter(adapter);
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            adapter.onSaveInstanceState(outState);
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            adapter.onRestoreInstanceState(savedInstanceState);
        }
    
        private void setData() {
            ArrayList<ChildText> iphones = new ArrayList<>();
            iphones.add(new ChildText("苹果"));
            iphones.add(new ChildText("橘子"));
            iphones.add(new ChildText("芒果"));
            iphones.add(new ChildText("香蕉"));
            iphones.add(new ChildText("火龙果"));
            iphones.add(new ChildText("草莓"));
            iphones.add(new ChildText("柚子"));
            iphones.add(new ChildText("哈密瓜"));
    
            ArrayList<ChildText> nexus = new ArrayList<>();
            nexus.add(new ChildText("足球"));
            nexus.add(new ChildText("篮球"));
            nexus.add(new ChildText("乒乓球"));
            nexus.add(new ChildText("棒球"));
            nexus.add(new ChildText("保龄球"));
            nexus.add(new ChildText("溜溜球"));
            nexus.add(new ChildText("橄榄球"));
    
            ArrayList<ChildText> windowPhones = new ArrayList<>();
            windowPhones.add(new ChildText("单击游戏"));
            windowPhones.add(new ChildText("主机游戏"));
            windowPhones.add(new ChildText("FPS游戏"));
            windowPhones.add(new ChildText("挂机游戏"));
            windowPhones.add(new ChildText("小游戏"));
            windowPhones.add(new ChildText("手游"));
    
            mobileOSes.add(new GroupText("水果", iphones));
            mobileOSes.add(new GroupText("球类", nexus));
            mobileOSes.add(new GroupText("游戏", windowPhones));
        }
    }
    
    

    相关文章

      网友评论

        本文标题:ExpandRecyclerView使用demo

        本文链接:https://www.haomeiwen.com/subject/oolmmctx.html