gpt4 book ai didi

android - 设计库 TabLayout,选项卡指示器在滚动后弹跳

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:07 24 4
gpt4 key购买 nike

编辑最新的设计库修复了这个问题

compile 'com.android.support:design:+'


我在两个实例中使用 TabLayout,PagerAdapter 和 FragmentStatePagerAdapter。在这两种情况下,当在页面之间滑动时,选项卡指示器会弹跳。单击选项卡换页时,选项卡指示器不会弹跳。

我不确定这是我的错误、已知问题还是指南的一部分。如果您需要更多代码或其他示例,我会发布它。

FragmentStatePagerAdapter

    mViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
LogMeal log = mLogMealAdapter.getItem(position);
return MealViewFragment.newInstance(log.getId());
}

@Override
public CharSequence getPageTitle(int position) { // Tab text
LogMeal logMeal1 = mLogMealAdapter.getItem(position);
String s;
if (logMeal1.getMealName().toString().trim().length() > 12) {
s = logMeal1.getMealName().substring(0, 12) + "..";
} else {
s = logMeal1.getMealName();
}
return s;
}

@Override
public int getCount() {
return mLogMealAdapter.getCount();
}
});

tabs.setTabTextColors(Color.parseColor("#80ffffff"), Color.parseColor("#ffffff"));
if (mLogMealAdapter.getCount() == 1) {
tabs.setTabMode(TabLayout.MODE_FIXED);
} else {
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
}
tabs.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(position);

XML

<RelativeLayout
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.support.v7.widget.Toolbar
android:id="@+id/toolbar_meal_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/primary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@+id/toolbar_meal_view"
android:background="@color/primary"
android:theme="@style/ToolbarTheme"/>

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="@+id/tabs"
android:background="@drawable/toolbar_shadow"/>

</RelativeLayout>

最佳答案

我最近遇到了类似的问题。
我的解决方法:

1) 改变tabs.setupWithViewPager(mViewPager);

    \\ use own OnPageChangeListener 
mViewPager.addOnPageChangeListener(new MyPageScrollListener(mTabLayout));

\\ Manually add tabs, for example:
mTabLayout.addTab(mTabLayout.newTab().setText(mViewPager.getAdapter().getPageTitle(0)));
mTabLayout.addTab(mTabLayout.newTab().setText(mViewPager.getAdapter().getPageTitle(1)));

\\ use own OnTabSelectedListener
mTabLayout.setOnTabSelectedListener(new MyOnTabSelectedListener());

2) 添加自定义监听器的实现

 private class MyPageScrollListener implements ViewPager.OnPageChangeListener {
private TabLayout mTabLayout;

public MyPageScrollListener(TabLayout tabLayout) {
this.mTabLayout = tabLayout;
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
if(mTabLayout != null) {
mTabLayout.getTabAt(position).select();
}
}

@Override
public void onPageScrollStateChanged(int state) {

}
}

private class MyOnTabSelectedListener implements TabLayout.OnTabSelectedListener {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
if (mViewPager.getCurrentItem() != position) {
mViewPager.setCurrentItem(position, true);
}
}

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

}

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

}
}

希望对你有帮助。

关于android - 设计库 TabLayout,选项卡指示器在滚动后弹跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30759858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com