gpt4 book ai didi

java - 使用 ViewPager 将 RecyclerView 添加到 Tabs fragment

转载 作者:行者123 更新时间:2023-11-30 00:35:12 25 4
gpt4 key购买 nike

我有一个带有 2 个选项卡的简单 fragment ,我想在每个 fragment 中添加一个 recyclerview。我的问题是如何将 recyclerview 附加到每个 fragment ?这是我的主适配器

public class MainActivity extends AppCompatActivity {
private List<ListMenu> menuList = new ArrayList<>();
private RecyclerView recyclerView;
private MenuAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("SINGLE"));
tabLayout.addTab(tabLayout.newTab().setText("PACKET"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final Pageradapter adapter = new Pageradapter(
getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(
new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});
}
}

最佳答案

您必须将 FragmentPagerAdapter 或 PagerAdapter 与 Fragment 一起使用

你可以看到例子:

http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

希望!帮助你

关于java - 使用 ViewPager 将 RecyclerView 添加到 Tabs fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43533734/

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