我无法使用此代码在listview中的Itemclicklistener上打开一个新片段。我该如何解决这个问题?
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
FragmentManager fm = getSupportFragmentManager();
menu = new Menu();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.frag, menu);
ft.commit();
答案 0 :(得分:1)
下面的代码对我有用。试试吧。如果您需要任何澄清,请询问。
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fr = new Menu2(); // next fragment class name is Menu2. Change it to your next class name
FragmentManager fm=getFragmentManager();
android.app.FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.content_main, fr); // your main layout id. not next fragment layout.
ft.addToBackStack(null);
ft.commit();
}
});