我有一个包含片段的override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedItem = matchingItems[indexPath.row].placemark
handleMapSearchDelegate?.dropPinZoomIn(selectedItem)
dismiss(animated: true, completion: nil)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.detailTextLabel?.text = parseAddress(selectedItem)
}
,在其中一个片段中我放了标签,在那些标签中我想放置片段,但我不知道怎么做这就是我现在所拥有的接口:
这是我在代码段中有标签并标记错误的代码 我需要添加或更改哪些内容才能使其正常工作?
我的片段代码:
BottomNavigationView
问题出在这一行
public class HomeFragment extends Fragment {
private Toolbar supportActionBar;
public HomeFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
Toolbar myToolbar = (Toolbar) view.findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_whatshot_black_24dp));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_person_black_24dp));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_phone_black_24dp));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
**Here it marks an error that I have wrong**
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
viewPager.setCurrentItem(position);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
public void setSupportActionBar(Toolbar supportActionBar) {
this.supportActionBar = supportActionBar;
}
}
PagerAdapter代码:
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
答案 0 :(得分:1)
您应该将片段FragmentManager用于片段中的片段。
如果需要支持库,则需要导入正确的Fragment类
答案 1 :(得分:1)
getSupportFragmentManager()
。但在你的情况下,你在Fragment中使用tablayout。所以tablayout中的所有片段都使用getChildFragmentManager()
方法进行操作。您可以详细了解getChildFragmentManager()
方法here