Recyclerview运行时数据添加

时间:2017-05-11 09:52:36

标签: android android-recyclerview fetch custom-adapter

  

我希望我的RecyclerView能够显示限制数据。例如,如果数据库中有1000条记录。我想只获取50并将它们提供给RecyclerView获取显示。但是当用户向下滚动到第48位时,我想从数据库中获取50多条记录并将它们添加到RecyclerView。

我已经创建了一个简单的RecyclerView,它从Drawable文件夹获取数据并且工作得很好。

public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomAdapter.MyViewHolder> {

    Context context;
    ArrayList<Information> data;
    LayoutInflater inflater;

    public MyCustomAdapter(Context context,ArrayList<Information> data){
        this.context=context;
        this.data=data;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public MyCustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view=inflater.inflate(R.layout.item_row,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.text.setText(data.get(position).title);
        holder.img.setImageResource(data.get(position).imgid);

    }
    @Override
    public int getItemCount() {
        return data.size();
    }

    //for getting refrences to img and txt row
    public class MyViewHolder extends  RecyclerView.ViewHolder{
        ImageView img;
        TextView text;
        public MyViewHolder(View itemView) {
            super(itemView);
            img= (ImageView) itemView.findViewById(R.id.img_row);
            text= (TextView) itemView.findViewById(R.id.txt_row);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用EndlessRecyclerView滚动列表来实现此目的:

    public class EndlessScrollRecyclListener extends RecyclerView.OnScrollListener
    {
    // The total number of items in the dataset after the last load
    private int previousTotalItemCount = 0;
    private boolean loading = true;
    private int visibleThreshold = 5;
    int firstVisibleItem, visibleItemCount, totalItemCount;
    private int startingPageIndex = 0;
    private int currentPage = 0;

    @Override
    public void onScrolled(RecyclerView mRecyclerView, int dx, int dy)
    {
        super.onScrolled(mRecyclerView, dx, dy);
        LinearLayoutManager mLayoutManager = (LinearLayoutManager) mRecyclerView
                .getLayoutManager();

        visibleItemCount = mRecyclerView.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();
        firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
        onScroll(firstVisibleItem, visibleItemCount, totalItemCount);
    }

    public void onScroll(int firstVisibleItem, int visibleItemCount, int totalItemCount)
    {
        // If the total item count is zero and the previous isn't, assume the
        // list is invalidated and should be reset back to initial state
        if (totalItemCount < previousTotalItemCount)
        {
            this.currentPage = this.startingPageIndex;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0)
            {
                this.loading = true;
            }
        }
        // If it’s still loading, we check to see if the dataset count has
        // changed, if so we conclude it has finished loading and update the current page
        // number and total item count.
        if (loading && (totalItemCount > previousTotalItemCount))
        {
            loading = false;
            previousTotalItemCount = totalItemCount;
            currentPage++;
        }

        // If it isn’t currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onLoadMore to fetch the data.
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem +
                visibleThreshold))
        {
            onLoadMore(currentPage + 1, totalItemCount);
            loading = true;
        }
    }

    // Defines the process for actually loading more data based on page
    public abstract void onLoadMore(int page, int totalItemsCount);

}

然后只是

rvList.setOnScrollListener(new EndlessScroll...);

答案 1 :(得分:0)

您需要在列表视图中实现分页。

你不需要做很多编码我们有图书馆来做这件事。

请参阅以下链接: - https://github.com/nicolasjafelle/PagingListView