2017-01-27 34 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

谢谢,这工作完美。 –