可扩展导航抽屉不适用于子项目

时间:2017-03-15 09:02:52

标签: android navigation expandablelistview

我为导航抽屉创建了可扩展列表视图。它正在工作,但当我从中选择任何项目然后它将关闭抽屉,但当它有一些子项目时,它也将关闭抽屉。

即。我有这样的抽屉

**Home**

**payment**

**delivery**

**order --> pending order --> complete order**

**exit**

当我选择Home然后它工作正常但是当我点击订单然后它将关闭抽屉..

我想要当我点击订单然后抽屉保持打开状态,当我选择子项目时它将关闭。

这里的完整代码如下: -

NavigationDrawerFragment.java

    package omcommunication.omprivillege;

/**
 * Created by sachin on 3/9/2017.
 */

        import android.content.Intent;
        import android.support.v4.app.FragmentTransaction;
        import android.support.v7.app.ActionBarActivity;
        import android.app.Activity;
        import android.support.v7.app.ActionBar;
        import android.support.v4.app.Fragment;
        import android.support.v7.app.ActionBarDrawerToggle;
        import android.support.v4.view.GravityCompat;
        import android.support.v4.widget.DrawerLayout;
        import android.content.res.Configuration;
        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.Menu;
        import android.view.MenuInflater;
        import android.view.MenuItem;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.ExpandableListView;


        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;

        import android.widget.ExpandableListView.OnGroupCollapseListener;
        import android.widget.ExpandableListView.OnGroupExpandListener;

  public class NavigationDrawerFragment extends Fragment {

    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

    private NavigationDrawerCallbacks mCallbacks;

    private ActionBarDrawerToggle mDrawerToggle;

    private DrawerLayout mDrawerLayout;
    private ExpandableListView mDrawerListView;
    private View mFragmentContainerView;

    private int mCurrentSelectedPosition = 0;


    ArrayList<String> groupItem = new ArrayList<String>();
    ArrayList<Object> childItem = new ArrayList<Object>();

    List<String> expandableListTitle;
    HashMap<String, List<String>> expandableListDetail;

    public NavigationDrawerFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Indicate that this fragment would like to influence the set of actions in the action bar.
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mDrawerListView = (ExpandableListView) inflater.inflate(
                R.layout.drawer_drawer, container, false);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });

        expandableListDetail = ExpandableListDataPump.getData();
        expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());


        mDrawerListView.setAdapter(new ExpandableListAdapter(getActivity(), expandableListTitle, expandableListDetail));

        mDrawerListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {

            }
        });


        mDrawerListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                boolean retVal = true;

                if (groupPosition == ExpandableListAdapter.ITEM1) {

                    FragmentTransaction t = getFragmentManager().beginTransaction();
                    Homepage mFrag = new Homepage ();
                    t.replace(R.id.container, mFrag);
                    t.commit();
                } else if (groupPosition == ExpandableListAdapter.ITEM2) {
                    FragmentTransaction t = getFragmentManager().beginTransaction();
                    Rorder mFrag = new Rorder();
                    t.replace(R.id.container, mFrag);
                    t.commit();
                } else if (groupPosition == ExpandableListAdapter.ITEM3) {
                    FragmentTransaction t = getFragmentManager().beginTransaction();
                    Dispatchorder mFrag = new Dispatchorder();
                    t.replace(R.id.container, mFrag);
                    t.commit();
                    // retVal = false;
                    // call some activity here
                } else if (groupPosition == ExpandableListAdapter.ITEM4) {
                    // call some activity here
                    retVal = false;
                }else if (groupPosition == ExpandableListAdapter.ITEM5) {
                    // call some activity here
                    //retVal = false;
                    FragmentTransaction t = getFragmentManager().beginTransaction();
                    RecordDeliver mFrag = new RecordDeliver();

                    t.replace(R.id.container, mFrag);
                    t.commit();

                }else if (groupPosition == ExpandableListAdapter.ITEM6) {
                    // call some activity here
                    //retVal = false;
                    FragmentTransaction t = getFragmentManager().beginTransaction();
                    OrderDelivery mFrag = new OrderDelivery();
                    t.replace(R.id.container, mFrag);
                    t.commit();

                }else if (groupPosition == ExpandableListAdapter.ITEM7) {
                    // call some activity here
                    //retVal = false;

                }else if (groupPosition == ExpandableListAdapter.ITEM8) {
                    // call some activity here
                    //retVal = false;

                }else if (groupPosition == ExpandableListAdapter.ITEM9) {
                // call some activity here
                //retVal = false;

            }

                    return retVal;
            }
        });

        mDrawerListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {

                if (groupPosition == ExpandableListAdapter.ITEM1) {
                    if (childPosition == ExpandableListAdapter.SUBITEM1_1) {

                            Intent i = new Intent(getContext(), MainActivity.class);
                            startActivity(i);


                    }
                    else if (childPosition == ExpandableListAdapter.SUBITEM1_2) {

                        // call activity here

                    }



                } else if (groupPosition == ExpandableListAdapter.ITEM2) {

                    if (childPosition == ExpandableListAdapter.SUBITEM2_1) {

                        // call activity here

                    }
                    else if (childPosition == ExpandableListAdapter.SUBITEM2_2) {

                        // call activity here

                    }
                    else if (childPosition == ExpandableListAdapter.SUBITEM2_3) {

                        // call activity here

                    }
                    else if (childPosition == ExpandableListAdapter.SUBITEM2_4) {

                        // call activity here

                    }


                }
                return true;
            }
        });

        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);

        View header= inflater.inflate(R.layout.drawer_header, null);
        mDrawerListView.addHeaderView(header);


        return mDrawerListView;
    }



    public boolean isDrawerOpen() {
        return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
    }

    /**
     * Users of this fragment must call this method to set up the navigation drawer interactions.
     *
     * @param fragmentId   The android:id of this fragment in its activity's layout.
     * @param drawerLayout The DrawerLayout containing this fragment's UI.
     */
    public void setUp(int fragmentId, DrawerLayout drawerLayout) {
        mFragmentContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.header, GravityCompat.START);
        // set up the drawer's list view with items and click listener

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the navigation drawer and the action bar app icon.
        mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActionBar().setIcon(R.drawable.header);

                if (!isAdded()) {
                    return;
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);


                if (!isAdded()) {
                    return;
                }



                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }
        };


        // Defer code dependent on restoration of previous instance state.
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    private void selectItem(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallbacks = (NavigationDrawerCallbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Forward the new configuration the drawer toggle component.
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // If the drawer is open, show the global app actions in the action bar. See also
        // showGlobalContextActionBar, which controls the top-left area of the action bar.
     /*  if (mDrawerLayout != null && isDrawerOpen()) {
            inflater.inflate(R.menu.menu_main, menu);
            showGlobalContextActionBar();
        }
     */   super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }


        return super.onOptionsItemSelected(item);
    }

    /**
     * Per the navigation drawer design guidelines, updates the action bar to show the global app
     * 'context', rather than just what's in the current screen.
     */
    private void showGlobalContextActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

    }

    private ActionBar getActionBar() {
        return ((ActionBarActivity) getActivity()).getSupportActionBar();
    }

    /**
     * Callbacks interface that all activities using this fragment must implement.
     */
    public static interface NavigationDrawerCallbacks {
        /**
         * Called when an item in the navigation drawer is selected.
         */
        void onNavigationDrawerItemSelected(int position);

    }
}

我参考了许多教程,但没有找到我的问题..请帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

drawerLayout.closeDrawer(mFragmentContainerView);
 return retVal;

关闭听众内部的抽屉。

答案 1 :(得分:0)

mDrawerLayout.closeDrawer(mDrawerListView);

在您的组和子选择方法的末尾设置此行代码。