android - Fragment is not shown on Activity -
in app,one activity has include 3 fragments, 1 of them remains on activity other 2 have change places. fragments not shown on activity.
i have read somewhere here should change linearlayout fragmentlayout did not work.
public class mainactivity extends actionbaractivity { private final fragment_1to6 f1to6 = new fragment_1to6(); private final fragment_7to12 f7to12 = new fragment_7to12(); private fragmentmanager mfragmentmanager; private framelayout mbuttonslayout, mlayout1to6; private framelayout mlayout7to12; private static final int match_parent = linearlayout.layoutparams.match_parent; private static final int wrap_content = linearlayout.layoutparams.wrap_content; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mbuttonslayout = (framelayout) findviewbyid(r.id.fragment_buttons); mlayout1to6 = (framelayout) findviewbyid(r.id.fragment_1to6); mlayout7to12 = (framelayout) findviewbyid(r.id.fragment_7to12); mfragmentmanager = getfragmentmanager(); final fragmenttransaction ft = mfragmentmanager.begintransaction(); ft.add(r.id.fragment_buttons, new buttonsfragment()); ft.add(r.id.fragment_1to6, f1to6); ft.addtobackstack("fragment_buttons"); ft.addtobackstack("fragment_1to6"); ft.commit(); mfragmentmanager .addonbackstackchangedlistener(new fragmentmanager.onbackstackchangedlistener() { public void onbackstackchanged() { setlayout(); } }); mfragmentmanager.executependingtransactions(); } private void setlayout() { // determine whether fragment has been added if (!f7to12.isadded()) { mbuttonslayout.setlayoutparams(new linearlayout.layoutparams(wrap_content, match_parent, 1f)); mlayout1to6.setlayoutparams(new linearlayout.layoutparams(wrap_content, match_parent, 6f)); } else { mbuttonslayout.setlayoutparams(new linearlayout.layoutparams(wrap_content, match_parent, 1f)); // make titlelayout take 6/13 of layout's width mlayout1to6.setlayoutparams(new linearlayout.layoutparams(wrap_content, match_parent, 6f)); // make quotelayout take 6/13's of layout's width mlayout7to12.setlayoutparams(new linearlayout.layoutparams(wrap_content, match_parent, 6f)); } }
how can solve ?
i had tablerows , reason, deleted them , made linearlayout , shows in preview of fragment not in activity_main. xml code main activity. , below xlm code of fragment activity. , adding fragment activity calling:mbuttonsfragment = (buttonsfragment) mfragmentmanager.findfragmentbyid(r.id.fragment_buttons);
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity" android:orientation="horizontal"> <include android:id="@+id/app_bar" layout="@layout/app_bar" /> <fragment android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/fragment_buttons" class="com.example.user.timeline.buttonsfragment" tools:layout="@layout/fragment_buttons" android:layout_weight="1" android:layout_gravity="left"> </fragment> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" class="com.example.user.timeline.fragment_1to6" android:id="@+id/fragment_1to6" tools:layout="@layout/fragment_buttons" android:layout_weight="3"> </framelayout> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_7to12" android:layout_weight="3"> </framelayout> </linearlayout> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="match_parent" tools:context="com.example.user.timeline.buttonsfragment" android:orientation="vertical" > <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="add activity" android:width="40dp" android:height="30dp" android:background="#b6b6b6" android:textcolor="#000000" android:onclick="onclick" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add activity" android:id="@+id/button2" android:width="40dp" android:height="30dp" android:background="#b6b6b6" android:textcolor="#000000" android:onclick="onclick" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add activity" android:id="@+id/button3" android:layout_column="0" android:width="40dp" android:height="30dp" android:background="#b6b6b6" android:textcolor="#000000" android:onclick="onclick" /> </linearlayout>
Comments
Post a Comment