我正在尝试从SideMenu加载片段。侧边菜单中有一个列表视图,我想在应用程序中加载片段。列表视图中有3个项目。点击第0和第3个位置的代码工作正常,但不适用于第1个位置,即i == 1.
我必须为此做些什么!
package comm.design.amer.sidemenu_new;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;<code>
public class MainActivity extends AppCompatActivity {
private ArrayList<listView> Listview = new ArrayList<listView>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideview);
//Create the ListView Items
Listview.add(new listView("page1", "enter", R.drawable.a));
Listview.add(new listView("page2", "enter", R.drawable.b));
Listview.add(new listView("page3", "enter", R.drawable.c));
Listview.add(new listView("page4", "enter", R.drawable.d));
//Call the Adapter
mycomstumerAdapter adapter = new mycomstumerAdapter(this, Listview);
ListView listview = (ListView) findViewById(R.id.list);
listview.setAdapter(adapter);
//make selection
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
selectItem(i);
}
});
}
public void selectItem (int i){
if (i==0){
Toast.makeText(getApplicationContext(),"bn",Toast.LENGTH_LONG).show();}
else if (i==1) {
Fragment1 fragment0 = new Fragment1();
FragmentManager fragmentm = getFragmentManager();
fragmentm.beginTransaction()
.add(R.id.linear,fragment0)
.commit();
}
else if (i==2)
finish();
}
public class mycomstumerAdapter extends BaseAdapter {
Context context;
ArrayList<listView> Listview;
public mycomstumerAdapter(Context context, ArrayList<listView> Listview) {
this.context = context;
this.Listview = Listview;
}
@Override
public int getCount() {
return Listview.size();
}
@Override
public Object getItem(int i) {
return Listview.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1;
if (view == null) {
LayoutInflater inflater = getLayoutInflater();
view1 = inflater.inflate(R.layout.listview, null);
} else
view1 = view;
TextView title = (TextView) view1.findViewById(R.id.textView);
TextView detail = (TextView) view1.findViewById(R.id.textView2);
ImageView imageView = (ImageView) view1.findViewById(R.id.imageView);
title.setText(Listview.get(i).Title);
detail.setText(Listview.get(i).Detail);
imageView.setImageResource(Listview.get(i).imageView);
return view1;
}
}
}
答案 0 :(得分:2)
在你的代码中我看到你正在使用添加片段而不是尝试更换片段。请参阅以下代码: -
Fragment1 fragment0 = new Fragment1();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.linear,fragment0);
fragmentTransaction.commit();