gpt4 book ai didi

java - 我需要扩展到fragment 还需要listactivity

转载 作者:行者123 更新时间:2023-12-01 17:52:00 25 4
gpt4 key购买 nike

Android 工作室。所以基本上我的 MainActivity 我有 NavigationDrawer,我用它来转到 fragment 。当我打开这个 fragment 时,我需要它来显示 ListView 。问题是为了让 listview 工作,我需要扩展到 listactivity,但是为了从菜单打开 fragment ,我需要扩展到 Fragment。该怎么办?

这是我的 MainActivity 抽屉导航

@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

Fragment fragment = null;

if (id == R.id.nav_main) {
// Handle the camera action
fragment = new WebListFragment();

} else if (id == R.id.nav_add) {

// Handle the camera action
fragment = new AddFragment();

} else if (id == R.id.nav_tut) {

}

if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

这是我的 fragment

公共(public)类 WebListFragment 扩展 Fragment{

public WebListFragment() {}

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

String[] links = {"Facebook", "Twitter", "StackOverflow"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(getListView().getContext(), android.R.layout.simple_list_item_1, links);
getListView().setAdapter(adapter);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_web_list, container, false);
}

最佳答案

您完全不必使用ListActivity,只需像平常一样创建ListView,然后设置适配器即可。然后就可以毫无问题地扩展Fragment了

ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(adapter);

关于java - 我需要扩展到fragment 还需要listactivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49071712/

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