public class Fragment_01 extends ListFragment {
String string[]={"Contact 1","Contact 2","Contact 3","Contact 4","Contact 5"};
public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState) {
View myview = inflater.inflate(R.layout.fragment_fragment_01, container, false);
ListView listView= (ListView) myview.findViewById(android.R.id.list);
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,string);
listView.setAdapter(array);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment_03 f3 = new Fragment_03();
ft.replace(R.id.linearLayout2, f3);
ft.commit();
}
});
return myview;
}
}
答案 0 :(得分:0)
不要在另一个内部更改片段。在活动中有框架布局。单击列表视图中的项目,通过界面将事件发送到活动,并在活动中替换片段。这是替换碎片的正确方法。
答案 1 :(得分:0)
Fragment_03 f3 = new Fragment_03();
FragmentManager fragmentManager = ((FragmentActivity) getContext()).getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new Fragment_03())
.commit();
答案 2 :(得分:0)
//I am using this code without listView and this is working fine then why the other is not working ??
public class Fragment_01 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState){
View myview=inflater.inflate(R.layout.activity_fragment_01,container,false);
Button btn = (Button) myview.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm =getFragmentManager();
FragmentTransaction ft =fm.beginTransaction();
Fragment_03 f3 = new Fragment_03();
ft.replace(R.id.linearLayout2,f3);
ft.commit();
}
});
return myview;
}
}