关闭

Android RecycleView上拉加载

676人阅读 评论(1) 收藏 举报
分类:

上拉加载的思路就是实现OnScrollListener

<pre name="code" class="java"> mRecyclerView.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == list.size()) {
		//这里进行加载数据操作                    
                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });



1、当RecycleView当前可见项的最后一个+1=列表的长度时候。比如列表长度为10,当前可见(从0开始计算)的最后一个是9,就是最后一个了。

2、newState是一个什么东西?

public void stopScroll() {
        setScrollState(SCROLL_STATE_IDLE);
        stopScrollersInternal();
    }



   <span style="font-size:18px;">这是来自RecyclerView的代码片段,从上面可以看出。当RecyclerView停止刷新(上拉或者下拉)的时候设置RecyclerView的状态为SCROLL_STATE_IDLE。当用户停止刷新并且当前可见的item是最后一个就显示上拉加载</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">activity代码</span>
<span style="font-size:18px;"></span><pre name="code" class="java">package com.qd.wangxm.recycleviewloadmore;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.Log;
import android.widget.Toast;

import com.qd.wangxm.recycleviewloadmore.adapter.MyAdapter;

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

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private MyAdapter mAdapter;

    private LinearLayoutManager linearLayoutManager;
    private int lastVisibleItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearLayoutManager = new LinearLayoutManager(this);
        final List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("第" + i + "个数据项");
        }

        mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
        mAdapter = new MyAdapter(list, this);
        RecyclerView.LayoutManager manager = new GridLayoutManager(this, 1, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem  == list.size() && mAdapter.getFooterType() == MyAdapter.FOOTER_LOAD_MORE) {
                    mAdapter.loading();
                    //在这里加载数据
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            List<String> newDatas = new ArrayList<String>();
                            for (int i = 0; i < 5; i++) {
                                int index = i + 1;
                                newDatas.add("more item" + index);
                            }
                            list.addAll(newDatas);
                            //加载完数据调用addItem
                            mAdapter.addItem(list);
                        }
                    }, 1000);
                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });


    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        Log.e("-------------", msg);
    }
}
</pre>adapter代码<pre>
<span style="font-size:18px;">
</span><pre name="code" class="java">package com.qd.wangxm.recycleviewloadmore.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qd.wangxm.recycleviewloadmore.R;

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

/**
 * Created by WangXM on 2016/5/30.
 */
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_ITEM = 0;  //普通Item View
    private static final int TYPE_FOOTER = 1;  //顶部FootView
    public static final int FOOTer_LOADING=1;
    public static final int FOOTER_LOAD_MORE=2;
    public static final int FOOTER_NO_ITEM=0;

    private int footerType=FOOTER_LOAD_MORE;

    private List<String> mList = new ArrayList<>();
    private Context mContext;

    public MyAdapter(List<String> list, Context context) {
        mList = list;
        mContext = context;
    }
    public int getFooterType(){
        return footerType;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_myadapter, parent, false);
            ItemHolder holder = new ItemHolder(view);
            return holder;
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_foot, parent, false);
            FootHolder holder = new FootHolder(view);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position < mList.size()) {
            ((ItemHolder) holder).tvContent.setText(mList.get(position));
        } else {

            if (footerType==FOOTER_LOAD_MORE)
                ((FootHolder) holder).tvContent.setText("上拉加载更多数据");
            else if (footerType==FOOTER_NO_ITEM)
                ((FootHolder) holder).tvContent.setText("已经是最后一页,没有数据了");
            else
                ((FootHolder) holder).tvContent.setText("正在加载更多数据。。。");
        }
    }


    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < mList.size()) {
            return TYPE_ITEM;
        } else {
            return TYPE_FOOTER;
        }
    }

    public void loading(){
        this.footerType=FOOTer_LOADING;
        notifyDataSetChanged();
    }

    public void  addItem(List<String> list){
//        this.mList.clear();
//        mList.addAll(list);
        this.footerType=FOOTER_LOAD_MORE;
        notifyDataSetChanged();

    }
    public void  addItemNoNext(List<String> list){
        this.mList.clear();
        mList.addAll(list);
        this.footerType=FOOTER_NO_ITEM;
        notifyDataSetChanged();

    }

    class ItemHolder extends RecyclerView.ViewHolder {
        public TextView tvContent;

        public ItemHolder(View itemView) {
            super(itemView);
            tvContent = (TextView) itemView.findViewById(R.id.tv_context);
        }
    }
    class FootHolder extends RecyclerView.ViewHolder {
        public TextView tvContent;

        public FootHolder(View itemView) {
            super(itemView);
            tvContent = (TextView) itemView.findViewById(R.id.tv_context);
        }
    }


}



BaseAdapter见下文 http://blog.csdn.net/jing85432373/article/details/51682156

</pre><pre code_snippet_id="1702488" snippet_file_name="blog_20160531_4_7186047" name="code" class="java">
</pre><pre>


1
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:58875次
    • 积分:1077
    • 等级:
    • 排名:千里之外
    • 原创:51篇
    • 转载:8篇
    • 译文:0篇
    • 评论:12条
    最新评论