2017-04-20 168 views
2

如何更新滾動位置。現在每當我點擊物品時,位置都會更新。我想每次滾動時更新位置。我想要做的是有一個文本視圖,它將在滾動項目時得到更新。滾動時更新recyclerview的位置(android)

recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(this, recyclerView, new GalleryAdapter.ClickListener() 
      { 

       @Override 
       public void onClick(View view, int position) { 

        Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class); 
        intent.putExtra(PDFViewerActivity.TAG, books.get(position)); 
        intent.putExtra("from", "mainActivityCarasoul"); 
        startActivity(intent); 
       } 
       @Override 
       public void onLongClick(View view, int position) { 
       } 
      })); 

      recyclerView.addOnScrollListener(new CenterScrollListener()); 

      recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 
       @Override 
       public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
        super.onScrollStateChanged(recyclerView, newState); 
        // title.setText(books.get(position).getName()); 

       } 
      }); 
+1

您onBindView持有人你是什麼意思更新的位置?你需要描述你想要做得更清楚的事情。 –

+0

我更新了問題 –

+0

退房http://stackoverflow.com/questions/24989218/get-visible-items-in-recyclerview –

回答

0

嗨,你可以使用下面的代碼,

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
    { 
     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
     { 
      GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager()); 
      int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition(); 
      int findFirstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition(); 
      int findLastVisibleItemPosition = layoutManager.findLastVisibleItemPosition(); 
      int findLastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition(); 
     } 
    }); 
+0

我得到這個錯誤java.lang.ClassCastException:com.azoft.carousellayoutmanager.CarouselLayoutManager can not be強制轉換爲android.support.v7.widget.GridLayoutManager –

+0

我認爲你的setLayoutManager是CarouselLayoutManager,所以你可以在上面的代碼中將GridLayoutManager更改爲CarouselLayoutManager。即CarouselLayoutManager layoutManager =((CarouselLayoutManager)mRecyclerView.getLayoutManager()); –

+1

是的,我試過,但我在這4行findFirstVisibleItemPosition(); –

0

利用這一點,你必須保持你的活動,並在您的適配器類selectedItem屬性爲INT intialize previousPoistion = -1 previousPosition;和selectedPosition = 1;

@Override 
     public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
      super.onScrollStateChanged(recyclerView, newState); 
      switch (newState) { 
       case 0: 
        int pos = linearLayoutManager1.findLastVisibleItemPosition(); 
        yourAdapter.selectedPosition = pos - 1; 
        previousPosition = pos - 1; 
        yourTextView.setText(yourLIst.get(pos-1)); 
        break; 
      } 
     } 
    }); 

和適配器

@Override 
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { 
if (position == selectedPosition) { 
      //do what you want when selected 
      } 
} 
相關問題