2016-03-07 52 views
1
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); 

我曾經有這行代碼禁用滑動手勢,但頂部切換按鈕打開抽屜。我發現支持庫的最新更新後,它不再正常工作。我無法通過切換按鈕或滑動手勢打開抽屜。我需要取消註釋以打開抽屜。無法使用按鈕在最近更新後使用LOCK_MODE_LOCKED_CLOSED打開抽屜

有誰知道需要改變什麼?

我也將setDrawerListener更改爲addDrawerListener,因爲前者折舊。

public abstract class BaseActivity extends AppCompatActivity { 

    protected DrawerLayout mDrawer; 

    ActionBarDrawerToggle actionBarDrawerToggle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_base); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

     mDrawer = (DrawerLayout) findViewById(R.id.navigation_drawer); 

     setSupportActionBar(toolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     getSupportActionBar().setHomeButtonEnabled(true); 

     actionBarDrawerToggle = new ActionBarDrawerToggle(this,mDrawer,toolbar,0,0); 

     mDrawer.addDrawerListener(actionBarDrawerToggle); 

     // Disable drawer's swiping function 
     mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); 

     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View contentView = inflater.inflate(getLayoutResource(), null, false); 

     FrameLayout mainContent = (FrameLayout) findViewById(R.id.main_content); 

     mainContent.addView(contentView, 0); 

     inflateDrawerItem(); 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 

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

     return super.onOptionsItemSelected(item); 
    } 



} 

回答

1

可以設置監聽工具欄和點擊切換按鈕時。這裏有一個參考。

import android.os.Bundle; 
    import android.app.Activity; 
    import android.content.res.Configuration; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.AdapterView.OnItemClickListener; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.support.v4.app.ActionBarDrawerToggle; 
    import android.support.v4.view.GravityCompat; 
    import android.support.v4.widget.DrawerLayout; 

    public class HelloDrawerActivity extends Activity 
    { 

     private String[] mPlanetTitles; 
     private DrawerLayout mDrawerLayout; 
     private ActionBarDrawerToggle mDrawerToggle; 
     private ListView mDrawerList; 

     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_hello_drawer); 

      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

      // init the ListView and Adapter, nothing new 
      initListView(); 

      // set a custom shadow that overlays the main content when the drawer 
      // opens 
      mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, 
        GravityCompat.START); 

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

       /** Called when a drawer has settled in a completely closed state. */ 
       public void onDrawerClosed(View view) 
       { 

        invalidateOptionsMenu(); // creates call to 
               // onPrepareOptionsMenu() 
       } 

       /** Called when a drawer has settled in a completely open state. */ 
       public void onDrawerOpened(View drawerView) 
       { 

        invalidateOptionsMenu(); // creates call to 
               // onPrepareOptionsMenu() 
       } 
      }; 

      // Set the drawer toggle as the DrawerListener 
      mDrawerLayout.setDrawerListener(mDrawerToggle); 

      // enable ActionBar app icon to behave as action to toggle nav drawer 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
      // getActionBar().setHomeButtonEnabled(true); 
      // Note: getActionBar() Added in API level 11 
     } 

     private void initListView() 
     { 
      mDrawerList = (ListView) findViewById(R.id.left_drawer); 

      mPlanetTitles = getResources().getStringArray(R.array.planets_array); 

      // Set the adapter for the list view 
      mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
        R.layout.list_item, mPlanetTitles)); 
      // Set the list's click listener 
      mDrawerList.setOnItemClickListener(new OnItemClickListener() 
      { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) 
       { 
        // Highlight the selected item, update the title, and close the 
        // drawer 
        mDrawerList.setItemChecked(position, true); 
        setTitle(mPlanetTitles[position]); 
        mDrawerLayout.closeDrawer(mDrawerList); 
       } 
      }); 
     } 

     @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 onOptionsItemSelected(MenuItem item) 
     { 
      // Pass the event to ActionBarDrawerToggle, if it returns 
      // true, then it has handled the app icon touch event 
      if (mDrawerToggle.onOptionsItemSelected(item)) 
      { 
       return true; 
      } 
      // Handle your other action bar items... 

      return super.onOptionsItemSelected(item); 
     } 

    } 

public class DrawerActivity extends Activity 
{ 
    private DrawerLayout mDrawerLayout = null; 

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

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

     Button button = (Button) findViewById(R.id.btn); 
     button.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) 
      { 
       // when button be presses,drawer open 
       mDrawerLayout.openDrawer(Gravity.LEFT); 

      } 
     }); 
    } 

} 
0

使用mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

相關問題