0

好吧,我正在試着製作一個像Youtube應用程序那樣的頂部欄。它幾乎適用於所有情況,但是當我使用RecyclerView中的ScrollListener時,出現問題。沿着RecyclerView製作視圖卷軸

正如你可以看到,當滾動查看並不在某一時刻改變其位置。

這裏是我的代碼:

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

      LinearLayoutManager layoutManager = (LinearLayoutManager) layoutManagerTop; 
      if(filterTopAdapter.lastSelected >= layoutManager.findFirstVisibleItemPosition() && filterTopAdapter.lastSelected <= layoutManager.findLastVisibleItemPosition()) { 
       selectionLine.setVisibility(View.VISIBLE); 
       final View view = rvTop.findViewHolderForAdapterPosition(filterTopAdapter.lastSelected).itemView; 
       selectionLine.setTranslationX(view.getLeft() - rvTop.getScrollX()); 
       Log.d("Scroll", String.valueOf(view.getLeft() - rvTop.getScrollX())); 
      } 
      else { 
       selectionLine.setVisibility(View.GONE); 
      } 

     } 

     @Override 
     public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
      super.onScrollStateChanged(recyclerView, newState); 
      LinearLayoutManager layoutManager = (LinearLayoutManager) layoutManagerTop; 
      if(filterTopAdapter.lastSelected >= layoutManager.findFirstVisibleItemPosition() && filterTopAdapter.lastSelected <= layoutManager.findLastVisibleItemPosition()) { 
       selectionLine.setVisibility(View.VISIBLE); 
       final View view = rvTop.findViewHolderForAdapterPosition(filterTopAdapter.lastSelected).itemView; 
       selectionLine.setTranslationX(view.getLeft() - rvTop.getScrollX()); 
       Log.d("Scroll", String.valueOf(view.getLeft() - rvTop.getScrollX())); 
      } 
      else { 
       selectionLine.setVisibility(View.GONE); 
      } 

     } 


    }); 
+0

你應該更喜歡TabLayout與viewpager設置可見分隔高度。 –

回答

1

你應該嘗試TabLayoutViewPager

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="stellar.kade_c.com.MainActivity"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/sliding_tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMode="scrollable" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</RelativeLayout> 
0

你應該ViewPager使用TabLayout這個