2017-05-11 12 views
0

RecyclerView에서 데이터를 제한적으로 표시하고 싶습니다. 예를 들어, 데이터베이스에 1000 개의 레코드가있는 경우. 나는 단지 50을 가져오고 RecyclerView에 표시를하고 싶습니다. 그러나 사용자가 48 번째 위치로 스크롤하면 데이터베이스에서 50 개의 레코드를 더 가져와 RecyclerView에 추가하려고합니다.Recyclerview 런타임 데이터 추가

나는 이미 Drawable 폴더에서 데이터를 가져 와서 정상적으로 작동하는 간단한 RecyclerView를 만들었습니다.

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를 사용하고 DB – 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에서 다음 항목을 호출해야합니다 – Yasir

0

당신이 당신의 목록보기에서 페이지 매김을 구현해야합니다.

이렇게하려면 많은 코드 작성 작업이 필요하지 않습니다.

링크를 참조하시기 바랍니다 : - https://github.com/nicolasjafelle/PagingListView