DrawLayout的实现官方文档上说的很详细了,但是在开发的过程中,发现还是有点问题,官方的文档有点旧了,mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close); 这个方法原来的api可以设置app的icon图片,现在这个参数没有了。
对于ActionBarDrawerToggle 这个类,作用时使得actionbar和drawlayout之间可以很好的相互组合。通过用
ActionBarDrawerToggle可以实现app icon点击来打开抽屉的功能呢。
主要需要实现:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
-
@Override
-
public boolean onOptionsItemSelected(MenuItem item) {
-
// The action bar home/up action should open or close the drawer.
-
// ActionBarDrawerToggle will take care of this.
-
Log.v("haha",""+item.getItemId());
-
if (mDrawerToggle.onOptionsItemSelected(item)) {
-
return true;
-
}
-
// Handle action buttons
-
switch(item.getItemId()) {
-
case R.id.action_settings:
-
-
return true;
-
default:
-
return super.onOptionsItemSelected(item);
-
}
-
}
已经在主activity中重载这个方法来是的对于actionbar的item的操作可以传给ActionBarDrawerToggle来处理,其实就我现在知道的就是处理actionbar的appicon的点击动作。
-
@Override
-
protected void onPostCreate(Bundle savedInstanceState) {
-
super.onPostCreate(savedInstanceState);
-
mDrawerToggle.syncState();
-
}
这个方法的重载调用的syncState就是为了保持actionBar和ActionBarDrawerToggle的状态一致,这个函数是Activity的生命周期里的一个方法,只会调用一次。
主要的代码:
-
package com.example.dolby.drawernavi;
-
-
import android.app.ActionBar;
-
import android.app.Fragment;
-
import android.app.FragmentManager;
-
import android.content.res.Configuration;
-
import android.support.v4.widget.DrawerLayout;
-
import android.support.v7.app.ActionBarActivity;
-
import android.os.Bundle;
-
import android.support.v7.app.ActionBarDrawerToggle;
-
import android.util.Log;
-
import android.view.Menu;
-
import android.view.MenuInflater;
-
import android.view.MenuItem;
-
import android.view.View;
-
import android.widget.AdapterView;
-
import android.widget.ArrayAdapter;
-
import android.widget.Button;
-
import android.widget.ListView;
-
-
-
public class MainActivity extends ActionBarActivity {
-
private String[] mPlanetTitles = {"list1","list2","list3"};
-
private DrawerLayout mDrawerLayout;
-
private ListView mDrawerList;
-
private Button drawbtn;
-
private ActionBarDrawerToggle mDrawerToggle;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
//mPlanetTitles = getResources().getStringArray(R.array.planets_array);
-
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
-
mDrawerList = (ListView) findViewById(R.id.left_drawer);
-
mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
-
// Set the drawer toggle as the DrawerListener
-
mDrawerLayout.setDrawerListener(mDrawerToggle);
-
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
-
getSupportActionBar().setHomeButtonEnabled(true);
-
-
// enable ActionBar app icon to behave as action to toggle nav drawer
-
//getActionBar().setDisplayHomeAsUpEnabled(true);
-
// Set the adapter for the list view
-
mDrawerList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1
-
, mPlanetTitles));
-
// Set the list's click listener
-
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
-
}
-
-
private class DrawerItemClickListener implements ListView.OnItemClickListener
-
{
-
-
@Override
-
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-
selectItem(position);
-
}
-
}
-
-
private void selectItem(int position)
-
{
-
Fragment fragment = new PlanetFragment();
-
-
// Insert the fragment by replacing any existing fragment
-
FragmentManager fragmentManager = getFragmentManager();
-
fragmentManager.beginTransaction()
-
.replace(R.id.content_frame, fragment)
-
.commit();
-
-
// 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);
-
mDrawerToggle.syncState();
-
}
-
-
// @Override
-
// public void onConfigurationChanged(Configuration newConfig) {
-
// super.onConfigurationChanged(newConfig);
-
// mDrawerToggle.onConfigurationChanged(newConfig);
-
// }
-
-
// @Override
-
// public boolean onCreateOptionsMenu(Menu menu) {
-
// MenuInflater inflater = getMenuInflater();
-
// inflater.inflate(R.menu.menu_main,menu);
-
// return super.onCreateOptionsMenu(menu);
-
// }
-
-
// @Override
-
// public boolean onPrepareOptionsMenu(Menu menu) {
-
//// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
-
//// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
-
//// return super.onPrepareOptionsMenu(menu);
-
// return true;
-
// }
-
-
@Override
-
public boolean onOptionsItemSelected(MenuItem item) {
-
// The action bar home/up action should open or close the drawer.
-
// ActionBarDrawerToggle will take care of this.
-
Log.v("haha",""+item.getItemId());
-
if (mDrawerToggle.onOptionsItemSelected(item)) {
-
return true;
-
}
-
// Handle action buttons
-
switch(item.getItemId()) {
-
case R.id.action_settings:
-
-
return true;
-
default:
-
return super.onOptionsItemSelected(item);
-
}
-
}
-
-
-
}
-
<android.support.v4.widget.DrawerLayout
-
xmlns:android=""
-
android:id="@+id/drawer_layout"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent">
-
<!-- The main content view -->
-
<FrameLayout
-
android:id="@+id/content_frame"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent">
-
<!--<Button-->
-
<!--android:id="@+id/btn"-->
-
<!--android:layout_width="match_parent"-->
-
<!--android:layout_height="wrap_content"-->
-
<!--android:text="open"-->
-
<!--/>-->
-
</FrameLayout>
-
-
<!-- The navigation drawer -->
-
<ListView android:id="@+id/left_drawer"
-
android:layout_width="240dp"
-
android:layout_height="match_parent"
-
android:layout_gravity="start"
-
android:choiceMode="singleChoice"
-
android:divider="@android:color/transparent"
-
android:dividerHeight="0dp"
-
android:background="#FFFFFF"/>
-
</android.support.v4.widget.DrawerLayout>
阅读(7782) | 评论(0) | 转发(0) |