0

我想在我的Staggered gridview中實現加載更多的功能。我已經嘗試了一些代碼行,例如使用addOnScrollListener,但當我到達列表的底部時沒有打電話。
請找到我的代碼,我試圖實現加載更多的功能,但沒有得到預期的結果。
交錯Gridview(Recycleview)加載更多的功能在NestedScrollView內

MY_STRAGGED_RECYCLIVIEW.addOnScrollListener(new RecyclerView.OnScrollListener() { 
        @Override 
        public void onScrolled(RecyclerView recyclerView, 
              int dx, int dy) { 
         super.onScrolled(recyclerView, dx, dy); 

         totalItemCount = staggeredGridLayoutManager.getItemCount(); 
         lastVisibleItem = staggeredGridLayoutManager 
           .findLastCompletelyVisibleItemPositions(null)[0]; 

         if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) { 
          // End has been reached 
          // Do something 
          System.out.println("I amm here dd "); 

          loading = true; 
         } 
        } 
       }); 

在我上面的代碼中,的System.out沒有得到打印...

我曾嘗試另一個偵聽器stragged gridview的是setOnScrollChangeListener,但它也沒有工作

MY_STRAGGED_RECYCLIVIEW.setOnScrollChangeListener(new View.OnScrollChangeListener() { 
      @Override 
      public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 
       System.out.println("I amm here fffffffffff "); 

      } 
     }); 

同樣的問題與此監聽器出現的時候,沒有得到打印我的System.out

更多的東西,我試圖在我的adapter類的onBindViewHolder方法是

if(getCount()==position) 
{ 
////for getting the last item of the recycleview 
} 

上面的代碼中也沒有working..Please幫我短路從這個問題..謝謝:)



@Abbas請檢查下面

我的適配器代碼
public class StraggredView extends RecyclerView.Adapter<StraggredView.ViewHolder> { 

    private List<Content> mDataSet; 
    private Context ctx; 

    public StraggredView(Context context, List<Content> arrList) { 
     ctx = context; 
     mDataSet = arrList; 

    } 

    @Override 
    public StraggredView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid, parent, false); 
     return new ViewHolder(view); 

    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, final int position) { 


     if(getItemCount()==position) 
     { 
      //// I AM NOT GETTING IT AT THE BOTTOM... IT INVOKED AS THE ADAPTER IS CALLED FIRST TIME...LOOK AT IT 
     } 

     if (mDataSet.get(position).getContentImage() == null || mDataSet.get(position).getContentImage().isEmpty()) { 

      Glide.with(ctx).load(R.drawable.no_content) 
        // .override(screenWidth/2, Utils.dpToPx(height)) 
        .into(holder.imgContent); 
     } else { 
      Glide.with(ctx).load(mDataSet.get(position).getContentImage()) 
        // .override(screenWidth/2, Utils.dpToPx(height)) 
        // .centerCrop() 
        //.transform(new CircleTransform()) 
        .into(holder.imgContent); 
     } 

     holder.imgContent.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mDataSet.get(position).getContentImage() != null 
         || !mDataSet.get(position).getContentImage().isEmpty()) { 
        FragmentActivity activity = (FragmentActivity) (ctx); 
        FragmentManager fm = activity.getSupportFragmentManager(); 
        FullScreenFragment dialog = FullScreenFragment.newInstance(
          mDataSet.get(position).getContentImage()); 
        dialog.show(fm, "dialog"); 
       } 
      } 
     }); 

    } 

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

    public class ViewHolder extends RecyclerView.ViewHolder { 
     public ImageView imgContent; 

     public ViewHolder(View itemView) { 
      super(itemView); 

      imgContent = (ImageView) itemView.findViewById(R.id.imgContent); 

     } 


    } 

} 
+0

你可以使用這個鏈接:http://stackoverflow.com/questions/8664935/android-loading-data-like-facebook-and-twitter –

+0

@AmitRanjan。 。我已經嘗試過上面的鏈接,它也不工作的情況下,gridview –

+0

你能看到這段代碼:http://stackoverflow.com/questions/16851934/android-onscrolllistener-for-staggered-grid-view –

回答

0

其實我用的是NestedScrollView這是父我Staggered recycleview。因此addOnScrollListener聽者的viewsetOnScrollChangeListener在它不工作..
我已經在NestedScrollView使用setOnScrollChangeListener它工作得很好。檢查我下面的解決方案,這: -

NestedScrollView myNestedScroll= (NestedScrollView) findViewById(R.id.myNestedScroll); 

myNestedScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { 
    @Override 
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 

     if (scrollY > oldScrollY) { 
      Log.i(TAG, "Scroll DOWN"); 
     } 
     if (scrollY < oldScrollY) { 
      Log.i(TAG, "Scroll UP"); 
     } 

     if (scrollY == 0) { 
      Log.i(TAG, "TOP SCROLL"); 
     } 

     if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) { 
      Log.i(TAG, "BOTTOM SCROLL"); 
     } 
    } 
}); 
相關問題