2017-05-11 31 views
0

我希望我的RecyclerView能够显示极限数据。例如,如果数据库中有1000条记录。我只想获取50并提供给RecyclerView获得显示。但当用户滚动到第48位时,我想从数据库中获取50多个记录,并将这些记录添加到RecyclerView中。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); 
     } 
    } 
} 
+0

您需要使用endlessScrollListener并从数据库 – Chol

+0

三江源亲爱@Chol – Yasir

回答

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...); 
+0

三江源这么多@Nick调用next项目 – Yasir