2015-09-12 144 views
0

我想停止后退按钮以关闭导航抽屉。如果有任何解决方案,请给我答案,如果你想。如何在导航抽屉打开时停止切换

我的代码是在这里,我已经使用了可扩展列表视图,其中首先会有没有孩子,但是当他点击任何项目时,我只是改变setAdapter的数组,并给他们的孩子。所以你能告诉我做什么改变像myntra左菜单实现。

import android.app.Activity; 
import android.content.res.Configuration; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ExpandableListView; 

import com.teamtreehouse.oslist.Adapter.ExpandableListAdapter; 

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


public class MainActivity extends Activity { 

    private DrawerLayout mDrawerLayout; 

    private ActionBarDrawerToggle mDrawerToggle; 

    private String mActivityTitle; 
    ExpandableListAdapter listAdapter; 
    ExpandableListView expListView; 
    List<String> listDataHeader; 
    List<String> listDataHeader1; 
    private ArrayAdapter<String> mAdapter; 
    HashMap<String, List<String>> listDataChild; 
    HashMap<String, List<String>> listDataChild1; 
    public static boolean isBack = true; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     expListView = (ExpandableListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); 
     expListView.setBackgroundColor(Color.WHITE); 
     expListView.setGroupIndicator(null); 
     mActivityTitle = getTitle().toString(); 
     prepareListData(); 
     prepareListData1(); 

     setupDrawer(); 
     addDrawerItems(); 


     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 
    } 

    private void addDrawerItems() { 

     listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 

     // setting list adapter 
     expListView.setAdapter(listAdapter); 

     Log.d("tag", "here i am in addDrawerItems"); 

     // Listview Group click listener 
     expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 

      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, 
             int groupPosition, long id) { 
       // Toast.makeText(getApplicationContext(), 
       // "Group Clicked " + listDataHeader.get(groupPosition), 
       // Toast.LENGTH_SHORT).show(); 
       isBack = false; 


       listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader1, listDataChild1); 

       // setting list adapter 
       expListView.setAdapter(listAdapter); 

       Log.d("tag", "here i am in setOngroupclick listitener"); 

       return true; 
      } 
     }); 





    } 

    private void setupDrawer() { 

     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) { 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getActionBar().setTitle("Navigation!"); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 

      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       getActionBar().setTitle(mActivityTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
     }; 

     mDrawerToggle.setDrawerIndicatorEnabled(true); 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 
    } 



    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 

     Log.d("tag","the keydown button"); 
     if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR 
       && keyCode == KeyEvent.KEYCODE_BACK 
       && event.getRepeatCount() == 0) { 
      // Take care of calling this method on earlier versions of 
      // the platform where it doesn't exist. 
      onBackPressed(); 
     } 

     return super.onKeyDown(keyCode, event); 
    } 

    @Override 
    public void onBackPressed() { 
     if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) { 
      Log.d("tag","the back button is pressed"); 
      return; 
     } else { 
      Log.d("tag","the back button is pressed2"); 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     // Activate the navigation drawer toggle 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
       return true; 

     } 

     return super.onOptionsItemSelected(item); 
    } 

    private void prepareListData() { 
     listDataHeader = new ArrayList<String>(); 
     listDataChild = new HashMap<String, List<String>>(); 

     // Adding child data 
     listDataHeader.add("Top 250"); 
     listDataHeader.add("Now Showing"); 
     listDataHeader.add("Coming Soon.."); 

     // Adding child data 
     List<String> top250 = new ArrayList<String>(); 
     top250.add("The Shawshan"); 
     top250.add("The Godfather"); 
     top250.add("The Godfathe"); 
     top250.add("Pulp Fiction"); 
     top250.add("The Good, t"); 
     top250.add("The Dark "); 
     top250.add("12 Angry"); 

     List<String> nowShowing = new ArrayList<String>(); 
     nowShowing.add("The Conjuring"); 
     nowShowing.add("Despicable"); 
     nowShowing.add("Turbo"); 
     nowShowing.add("Grown U"); 
     nowShowing.add("Red 2"); 
     nowShowing.add("The Wolveri"); 

     List<String> comingSoon = new ArrayList<String>(); 
     comingSoon.add("2 Guns"); 
     comingSoon.add("The Smurfs 2"); 
     comingSoon.add("The Spectacu"); 
     comingSoon.add("The Canyons"); 
     comingSoon.add("Europa Report"); 

     listDataChild.put(listDataHeader.get(0), top250); // Header, Child data 
     listDataChild.put(listDataHeader.get(1), nowShowing); 
     listDataChild.put(listDataHeader.get(2), comingSoon); 
    } 

    private void prepareListData1() { 
     listDataHeader1 = new ArrayList<String>(); 
     listDataChild1 = new HashMap<String, List<String>>(); 

     // Adding child data 
     listDataHeader1.add("Second List"); 
     listDataHeader1.add("New List"); 
     listDataHeader1.add("Old List"); 

     // Adding child data 
     List<String> top250 = new ArrayList<String>(); 
     top250.add("The Shawshan"); 
     top250.add("The Godfather"); 
     top250.add("The Godfathe"); 
     top250.add("Pulp Fiction"); 
     top250.add("The Good, t"); 
     top250.add("The Dark "); 
     top250.add("12 Angry"); 

     List<String> nowShowing = new ArrayList<String>(); 
     nowShowing.add("The Conjuring"); 
     nowShowing.add("Despicable"); 


     List<String> comingSoon = new ArrayList<String>(); 
     comingSoon.add("2 Guns"); 
     comingSoon.add("The Smurfs 2"); 
     comingSoon.add("The Spectacu"); 


     listDataChild1.put(listDataHeader1.get(0), top250); // Header, Child data 
     listDataChild1.put(listDataHeader1.get(1), nowShowing); 
     listDataChild1.put(listDataHeader1.get(2), comingSoon); 
    } 
} 
+0

当您按下后退按钮时是否打印任何日志?你可以删除'onKeyDown'方法并重试吗?什么是您的应用程序的minSdkVersion? – ataulm

回答

1

是的,你可以在你的活动覆盖onBackPressed(),检查是否打开抽屉,和吞咽时,如果左右。

@Override 
public void onBackPressed() { 
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) { 
     return; 
    } else { 
     super.onBackPressed(); 
    } 
} 

在标准应用程序中,使用后退按钮关闭抽屉是正常行为;如果您的情况为需要,请仅应用此选项。

+0

感谢您的建议,但是这种方法不需要点击后退按钮。其实我想要这个,因为我想通过点击其中的任何项目来更改导航抽屉的项目。但点击后退按钮时,前一个项目会被加载,因此不能关闭抽屉,这就是为什么我需要这个。其实我想让菜单像myntra app –

+0

有什么方法可以阻止抽屉关闭。 –

+0

我刚刚下载了myntra应用程序,并且后退按钮确实会关闭导航抽屉(如果已打开)。也许你正在测试的设备是非标准的。 – ataulm