android - Three fragments in one one activity -
new android, , i'm trying make app displays name list, right topic heading on right of that, , third fragment displays details on topic heading. i'd 3 fragments diplay on main activity. want this:
so far main xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".mainactivity"> <linearlayout android:id="@+id/companylistfrag_ll" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class= "name.financialnews.companieslistfragment" android:id="@+id/list_companies" android:layout_width="10dp" android:layout_weight="20" android:layout_height="wrap_content" /> <framelayout android:id="@+id/list_headings" android:layout_width="20dp" android:layout_weight="80" android:layout_marginleft="20dp" android:layout_height="wrap_content" android:layout_margintop="10dp" /> </linearlayout> <framelayout android:id="@+id/detail_fragment" android:layout_width="0dp" android:layout_below="@id/list_headings" android:layout_weight="1" android:layout_height="wrap_content" /> </relativelayout>
when run this, im able click on name topic heading on right , when click topic heading, nothing shows detail fragment. can me figure out i'm doing wrong?
thank you
here detail fragment xml:
<framelayout 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:orientation="vertical" > <!-- todo: update blank fragment layout --> <textview android:layout_margintop="30dp" android:id="@+id/textheading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textappearance="?android:attr/textappearancemedium" /> /> <textview android:id="@+id/textdescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </framelayout>
added main activity java:
import android.app.fragmenttransaction; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.app.fragment; public class mainactivity extends appcompatactivity implements companieslistfragment.oncompaniesselectedlistener, headingslistfragment.onheadingsselectedlistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void oncompanyselected(long id){ headingslistfragment headingslist = new headingslistfragment(); fragmenttransaction transaction= getfragmentmanager().begintransaction(); headingslist.updatecompanyid(id); transaction.replace(r.id.list_headings,headingslist); transaction.addtobackstack(null); transaction.settransition(fragmenttransaction.transit_none); transaction.commit(); } @override public void onheadingselected(long id) { detail_fragment items = new detail_fragment(); fragmenttransaction transaction= getfragmentmanager().begintransaction(); items.updateheadingid(id); transaction.replace(r.id.detail_fragment, items); transaction.addtobackstack(null); transaction.settransition(fragmenttransaction.transit_none); transaction.commit(); } }
change root element relativelayout linearlayout , change orientation of vertical. put weight on inner linearlayout both direct children of root element have equal weights. space giving companies list , headings small (20dp not big).
when paste code android studio, there warnings state few problems layout have pointed in right direction. overall, recommend starting simple , adding 1 fragment @ time. 1 thing can turn on setting "show layout bounds" in developer options if having trouble view layouts.
Comments
Post a Comment