gpt4 book ai didi

android - 带有 ViewPager 的 TabLayout 不能在 fragment Android 中工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:43 29 4
gpt4 key购买 nike

您好,我在 fragment 中有一个 TabLayout 和 ViewPager。但是我无法在选项卡之间滑动以到达子 fragment (在 Activity 中正常工作但在 fragment 中不工作)。

这是我在 fragment 内的 Tablayout 代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View inflateView = inflater.inflate(R.layout.fragment_assign_beacon_to_event, container, false);

//Viewpager and TabLayout
viewPager = (ViewPager)inflateView.findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getFragmentManager(), getContext()));

tabLayout = (TabLayout)inflateView.findViewById(R.id.tabLayout);


// Inflate the layout for this fragment
return inflateView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);

}
});

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});

Any here is my Custom Adapter class

private class CustomAdapter extends FragmentPagerAdapter {

private String fragments[] = {"Assign Event", "Manage Event"};

public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// return new AssignEventToBeacon();
return new Test1();
case 1:
return new ManageEventAssigned();
default:
return null;
}
}

@Override
public int getCount() {
return fragments.length;
}

@Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}

}

我怀疑这条线有问题:

viewPager.setAdapter(new CustomAdapter(getFragmentManager(), getContext()));

非常感谢任何帮助,谢谢

最佳答案

我做过一个类似的项目,这里是我的 fragment 代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.outer_fragment, container, false);
setHasOptionsMenu(true);

ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}

Log.v("Layout","Tabs");
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
tabLayout.setTabTextColors(Color.parseColor("#707070"), Color.parseColor("#FFFFFF"));
assert viewPager != null;
tabLayout.setupWithViewPager(viewPager);

return view;
}

private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getChildFragmentManager());
adapter.addFragment(new Tab1Fragment(), "PHOTOS");
adapter.addFragment(new Tab2Fragment(), "HI-FIVES");
viewPager.setAdapter(adapter);
}


static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();

public Adapter(FragmentManager fm) {
super(fm);
}

public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}

@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}

@Override
public int getCount() {
return mFragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}

关于android - 带有 ViewPager 的 TabLayout 不能在 fragment Android 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34428511/

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