首页 > 代码库 > RecyclerView实现底部载入很多其它功能

RecyclerView实现底部载入很多其它功能

     这两天在公司没有什么任务分配,就研究了下咱们Google在Android5.0 推出的一个用来取代ListView的列表控件----RecyclerView。

发现功能上确实比ListView强大了不少,可扩展性也增强了不少。

可是使用过RecyclerView的Programmers应该都知道,RecyclerView无法直接代码加入头布局和脚布局。

那么他喵的怎么样实现最经常使用的底部载入很多其它数据呢?带着这种疑惑我在StackOverFlow上搜索到了一个有參考价值的答案并写了一个Demo实现了它。

參考链接附上:http://stackoverflow.com/questions/30681905/adding-items-to-endless-scroll-recyclerview-with-progressbar-at-bottom?answertab=active#tab-top  

废话不多说。開始进入正题,直接上代码。

第一步:创建两个接口

public interface LoadMoreDataListener {
    public abstract void loadMoreData();
}
public interface RecyclerOnItemClickListener {
    public abstract void onClick(View view);
}

第二步:依据XML中的布局来创建一个Activity

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private TextView tv_empty;
    private List<String> list = new ArrayList<>();
    private List<String> moreData = http://www.mamicode.com/new ArrayList<>();>

 

activity_main

<?xml version="1.0" encoding="utf-8"?

> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.byzk.loadmorerecyclerview.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src=http://www.mamicode.com/"@android:drawable/ic_dialog_email" />>


content_main

<?xml version="1.0" encoding="utf-8"?

> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.byzk.loadmorerecyclerview.MainActivity" tools:showIn="@layout/activity_main"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/sfl" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/mRecyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:padding="5dp" android:scrollbars="vertical"> </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout> <TextView android:id="@+id/tv_empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="暂无数据" android:textColor="@color/colorAccent" android:visibility="gone" /> </RelativeLayout>


创建RecyclerView的条目布局 item_view

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="horizontal"
    card_view:cardUseCompatPadding="true">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="?

android:selectableItemBackground" android:gravity="center"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="5dp" android:text="Name" android:textColor="@android:color/black" android:textSize="18sp" /> </RelativeLayout> </android.support.v7.widget.CardView>


创建脚布局item_footer

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp">

    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

第三步:创建一个RecyclerView的Adapter 这一步是很关键的一步。使用过RecyclerView的programmer应该都知道它无法设置条目点击事件——!

所以必须自己写一个接口回调,来自己定制点击事件。代码例如以下

/**
 * Author: lihongxiang
 * Time: 上午 11:14
 * Email:lihongxiangleo@163.com
 */

public class MyAdapter extends RecyclerView.Adapter {

    private static final int VIEW_ITEM = 0;
    private static final int VIEW_PROG = 1;
    private final Context mContext;
    private final RecyclerView mRecyclerView;
    private List<String> mData;
    private final LayoutInflater inflater;
    private boolean isLoading;
    private int totalItemCount;
    private int lastVisibleItemPosition;
    //当前滚动的position以下最小的items的临界值
    private int visibleThreshold = 5;


    public MyAdapter(Context context, RecyclerView recyclerView) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        mRecyclerView = recyclerView;
        if (mRecyclerView.getLayoutManager() instanceof LinearLayoutManager) {
            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            //mRecyclerView加入滑动事件监听
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    totalItemCount = linearLayoutManager.getItemCount();
                    lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                    Log.d("test", "totalItemCount =" + totalItemCount + "-----" + "lastVisibleItemPosition =" + lastVisibleItemPosition);
                    if (!isLoading && totalItemCount <= (lastVisibleItemPosition + visibleThreshold)) {
                        //此时是刷新状态
                        if (mMoreDataListener != null)
                            mMoreDataListener.loadMoreData();
                        isLoading = true;
                    }
                }
            });
        }

    }

    public void setLoaded() {
        isLoading = false;
    }

   @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        if (viewType == VIEW_ITEM) {
            holder = new MyViewHolder(inflater.inflate(R.layout.item_view, parent, false));
        } else {
            holder = new MyProgressViewHolder(inflater.inflate(R.layout.item_footer, parent, false));
        }
        return holder;
    }
 @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            if (((MyViewHolder) holder).tv_name != null)
                ((MyViewHolder) holder).tv_name.setText(mData.get(position));
        } else if (holder instanceof MyProgressViewHolder) {
            if (((MyProgressViewHolder) holder).pb != null)
                ((MyProgressViewHolder) holder).pb.setIndeterminate(true);
        }
    }


    @Override
    public int getItemCount() {
        return mData =http://www.mamicode.com/= null ? 0 : mData.size();>


通过以上几部操作,就能够实现RecyclerView的底部载入很多其它的功能了,同一时候我用SwipeRefreshLayout实现了下拉刷新。

今天第一次写技术博客,应该有非常多地方写的不够妥当,望各位大神们指点。技术分享实现效果例如以下!

技术分享




RecyclerView实现底部载入很多其它功能