2017-01-27 38 views
7

我遇到了Android TabLayoutTabLayout刪除不必要的滾動

import android.support.design.widget.TabLayout; 

To see what's going on, look at this gif

當我選擇的首要選項卡的左側,然後滾動右側標籤頁,選擇最重要的選項卡中的棘手的問題在右側,TabLayout首先再次顯示左側選項卡,然後滾動到右側選定的選項卡。這是我的設置代碼

void setupTabs(ViewPager viewPager, TabLayout tabLayout) { 
     ProductsPagerAdapter adapter = new ProductsPagerAdapter(getChildFragmentManager(), rootCategory.getChildCategories()); 
     viewPager.setAdapter(adapter); 
     tabLayout.setupWithViewPager(viewPager); 

     setStartingSelection(viewPager, adapter); 


    } 

    private void setStartingSelection(ViewPager viewPager, ProductsPagerAdapter adapter) { 
     for(int i = 0 ; i < adapter.getCount(); i++){ 
      String title = (String) adapter.getPageTitle(i); 
      if(title.equals(selectedCategory.getName())){ 
       viewPager.setCurrentItem(i); 
       break; 
      } 
     } 
    } 

和佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/toolbar_color" 
      app:navigationIcon="@drawable/ic_back_white" 
      app:title="@string/title_transport" 
      app:titleTextColor="@color/title_color"/> 


     <android.support.design.widget.TabLayout 
      android:id="@+id/tab_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabMode="scrollable" 
      app:tabTextColor="@color/white" 
      app:tabIndicatorColor="@color/tab_indicator" 
      app:tabGravity="fill"/> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="0px" 
     android:layout_weight="1" 
     android:background="@android:color/white" /> 

</LinearLayout> 
+0

'AppBarLayout'應該CoordinatorLayout'的'直接孩子,但! https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html – Mohsen

+0

我試圖改變它,但它不會改變任何東西 – RexSplode

+0

如果你在setupTabs中setStartingSelection方法的調用中發表評論,那麼問題依然存在? –

回答

0

經過大量的搜索後,我明白我需要另一個看起來像TabLayout的組件,但不會是TabLayout。所以我用 ViewPagerIndicator

它與TabLayout非常相似,雖然它缺少很酷的選擇動畫和TabLayout中存在的選擇標籤指示符動畫。它也不會調整文本大小來適應製表符,這很好,並且它不會調整製表符以適應文本,這是不好的(嘗試了所有我知道的樣式參數 - 無法使文本換行)。

但它選擇標籤很好,沒有額外的滾動。不幸的是,AndroidArsenal沒有包含比這更好的解決方案(至少在我搜索時沒有)。

0

改變你的方法如下:

private void setStartingSelection(ViewPager viewPager, ProductsPagerAdapter adapter) { 
     int tabPosition=0; 
     for(int i = 0 ; i < adapter.getCount(); i++){ 
      String title = (String) adapter.getPageTitle(i); 
      if(title.equals(selectedCategory.getName())){ 
       tabPosition=i; 
       break; 
      } 
     } 
     viewPager.setCurrentItem(tabPosition); 
    } 

我希望這可以幫助你。

+0

時小部件的滾動方式無關。我很抱歉,但我擔心設置起始選擇在這裏沒有幫助。這種滾動不僅出現在第一次,但每次當你滾動標籤,並選擇其中之一 – RexSplode

1

其實你正在處理滾動問題。是。事情是,TabLayout擴展Horizo​​ntalScrollView。 嘗試像這樣的東西。

public class CustomTabLayout extends TabLayout { 

public CustomTabLayout(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(context); 
} 

public CustomTabLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
} 

public CustomTabLayout(Context context) { 
    super(context); 
    init(context); 
} 

void init(Context ctx){ 

} 

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    // Do not allow touch events. 
    return false; 
} 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    // Do not allow touch events. 
    return false; 
} 

} 
+0

事情是,如果我重寫這些方法,他們我將不得不爲我自己實現所有選擇和滾動行爲。並且假設大多數TabLayout方法都是私有的,這種方法也必須使用Reflection API。但是,這是一種可能性。 – RexSplode

2

你可以試試這個代碼

tabLayout.setupWithViewPager(viewPager); 
    // little hack to prevent unnecessary tab scrolling 
    tabLayout.clearOnTabSelectedListeners(); 
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition(), false); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { } 
    }); 

,或者您也可以在最後一行做到這一點直接

tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) { 
    @Override 
    public void onTabSelected(TabLayout.Tab tab) { 
     viewPager.setCurrentItem(tab.getPosition(), false); 
    } 
}); 
+0

謝謝,這工作完美。 –