这是点击事件。
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setBackgroundResource(R.color.colorTim);
FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HienThiSanPhamTheoDanhMucActivity hienThiSanPhamTheoDanhMucActivity = new HienThiSanPhamTheoDanhMucActivity();
Bundle bundle = new Bundle();
bundle.putInt("MALOAI",loaiSanPhams.get(groupPosition).getMALOAISP());
bundle.putBoolean("KIEMTRA",false);
bundle.putString("TENLOAI",loaiSanPhams.get(groupPosition).getTENLOAISP());
hienThiSanPhamTheoDanhMucActivity.setArguments(bundle);
fragmentTransaction.addToBackStack("TrangChuActivity");
fragmentTransaction.replace(R.id.themFragment,hienThiSanPhamTheoDanhMucActivity);
fragmentTransaction.commit();
return false;
}
});
这是xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout">
<FrameLayout
android:id="@+id/themFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:id="@+id/appbar"
android:layout_width="match_parent"
android:background="@color/bgToolbar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/chumenu"
app:logo="@mipmap/logo"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin"/>
<LinearLayout
android:id="@+id/lnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/bgsearch"
android:layout_marginTop="55dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed|snap"
app:layout_collapseMode="parallax">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/timkiem"
android:text="@string/timkiem"
android:layout_weight="1"
android:textAllCaps="false"
android:background="@drawable/bgsearchselected"
android:textColor="@color/colorTim"
android:gravity="center_vertical"
android:paddingLeft="20dp"/>
<View
android:layout_width="1dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_height="match_parent"
android:background="@color/bgToolbar"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/searchicon"
android:padding="10dp"
android:background="@drawable/bgimagesearch"
android:src="@drawable/ic_search_black_24dp"/>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="@color/colorGray"
app:tabTextAppearance="@style/chuthuong"
app:tabMode="scrollable">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
<ExpandableListView
android:id="@+id/epMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorWhite"
android:choiceMode="singleChoice"
android:groupIndicator="@null">
</ExpandableListView>
</android.support.v4.widget.DrawerLayout>
结果将显示在片段中,但仍会显示抽屉菜单。我该怎么做才能关闭它?任何人都给我建议如何解决这个,任何帮助非常感谢。谢谢。
答案 0 :(得分:3)
每次触摸任何项目时,在onTouchListener
内添加此内容都会关闭抽屉:
drawer.closeDrawer(GravityCompat.START);
答案 1 :(得分:0)
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setBackgroundResource(R.color.colorTim);
FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HienThiSanPhamTheoDanhMucActivity hienThiSanPhamTheoDanhMucActivity = new HienThiSanPhamTheoDanhMucActivity();
Bundle bundle = new Bundle();
bundle.putInt("MALOAI",loaiSanPhams.get(groupPosition).getMALOAISP());
bundle.putBoolean("KIEMTRA",false);
bundle.putString("TENLOAI",loaiSanPhams.get(groupPosition).getTENLOAISP());
hienThiSanPhamTheoDanhMucActivity.setArguments(bundle);
fragmentTransaction.addToBackStack("TrangChuActivity");
fragmentTransaction.replace(R.id.themFragment,hienThiSanPhamTheoDanhMucActivity);
fragmentTransaction.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return false;
}
});
答案 2 :(得分:0)
将以下代码添加到MainActivity.java的onCreate()中:
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(mOnNavigationDrawerItemSelectedListener);
然后添加以下代码:
private NavigationView.OnNavigationItemSelectedListener mOnNavigationDrawerItemSelectedListener
= new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment fragment;
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_reload_downloaded_tasks:
toolbarTitle.setText(R.string.menu_reload_downloaded_tasks);
fragment = new ReloadDownloadedTasksFragment();
loadFragment(fragment);
break;
case R.id.nav_download_base_data:
toolbarTitle.setText(R.string.menu_download_base_data);
fragment = new ReloadDownloadedTasksFragment();
loadFragment(fragment);
break;
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
};
这:
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_host_fragment, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
在此处输入代码