我已经使用ViewPayer创建了TabLayout,就像这样
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/layout"
layout="@layout/common_act" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:tabGravity="fill"
app:tabIndicatorHeight="4dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
然后我创建了两个片段。
显示TabLayout 我已经为我的TabLayout活动创建了类似下面的代码
public class ManageWorkShopActivity extends BaseActivity {
private ManageWorkShopActBinding mBinding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.manage_work_shop_act);
mBinding.layout.title.setText("Manage Workshop");
setupViewPager(mBinding.viewpager);
mBinding.tabs.setupWithViewPager(mBinding.viewpager);
mBinding.layout.nav.setVisibility(View.VISIBLE);
mBinding.layout.nav.setOnClickListener(view -> onBackPressed());
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ViewProfileFragment(), "Fragment1");
adapter.addFragment(new ViewPerformanceFragment(), "Fragment2");
viewPager.setAdapter(adapter);
}
}
然后我为以下代码创建了适配器。
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
然后如果我运行应用程序,它将显示Fragment1作为默认选择的屏幕。
另一方面,我有一个按钮来导航这个TabLayout活动,如果我点击那个按钮,那么我应该能够将Fragment2作为选定的屏幕。所以任何人都可以建议解决方案
提前致谢。
答案 0 :(得分:0)
您可以使用viewpager.setCurrentItem(int index)
例如,你有一个Button btnToNextFragment;
:
btnToNextFragment.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
yourViewPager.setCurrentItem(int indexOfYourFragment2)
}
});
答案 1 :(得分:-1)
当点击事件发生时,只有pager的setCurrentItem
nxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
});