java - Using a DrawerLayout along with a ListView -
i'm trying write android app listview of few different items , drawerlayout can drag left side of screen. here's mean....
this main screen looks like:
and here's drawer menu thing looks like:
the problem i'm having when open side drawer menu thing , tap option doesn't work , menu closes. i'm able interact main listview page. here's code looks like:
string[] homearray = { "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test" }; private listview homelistview; private arrayadapter arrayadapter; private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; private string[] mdrawertitles; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtitle = mdrawertitle = gettitle(); mdrawertitles = getresources().getstringarray(r.array.menu); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); // set adapter list view mdrawerlist.setadapter(new arrayadapter<string>(this, r.layout.drawer_list_item, mdrawertitles));
and activity_main.xml:
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_height="match_parent" android:layout_width="match_parent" > <framelayout android:id="@+id/content_frame" android:layout_height="match_parent" android:layout_width="match_parent" /> <listview android:id="@+id/left_drawer" android:layout_height="match_parent" android:layout_width="240dp" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@android:color/darker_gray" android:dividerheight="1dp" android:background="#111" /> <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:orientation="vertical" tools:context=".listactivity" > <listview android:id="@+id/homelistview" android:layout_width="match_parent" android:layout_height="match_parent" > </listview> </linearlayout> </android.support.v4.widget.drawerlayout>
i have feeling problem in xml i'm not sure.
please post full code of activity holds navigationdrawer.
furthermore, i recommend (and google) use fragments individual navigationdrawer sections ("settings", "submit bug" , "help" in case).
do not put layout components layout file of activity.
the layout therefore should following:
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- main content view --> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- navigation drawer --> <listview android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="0dp" android:background="#111"/> </android.support.v4.widget.drawerlayout>
the framelayout content_frame fragment holds listview go into. create custom fragment (or use listfragment) contains listview displayed in picture , insert "content_frame".
use replace fragments inside content frame: (when switch section)
/** swaps fragments in main content view */ private void selectitem(int position) { // create new fragment , specify planet show based on position fragment fragment = new yourlistfragment(); // fragment contains list "test" items // insert fragment replacing existing fragment fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.content_frame, fragment) .commit(); // highlight selected item, update title, , close drawer mdrawerlist.setitemchecked(position, true); settitle(mplanettitles[position]); mdrawerlayout.closedrawer(mdrawerlist); }
an example of how fragment like: yourlistfragment.java layout file "list_fragment.xml" contains whatever layout components want. example listview. initialize populate listview inside fragments oncreateview() method.
public class yourlistfragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.list_fragment, container, false); return rootview; } }
all taken googles example on how create navigationdrawer:
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Comments
Post a Comment