我制作了一个滑动标签功能,目前正在使用。我也想让它可点击
public class MainActivity extends AppCompatActivity {
public List<String> datafragmnt = new ArrayList<String>();
public ViewPager viewPager;
public pagerfragmetAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datafragmnt.add("Frgmnt 1 ");
datafragmnt.add("Frgmnt 2 ");
datafragmnt.add("Frgmnt 3 ");
datafragmnt.add("Frgmnt 4 ");
myAdapter = new pagerfragmetAdapter(getSupportFragmentManager());
myAdapter.setMydata(datafragmnt);
myAdapter.setFragmntList(new fragment1());
myAdapter.setFragmntList(new fragment2());
myAdapter.setFragmntList(new fragment3());
myAdapter.setFragmntList(new fragment4());
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(myAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
这是我到目前为止所尝试的。欢迎任何帮助! 请帮帮我!
答案 0 :(得分:0)
您需要为该视图页面创建一个适配器,如下所示:
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);
}
}
在SetUpViewPager
中,您可以像这样添加片段:
private void setupViewPager(ViewPager viewPager) {
Main.ViewPagerAdapter adapter = new Main.ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new fragment1(), "FRAGN+MENT UNO");