如何从适配器类关闭导航抽屉 [英] How to close navigation drawer from an adapter class

查看:63
本文介绍了如何从适配器类关闭导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义导航抽屉,其中包含在gridview中的按钮...我需要从适配器类关闭导航抽屉...或者当我单击gridview中的按钮时,也可以使用其他任何方式来关闭抽屉.

I have a custom navigation drawer which contains buttons in gridview...I need to close the navigation drawer from the adapter class...OR is any other ways to close the drawer when i click a button in the gridview.

Onclick运行正常,但是导航抽屉没有关闭...

Onclick is working perfectly but the navigation drawer is not closing...

这是我的适配器类...

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>

推荐答案

您可以将侦听器传递给构造函数.该侦听器具有closeDrawer()方法,您可以在适配器的onClick()方法中调用它.

You could pass a listener to the constructor. This listener has a method closeDrawer() which you can call in your onClick() method in your adapter.

我猜您是在活动中创建此适配器的吗?在这种情况下,您的活动可以通过调用findViewById()找到抽屉.

I guess you create this adapter in your activity? In this case your activity can find your drawer by calling findViewById().

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

这篇关于如何从适配器类关闭导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆