//我动态生成了多个标签,如30个标签,默认情况下,我已按索引选择了特定标签,但我无法自动滚动到该标签以在屏幕上查看
//首先,我通过传递日期和相应的数据列表来调用片段
viewPager = (ViewPager) findViewById(R.id.view_pager);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new CalenderFragmentPageAdapter(fragmentManager, MainActivity.this, Finaldates, ProductList));
//然后我通过扩展FragmentStatePagerAdapter
来调用哪个设置tab属性public class CalenderFragmentPageAdapter extends FragmentStatePagerAdapter {
private ArrayList tabTitle;
Context context;
ArrayList<Map<String, String>> ProductList;
String finalDate;
public CalenderFragmentPageAdapter(FragmentManager fm, Context context, ArrayList<String> str, ArrayList<Map<String, String>> ProductList) {
super(fm);
this.context = context;
tabTitle = str;
this.ProductList = ProductList;
}
@Override
public Fragment getItem(int position) {
// called new fragment for each generated tab
return calenderFragment;
}
@Override
public int getCount() {
return tabTitle.size();
}
@Override
public CharSequence getPageTitle(int position) {
//Title to show on tab
}
}
//然后我最后加载了我想要的选定标签的片段 由
TabLayout.Tab tab = tabLayout.getTabAt(selectedTabPos);
//现在我可以通过上面的方法为该标签选择片段,但无法自动滚动我已设置selectedTabPos的标签
//的Xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"
app:tabGravity="center"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@color/colorAccent" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_marginTop="115dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
答案 0 :(得分:0)
您必须对其应用水平滚动
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.design.widget.TabLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorMyGreen"/>
试试这个......
答案 1 :(得分:0)
如果您的TabLayout
不可滚动,则可以在XML中为TabLayout
添加以下行:
app:tabMode="scrollable"
答案 2 :(得分:0)
试试这个:
mTablayout.getTabAt(position).select();