2017-08-25 76 views
-1

我有一個自定義導航抽屜,其中包含gridview中的按鈕...我需要關閉適配器類中的導航抽屜...或者是任何其他關閉抽屜時的方式我點擊了gridview中的一個按鈕。如何關閉適配器類中的導航抽屜

的onclick工作完美,但抽屜式導航欄不打烊......

這是我的適配器類...

public class NavMenuGridViewAdapter extends ArrayAdapter<MenuGridItem>{ 

     ArrayList<MenuGridItem> menuList = new ArrayList<>(); 
     Context context; 
     View activityHome; 

     public NavMenuGridViewAdapter(Context context, int textViewResourceId, ArrayList<MenuGridItem> objects) { 
      super(context, textViewResourceId, objects); 
      menuList = objects; 
      this.context=context; 
     } 

     @Override 
     public int getCount() { 
      return super.getCount(); 
     } 

     @Override 
     public View getView(int position, final View convertView, ViewGroup parent) { 

      View v = convertView; 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.nav_menu_view_item, null); 
      activityHome = inflater.inflate(R.layout.activity_home, null); 
      AppCompatButton appCompatButton = (AppCompatButton) v.findViewById(R.id.nav_menu_button); 
      appCompatButton.setBackgroundResource(menuList.get(position).getMenuImage()); 
      appCompatButton.setPadding(0,230,0,0); 
      appCompatButton.setText(menuList.get(position).getMenuName()); 
      appCompatButton.setTag(Integer.valueOf(position)); 
      appCompatButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Integer position = (Integer)v.getTag(); 
        Fragment fragment=null; 
        FragmentTransaction ft=null; 
        Intent intent; 
        switch (position){ 
         case 0: 
          fragment = new DashboardFragment(); 
          ft = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction(); 
          ft.replace(R.id.content_frame, fragment); 
          ft.commit(); 
          break; 
         case 1: 
          fragment = new MyGiftCardFragment(); 
          ft = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction(); 
          ft.replace(R.id.content_frame, fragment); 
          ft.commit(); 

          break; 
         case 2: 
          break; 
         case 3: 
          break; 
         case 4: 
          break; 
         case 5: 
          break; 
        } 
        DrawerLayout drawer = activityHome.findViewById(R.id.drawer_layout); 
        Log.i("GiftCard", "Menu: " + drawer); 
        //drawer.closeDrawer(Gravity.LEFT); 
        drawer.closeDrawer(GravityCompat.START); 
       }}); 
      return v; 

     } 

    } 

這是我mainActivity

NavMenuGridViewAdapter navMenuGridViewAdapter=new NavMenuGridViewAdapter(this,R.layout.nav_menu_view_item,menuList); 
     navbarMenuGridView.setAdapter(navMenuGridViewAdapter); 

這是我nav_menu_view_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical"> 

     <android.support.v7.widget.AppCompatButton 
      android:id="@+id/nav_menu_button" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:adjustViewBounds="true" 
      android:textColor="#FFFF" 
      android:scaleType="fitCenter" 
      /> 
    </RelativeLayout> 
</LinearLayout> 
+0

投上下文活動並關閉它喜歡: ((HomeActivity)mContext).mBinding.drawerLayout.closeDrawers(); – Killer

+0

對不起,我沒有得到你....我是新手到Android ...你可以解釋 –

回答

0

你可以監聽器傳遞給構造函數。 這個監聽器有一個方法closeDrawer(),你可以在你的適配器的onClick()方法中調用它。

我想你在你的活動中創建這個適配器?在這種情況下,您的活動可以通過調用findViewById()來查找抽屜。

 new ANavMenuGridViewAdapter(this, textViewResourceId, objects, new OnDrawerCloseListener() { 
     public void closeDrawer() { 
      ((DrawerLayout) findViewById(R.id.drawer_layout)).closeDrawer(GravityCompat.START); 
     } 
    }); 
+0

保存我的一天:)謝謝你這麼多先生.... –