我创建了一个导航抽屉活动。我想创建一个在单击抽屉列表中的相应项目时打开的片段。
如何实现此功能,以便在单击相应项目时显示图像?
答案 0 :(得分:1)
让我分享一个简单而完整的代码来使用Navigation Drawer。 MainActivity类。
ask one-of patches with [distancexy 0 0 < 30]
[ if (random-float 1 < (beta * pm * 1000)) [sprout-parties 1 [initialize-party] ]]
}
activity_main.xml文件如下所示:
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
private NavigationView navigationView;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle =
new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) {
@Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView){
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
@Override
public boolean onNavigationItemSelected(MenuItem item){
int id = item.getItemId();
switch (id){
case R.id.item1:
/* getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MySimpleFragment())
.addToBackStack(null)
.commit();*/
Intent intent = new Intent(this, Class1.class);
startActivity(intent);
break;
case R.id.item2:
intent = new Intent(this, Class2.class);
startActivity(intent);
break;
case R.id.item3:
intent = new Intent(this, Class3.class);
startActivity(intent);
break;
default:
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
并且,header.xml文件的布局是
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<RelativeLayout 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:weightSum="1"
android:padding="3dp"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
android:minHeight="?attr/actionBarSize"
android:background="@android:color/transparent"/>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_below="@id/toolbar"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame"
android:background="@drawable/jackson">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="italic"
android:padding="30dp"
android:layout_marginLeft="50dp"
android:textSize="15dp"
android:layout_marginTop="40dp"
android:textColor="@color/red"
android:text="Just Beat It"/>
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/drawer"
app:itemIconTint="@color/colorAccent"
app:headerLayout="@layout/header"
app:menu="@menu/drawer">
</android.support.design.widget.NavigationView>
并且,menu / drawer.xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="@drawable/header"
android:orientation="vertical">
<ImageView
android:layout_width="76dp"
android:layout_height="76dp"
android:id="@+id/profile_image"
android:layout_marginLeft="54dp"
android:layout_marginTop="50dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/myphoto_cutmypic"/>
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/profile_image"
android:layout_marginLeft="54dp"
android:textStyle="bold"
android:paddingTop="10dp"
android:textColor="@color/red"
android:text="@string/my_name"/>
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt1"
android:textStyle="bold"
android:paddingTop="10dp"
android:layout_marginLeft="54dp"
android:textColor="@color/red"
android:text="@string/my_email"/>
</RelativeLayout>
如果您有任何疑问,请回复此答案