2017-03-15 20 views
0

ナビゲーションドロワーの拡張可能リストビューを作成しました。それは働いているが、私はそれからそれからいくつかのサブアイテムを持っているときに引き出しを閉じますが、それはまた、引き出しを閉じます。拡張可能なナビゲーションドロワーがサブアイテムで機能しない

私は持っている。すなわち、この

**Home** 

**payment** 

**delivery** 

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

**exit** 

のような引き出し、私はそれが正常に動作しているホームを選択するが、私は順番にクリックしたとき、それは引き出しを閉じます..私は私がクリックしたときに好きたい

注文時に引き出しが開いたままになり、サブアイテムを選択すると閉じます。

ここに完全なコードは以下の通りである: -

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

    } 
} 

私はそれのための多くのチュートリアルを参照してくださいが、私の問題を見つけることができません。これで私を助けてください。

答えて

0

mDrawerLayout.closeDrawer(mDrawerListView);

このコード行は、グループと子の選択方法の最後に設定してください。

+0

アイテムで作業していますが、サブアイテムはどうですか? – Andie

0
drawerLayout.closeDrawer(mFragmentContainerView); 
return retVal; 

リスナー内の引き出しを閉じます。

+0

それは私が拡張可能な項目をクリックしたとき、それは引き出し – Andie

+0

を閉じます動作していないが、サブアイテムのclixkリスナーにこのラインを含め、あまりにも –

+0

onchildクリックリスナーのあなたlistview.Itからクリックonitem削除 –

関連する問題