我无法在 Home Fragment 中获取 Support Fragment?
Posted
技术标签:
【中文标题】我无法在 Home Fragment 中获取 Support Fragment?【英文标题】:I cant getSupportFragment in the Home Fragment? 【发布时间】:2021-05-07 16:38:40 【问题描述】:我创建一个主片段并在内部创建选项卡布局并使用 java 类和扩展 fragmentPagerAdapter
然后当我想在主片段中初始化 TabAdapter 时需要 getSupportFragmentManager
并且我无法在片段中设置它(主片段)我能做些什么来修复它?
我的 TabAdapter 代码:
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
public class TabAdapter extends FragmentPagerAdapter
private int numberOfTabs;
public TabAdapter(FragmentManager fm, int behavior)
super(fm);
this.numberOfTabs = behavior;
@NonNull
@Override
public Fragment getItem(int position)
switch (position)
case 0:
return new MobileCatFragment();
case 1:
return new TabletCatFragment();
case 2:
return new LaptopCatFragment();
default:
return null;
@Override
public int getCount()
return numberOfTabs;
当我尝试初始化 TabAdapter 时,我的主片段:
TabAdapter tabAdapter = new TabAdapter( ??? , tlCategory.getTabCount());
tlCategory 是我的 TabLayout...
更新:
这是我的家庭片段代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_home,container,false);
progressDialog = new ProgressDialog(getContext());
sessionManager = new SessionManager(getContext());
TabAdapter tabAdapter = new TabAdapter(getChildFragmentManager() , tlCategory.getTabCount());
imgBtnSearch = view.findViewById(R.id.imgBtnSearch);
imgBtnSearch.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
searchDialog();
);
svBanner = view.findViewById(R.id.svBanner);
svBanner.setAutoCycle(true);
svBanner.setScrollTimeInSec(8);
svBanner.setIndicatorAnimation(IndicatorAnimationType.WORM);
showBanner();
tlCategory = view.findViewById(R.id.tlCategory);
tiMobile = view.findViewById(R.id.tiMobile);
tiTablet = view.findViewById(R.id.tiTablet);
tiLaptop = view.findViewById(R.id.tiLaptop);
vpTabs = vpTabs.findViewById(R.id.vpTabs);
vpTabs.setAdapter(tabAdapter);
tlCategory.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
@Override
public void onTabSelected(TabLayout.Tab tab)
vpTabs.setCurrentItem(tab.getPosition());
@Override
public void onTabUnselected(TabLayout.Tab tab)
@Override
public void onTabReselected(TabLayout.Tab tab)
);
return view;
【问题讨论】:
【参考方案1】:你需要使用getFragmentManager()
【讨论】:
【参考方案2】:如果您从片段中调用 TabAdapter,请使用 TabAdapter tabAdapter = new TabAdapter( getChildFragmentManager(), tlCategory.getTabCount());
您是从 AppCompatActivity/Activity 使用中调用 TabAdapter
TabAdapter tabAdapter = new TabAdapter( getSupportFragmentManager(), tlCategory.getTabCount());
【讨论】:
@mohammadmansouri 请上传 homefragment 代码【参考方案3】:它应该是 ChildFragmentManager 因为你不能用 Fragment 引用调用 getSupportFragmentManger()
TabAdapter tabAdapter = new TabAdapter( getChildFragmentManager() , tlCategory.getTabCount());
或者你有活动参考的电话
requirActivity().getSupportFragmentManager()
【讨论】:
以上是关于我无法在 Home Fragment 中获取 Support Fragment?的主要内容,如果未能解决你的问题,请参考以下文章
在 Fragment 中使用 .getActivity() 后无法访问的语句
如何在activity中获取fragment的控件,然后修改控件的内容,
如何从 Support V4 Fragment 获取父活动?