我有一个包含TabLayout的活动,并为每个选项卡使用一个片段.作为回报,该片段包含与ViewPager一起使用的另一个片段.这是我的设置:
RoutineActivity.java
public class RoutineActivity extends AppCompatActivity {
private ArrayList<CategoryFragment> categories = new ArrayList<>();
private TabLayout tabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routine);
// Set the toolbar as the action bar and add the Up button
Toolbar toolbar = findViewById(R.id.routine_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Get the intent and extract the string array
Intent intent = getIntent();
ArrayList<String> selected =
intent.getStringArrayListExtra(SelectCategoriesActivity.EXTRA_CATEGORIES);
// Set the TabLayout
this.setTabs(selected);
}
// Set the tabs of the TabLayout given a list of tab names …
Run Code Online (Sandbox Code Playgroud) java android android-fragments android-viewpager fragmentstatepageradapter