java - How to replace fragment run time -
java - How to replace fragment run time -
i want add together , replace frame layout fragment on fragment-1 , fragment-2 on item click of grid , list item.
i had created main.xml class
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <linearlayout android:layout_width="0dp" android:layout_weight="1" android:orientation="vertical" android:layout_height="fill_parent" > <fragment android:id="@+id/fragment1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" class="com.example.fragment.fragment1" /> <view android:layout_width="fill_parent" android:layout_height="1dp" android:background="#000000" /> <fragment android:id="@+id/fragment2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" class="com.example.fragment.fragment2" /> </linearlayout> <view android:layout_width="1dp" android:layout_height="fill_parent" android:background="#000000" /> <framelayout android:id="@+id/fragment3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="2"/>
here fragment_1.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingleft="8dp" android:paddingright="8dp" > <gridview android:id="@+id/grid1" android:layout_width="match_parent" android:numcolumns="auto_fit" android:columnwidth="100dp" android:layout_height="0dp" android:layout_weight="1" android:drawselectorontop="false" />
and here row_fragment1_list.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="15dp" android:orientation="vertical" > <imageview android:id="@+id/img_view" android:layout_width="50dp" android:layout_height="50dp" android:background="#cccccc" android:textsize="16sp" android:textstyle="bold" /> <textview android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="16sp" />
and fragment_3.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <textview android:id="@+id/tv_fragment3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="22sp" android:gravity="center" android:text="this 3rd fragment" />
and here main.xml
public class main extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } }
and here fragment1.class
public class fragment1 extends fragment implements onitemclicklistener { activity myactivity; gridview mgridview; private string listitem[] = {"item 1","item 2","item 3","item 4","item 5","item 6","item 7"}; private int imgid[] = {r.drawable.admin_access_rule, r.drawable.admin_backup,r.drawable.admin_browsesite, r.drawable.admin_comment_post,r.drawable.admin_content, r.drawable.admin_content_type,r.drawable.admin_logout,}; @override public void onattach(activity activity) { super.onattach(activity); this.myactivity = activity; toast.maketext(activity.getapplicationcontext(), "on attach", toast.length_short).show(); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); toast.maketext(myactivity.getapplicationcontext(), "on create", toast.length_short).show(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { toast.maketext(myactivity.getapplicationcontext(), "on create view", toast.length_short).show(); view view = inflater.inflate(r.layout.fragment_1,container, false); mgridview = (gridview)view.findviewbyid(r.id.grid1); homecoming view; } @override public void onactivitycreated(bundle savedinstancestate) { toast.maketext(myactivity.getapplicationcontext(), "on activity created", toast.length_short).show(); super.onactivitycreated(savedinstancestate); } @override public void onstart() { super.onstart(); mgridview.setadapter(new gridbaseadapter(myactivity)); mgridview.setonitemclicklistener(this); } private class gridbaseadapter extends baseadapter { layoutinflater mlayoutinflater = null; public gridbaseadapter(context mcontext) { mcontext = myactivity; mlayoutinflater = layoutinflater.from(mcontext); } @override public int getcount() { homecoming listitem.length; } @override public object getitem(int position) { homecoming null; } @override public long getitemid(int position) { homecoming listitem.length; } @override public view getview(final int position, view convertview, viewgroup parent) { if(convertview == null) { convertview = mlayoutinflater.inflate(r.layout.row_fragment_list, null); } imageview mimageview = (imageview) convertview.findviewbyid(r.id.img_view); mimageview.setimageresource(imgid[position]); textview tvuseremail = (textview) convertview.findviewbyid(r.id.text2); tvuseremail.settext("sub " +listitem[position]); homecoming convertview; } } @override public void onitemclick(adapterview<?> arg0, view view, int position, long id) { textview txt3 = (textview)myactivity.findviewbyid(r.id.tv_fragment3); txt3.settext("1st fragment's : " + position +" item clicked"); } }
my fragment2.class
public class fragment2 extends listfragment { activity myactivity; private string listitem[] = {"item 1","item 2","item 3","item 4","item 5","item 6","item 6","item 7"}; @override public void onattach(activity activity) { super.onattach(activity); this.myactivity = activity; toast.maketext(activity.getapplicationcontext(), "on attach", toast.length_short).show(); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); toast.maketext(myactivity.getapplicationcontext(), "on create", toast.length_short).show(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { toast.maketext(myactivity.getapplicationcontext(), "on create view", toast.length_short).show(); /** creating array adapter store list of countries **/ arrayadapter<string> adapter = new arrayadapter<string>(inflater.getcontext(), android.r.layout.simple_list_item_1,listitem); /** setting list adapter listfragment */ setlistadapter(adapter); homecoming super.oncreateview(inflater, container, savedinstancestate); } @override public void onactivitycreated(bundle savedinstancestate) { toast.maketext(myactivity.getapplicationcontext(), "on activity created", toast.length_short).show(); super.onactivitycreated(savedinstancestate); } @override public void onlistitemclick(listview l, view v, int position, long id) { toast.maketext(myactivity.getapplicationcontext(), position +" item clicked", toast.length_short).show(); textview txt3 = (textview)myactivity.findviewbyid(r.id.tv_fragment3); txt3.settext("2nd fragment's : " + position +" item clicked"); super.onlistitemclick(l, v, position, id); } }
and lastly fragment3.class
public class fragment3 extends fragment { activity myactivity; textview txt_view; @override public void onattach(activity activity) { super.onattach(activity); this.myactivity = activity; toast.maketext(activity.getapplicationcontext(), "on attach", toast.length_short).show(); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); toast.maketext(myactivity.getapplicationcontext(), "on create", toast.length_short).show(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { toast.maketext(myactivity.getapplicationcontext(), "on create view", toast.length_short).show(); view view = inflater.inflate(r.layout.fragment_3,container, false); txt_view = (textview)view.findviewbyid(r.id.tv_fragment3); homecoming view; } @override public void onactivitycreated(bundle savedinstancestate) { toast.maketext(myactivity.getapplicationcontext(), "on activity created", toast.length_short).show(); super.onactivitycreated(savedinstancestate); } }
make fragments expose interface activity can attach to. event should occur when desired (e.g. list item clicked). then, activity should place instance of fragment should be, example:
// fragment instance of fragment want show final fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.fragment3, fragment); transaction.commit();
java android android-layout android-fragments android-widget
Comments
Post a Comment