recyclerview工具集合

引入当前最好的功能,在基础上整合,一个recyclerview功能集合库,首先

感谢

Ultra Pull to Refresh for Android:https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh

Sticky Headers decorator for Android’s RecyclerView:https://github.com/timehop/sticky-headers-recyclerview

A custom RecyclerView which allows for an expandable view to be attached to each ViewHolder:https://github.com/bignerdranch/expandable-recycler-view

Easy sidebar for RecyclerView: https://github.com/CaMnter/EasyRecyclerViewSidebar

起因

由于老需要重复实现:下拉刷新、加载更多,偶尔还要加个HeaderView啥的,每次都要写一些一样的代码,何不做个二次封装,简化使用。
于是WrapperRecyclerView使用组合的方式集成:秋百万的下拉刷新、加载更多。
于是BaseWrapperRecyclerAdapter实现了AddHeader、AddFooter、StickyHeaders、Expandable、Indexable。

如何使用

Gradle

repositories {
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

dependencies {
    compile compile 'com.github.captain-miao:recyclerviewutils:1.1.9'
}

使用WrapperRecyclerView(代替RecyclerView)

<?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:background="@color/light_grey">

    <com.github.captain_miao.recyclerviewutils.WrapperRecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent">

    </com.github.captain_miao.recyclerviewutils.WrapperRecyclerView>
</RelativeLayout>

实现BaseWrapperRecyclerAdapter

//onCreateItemViewHolder代替onCreateViewHolder()
//onBindItemViewHolder代替onBindViewHolder()
public class SimpleAdapter extends BaseWrapperRecyclerAdapter<String, SimpleAdapter.ItemViewHolder> {

    @Override
    public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent, false);

        return new SimpleAdapter.ItemViewHolder(view);
    }


    @Override
    public void onBindItemViewHolder(SimpleAdapter.ItemViewHolder vh, int position) {
        vh.mTvContent.setText(getItem(position));
    }


    public class ItemViewHolder extends ClickableViewHolder{
        public TextView mTvContent;

        public ItemViewHolder(View view) {
            super(view);
            mTvContent = (TextView) view.findViewById(R.id.tv_content);
        }
    }

}

Activity使用

    private SimpleAdapter mAdapter;
    private WrapperRecyclerView mWrapperRecyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        setContentView(R.layout.ac_refresh_recycler_view);
        ...

        mWrapperRecyclerView = (WrapperRecyclerView) findViewById(R.id.recycler_view);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mWrapperRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new SimpleAdapter(new ArrayList<String>());
        mAdapter.setLoadMoreFooterView(new DefaultLoadMoreFooterView(this));
        mWrapperRecyclerView.setAdapter(mAdapter);

        mWrapperRecyclerView.setRecyclerViewListener(new RefreshRecyclerViewListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore(int pagination, int pageSize) {

            }
        });
        mWrapperRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                mWrapperRecyclerView.autoRefresh();
            }
        });
    }

效果图:

refresh_and_load_more

StickyHeaders、Expandable

这两个都是实现对应的adapter就可以了(StickyRecyclerHeadersAdapter、ExpandableRecyclerAdapter)

Indexable

1. 右侧放一个EasySidebar
2. 点击和活动的时候,定位到对应RecyclerView位置

源码

GitHub地址:RecyclerViewUtils

发表评论

电子邮件地址不会被公开。 必填项已用*标注