I have a navigation drawer in the MainActivity of my application. In this activity, an AdapterView is set. When I click one of the rows of the adapter, I want a new activity to be opened, but under the same navigation drawer :
searchResultList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(MainActivity.this, Details.class));
}
});
Right now, the new activity (Details) is opened without showing the navigation drawer.
Please show me how to do it, without writing the code of navigation drawer for each activity...
Thanks in advance.
Hey before you look at the answer you should watch this Android dev Summit Video , it will give you a good idea to what to do plus it has a new way of easily achieving a navigational drawer layout. For that you have two options, first and most preferable is to use Fragments
instead of your Activities
and have one FragmentActivity
that has your DrawerLayout
and a FrameLayout
that will contain the `Fragment' view.
<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">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The 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"/>
Thats what is in the Android documentation for using DrawerLayout
you can find it more in details here .
If you are wondering of how you can add Fragments
to your Activity
in code, here is what you have to do:
FragmentManager fm = getSupportFragmentManager();
/*if you are using a FragmentActivity or an AppCompatActivity as
your super class you'll need to use getSupportFragmentManager
method*/
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.YOUR_FRAMELAYOUT_ID, fragment, fargmentTag);
/*Here you should add your frameLayout ID as your first argument
Thats where your new fragment will reside on the screen its like
an iframe.
This will replace the current fragment shown in your frameLayout if any and
add the new one*/
ft.addToBackStack(null);
/*You can add your fragment to your backstack and assign a String value
as its key if you want to get back to it in the future*/
ft.commit();
//By commiting you apply the Fragment transaction and start
//drawing the Fragment on screen
The other option is to have a Parent Activity
that has the code and view setup for the DrawerLayout
and you extend from that ParentActivity
in your Activities
. You will also need to use have an parent_activity.xml
that will have your DrawerLayout
as the main tag and have a ViewStub
to add each of your Activity
view in it.
<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">
<ViewStub
android:id="@+id/layout_stub"
android:inflatedId="@+id/myActivityLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
and here what your ParentActivty
that you should inherit from should look like:
public class ParentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parent_activity);
}
public void setUpChildActivityView(int childActivityView){
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(childActivityView);
View inflated = stub.inflate();
}}
You can call the setUpChildActivtiyView()
method from your new Activity
to set your content, you can also achieve the same result without using a ViewStub
but with inflating and adding your new Activity
view to the ParentActivity
view using addView()
method.
Personally i prefer the first option since its the convention that is there in the documentation, but you can do it either way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.