Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

android - How to close navigation drawer from an adapter class

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 is working perfectly but the navigation drawer is not closing...

This is my adapter class...

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;

        }

    }

This is my mainActivity

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

This is my 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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);
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...