2016-12-29 197 views
1

ScreenShot如何在點擊項目菜單時關閉抽屜菜單?

這是click事件。

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

檢查了這一點: http://stackoverflow.com/questions/25534806/navigation- drawer-lag-on-android –

回答

2

添加此您onTouchListener內每次觸及任何項目的時間將關閉抽屜:

drawer.closeDrawer(GravityCompat.START); 
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; 
     } 
    }); 
+0

https://github.com/bmskyline/DATN/blob/master/app/src/main/java/com/tienmanh/datn/Adapter/ExpandAdapter.java –

+0

我無法調用findViewById –