前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ExpandRecyclerView

ExpandRecyclerView

作者头像
code_horse
发布2018-07-02 14:25:02
1.2K0
发布2018-07-02 14:25:02
举报
文章被收录于专栏:Android NoteAndroid Note
前言

大家经常会用到RecycleView,RecycleView虽然很好用,但是没有了ExpandListView的功能,这时候,我们就需要自己去写了。今天给大家介绍一个ExpandableRecyclerview,个人感觉封装挺好的,分享给大家。

先看效果图:

效果是模拟机录的,看起来会闪烁的感觉,真机没有的!!!

引入依赖库

代码语言:javascript
复制
compile 'com.thoughtbot:expandablerecyclerview:1.0'

定制POJO类

代码语言:javascript
复制
//Group
@SuppressLint("ParcelCreator")
public class GroupText extends ExpandableGroup<ChildText> {
    public GroupText(String title, List<ChildText> items) {
        super(title, items);
    }
}
代码语言:javascript
复制
//Child
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];
        }
    };
}
GroupViewHolder和ChildViewHolder
代码语言:javascript
复制
//Group
public class GroupContentViewHolder extends cn.expandrecyclerview.demo.viewholders.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.below_arrow_icon, 0);
    }

    @Override
    public void collapse() {
        name.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.up_arrow, 0);
    }

    public void setGroupName(ExpandableGroup group) {
        osName.setText(group.getTitle());
    }
}

GroupViewHolder 布局文件

代码语言:javascript
复制
<?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/down_arrow"
        android:gravity="left|center"
        android:padding="8dp"
        android:textColor="#e6e600" />

</RelativeLayout>
代码语言:javascript
复制
//ChildViewHolder
public class ChildContentViewHolder extends cn.expandrecyclerview.demo.viewholders.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, 0, 0, 0);
        } else if (group.getTitle().equals("球类")) {
            name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
        } else {
            name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
        }
    }
}

ChildViewHolder布局文件

代码语言:javascript
复制
<?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
代码语言:javascript
复制
public class RecyclerAdapter extends ExpandableRecyclerViewAdapter<OSViewHolder, PhoneViewHolder> {

    private Activity activity;

    public RecyclerAdapter(Activity activity, List<? extends ExpandableGroup> groups) {
        super(groups);
        this.activity = activity;
    }

    @Override
    public OSViewHolder 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 PhoneViewHolder 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);
    }
}
使用方法
代码语言:javascript
复制
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));
    }
}

activity 布局文件

代码语言:javascript
复制
<?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="match_parent"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

最后附上library地址,喜欢的可以去自己去研究研究。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • GroupViewHolder和ChildViewHolder
  • RecyclerAdapter
  • 使用方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档