gpt4 book ai didi

java - 如何在 Android Studio 3.5 - 最新版本中将选项卡式 Activity 添加到抽屉导航片段中

转载 作者:行者123 更新时间:2023-12-01 19:03:24 25 4
gpt4 key购买 nike

请问如何在 Android Studio 3.5 - 最新版本中将选项卡式 Activity 添加到抽屉导航片段中。我遇到“无法通过 findViewById 和 getSupportFragmentManager 解析方法”的问题。有什么我需要导入的吗,我正在使用最新版本和 androidx.如果我向类中添加实现 FragmentActivity 或任何其他 Activity ,它将引发关闭应用程序的异常。

公共(public)类 SendMoneyFragment 扩展 Fragment {

private SendMoneyViewModel toolsViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
toolsViewModel =
ViewModelProviders.of(this).get(SendMoneyViewModel.class);
View root = inflater.inflate(R.layout.fragment_sendmoney, container, false);
final TextView textView = root.findViewById(R.id.text_sendmoney);
toolsViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;

//setupViewPager();

//method two
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
TabsAdapter adapter = new TabsAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.main_tabs);
tabLayout.setupWithViewPager(viewPager);
}

最佳答案

该解决方案完美运行:

公共(public)类 SendMoneyFragment 扩展 Fragment {

ViewPager pager;


private SendMoneyViewModel toolsViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
toolsViewModel = ViewModelProviders.of(this).get(SendMoneyViewModel.class);
View root = inflater.inflate(R.layout.fragment_sendmoney, container, false);
final TextView textView = root.findViewById(R.id.text_sendmoney);

TabLayout tabs = (TabLayout) root.findViewById(R.id.main_tabs);

pager=(ViewPager) root.findViewById(R.id.pager);

ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());

adapter.addFragment(new MoneyAccount(), "Send To Accounts");
adapter.addFragment(new MoneyAgents(), "Send To Agents");
adapter.addFragment(new MoneyBanks(), "Send To Banks");

pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);

toolsViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;



}



class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mList = new ArrayList<>();
private final List<String> mTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}

@Override
public Fragment getItem(int i) {
return mList.get(i);
}

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

public void addFragment(Fragment fragment, String title) {
mList.add(fragment);
mTitleList.add(title);
}

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

关于java - 如何在 Android Studio 3.5 - 最新版本中将选项卡式 Activity 添加到抽屉导航片段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59598592/

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