2017-05-11 5 views
0

Ich möchte meine RecyclerView zeigen Daten in Grenzen. zum Beispiel, wenn es 1000 Datensätze in der Datenbank gibt. Ich möchte nur 50 holen und diese zu RecyclerView erhalten Anzeige. aber wenn der Benutzer scrollt hinunter zur 48. Position möchte ich 50 weitere Datensätze aus der Datenbank holen und diese zum RecyclerView hinzufügen.Recyclerview Laufzeitdaten hinzufügen

Ich habe bereits eine einfache RecyclerView erstellt, die Daten aus Drawable-Ordner nimmt und das funktioniert gut.

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

Sie benötigen einen endlessScrollListener verwenden und weiter zum nächsten Artikel aus Ihrem db – Chol

+0

thankyou lieber @Chol – Yasir

Antwort

0

Sie können diese EndlessRecyclerView Scroll-lister zu erreichen verwenden:

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); 

} 

und dann nur

rvList.setOnScrollListener(new EndlessScroll...); 
+0

Thankyou so viel @ Nick nennen – Yasir

Verwandte Themen