我最近设法对导航抽屉进行了编辑和响应点击,但在转换到另一个活动后,会显示导航抽屉,但不会对任何进一步的点击做出响应。 返回的唯一方法是在“后退”按钮中按下android buikt
谢谢
答案 0 :(得分:0)
在您的活动布局中,您应该将NavigationView添加到drawerLayout,如下所示:
<android.support.v4.widget.DrawerLayout>
...
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:itemIconTint="#333"
app:itemTextColor="#333">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="fragments.MenuFragment"
android:id="@+id/fragment"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
在您的MenuFragment布局中,您可以这样:
<?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="match_parent">
<include
android:id="@+id/header"
layout="@layout/nav_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/menuRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/header"/>
</RelativeLayout>
在片段的课程中,你可以这样做:
public class MenuFragment extends Fragment {
RecyclerView recyclerView;
ArrayList<MenuItem> menuItems = new ArrayList<>();
private OnFragmentInteractionListener mListener;
public MenuFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.menu_fragment, container, false);
recyclerView = root.findViewById(R.id.menuRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(...);//add the code to handle menu click...
return root;
}
nav_header.xml实际上是导航视图的标题,并没有什么特别之处:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout2"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:padding="10dp">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:paddingBottom="20dp"
android:src="@drawable/ic_white"/>
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/logo">
<ImageView
android:id="@+id/account"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="2dp"
android:src="@drawable/ic_account_box_white_48dp"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_toLeftOf="@+id/account"
android:ellipsize="marquee"
android:singleLine="true"
android:text="UserName"
android:textColor="#ffffff"/>
</RelativeLayout>
</RelativeLayout>
menuRecyclerView只是一个recyclelerView,它负责显示菜单项,你必须将项目添加到那里(你可以看到this或this来开始使用recyclerView)
添加项目后,您需要将OnItemClickListener添加到您可以学习的回收站视图here
完成所有这些后,您可以开始另类活动:
Intent newActivity = new Intent(getActivity(), newActivity.class);
startActivity(newActivity);