Issue
I use Xamarin for Android. I have two activities with some content, there is android.support.v4.widget.DrawerLayout
and android.support.design.widget.NavigationView
in XML files.
In MainActivity I use two object for content control. These are DrawerLayout
and NavigationView
. To switch between activities I use own method.
private void OnNavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
switch (e.MenuItem.ItemId)
{
case (Resource.Id.home):
//Go home
break;
case (Resource.Id.settings):
//Go to other activity
break;
}
drawerLayout.CloseDrawers();
}
DrawerLayout
, NavigationView
and OnNavigationItemSelected
is located in MainActivity, but I've created one more activities. This is Settings
and when I chose settings item, I'm moving to Settings
activity.
I wrote the same implementation in Settings
activity. I have DrawerLayout
, NavigationView
and OnNavigationItemSelected
.
It turns out that I just copied code. Maybe I should use inheritance? I just don't understand how can I realize that in Android application.
Solution
Create the common layout.xml for share between each activities .
Put
DrawerLayout
andNavigationView
into a xml we call itA.xml
.Include
A.xml
into the xml of home and settings.
Example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
layout="@layout/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Check https://stackoverflow.com/a/17133102/8187800
Answered By - ColeX - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.