Android中的DrawerLayout
简介
Drawerlayout是google自带的控件,功能类似开源的SlidingMenu,在support-v4包下用来替代SlidingMenu(google好无耻啊)。
使用方法
在activity_main.xml中定义:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/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"> </FrameLayout> <!-- The Navigation view --> <ListView android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#ffffcc" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" ></ListView> </android.support.v4.widget.DrawerLayout>
注意事项:
- drawerlayout的主视图必须要是DrawerLayout的第一个子视图。
- 主视图宽度和高度匹配父视图,即match_parent,抽屉隐藏的时候,必须让主视图占满屏幕。
- 必须指定抽屉的android:layout_gravity属性
- 抽屉视图的宽度以dp为单位,最好不超过300dp
在drawerLayout中添加子项:
private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ArrayList<String> menuLists; private ArrayAdapter<String> mAdapter;
for (int i = 0; i < 5; i++){ menuLists.add("Hello0" + i); } mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuLists); mDrawerList.setAdapter(mAdapter); mDrawerList.setOnItemClickListener(this);
同时响应点击事件:
@Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //动态插入fragment到framelayout当中 Fragment contentFragment = new ContentFragment(); Bundle args = new Bundle(); args.putString("text", menuLists.get(i)); contentFragment.setArguments(args); FragmentManager fm = getFragmentManager(); fm.beginTransaction().replace(R.id.content_frame, contentFragment).commit(); mDrawerLayout.closeDrawer(mDrawerList); }
根据点击位置设置动态添加的fragment中的文字:
public class ContentFragment extends Fragment { private TextView mTextView; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_content, container, false); mTextView = (TextView) view.findViewById(R.id.textView); String text = getArguments().getString("text"); mTextView.setText(text); return view; } }
为DrawerLayout打开关闭设置监听器:
- mDrawerLayout.setDrawerListener(DrawerLayout.DrawerListener); 2. ActionBarDrawerToggle是DrawerLayout.DrawerListener的具体实现。
- 设置android.R.id.home的图标
- Drawer拉出,隐藏带有android.R.id.home动画效果
- 监听Drawer拉出还是隐藏事件
3. 覆盖ActionBarDrawerToggle的onDrawerOpened和onDrawerClosed()以监听抽屉拉出或隐藏事件。
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); getActionBar().setTitle("Please Choose"); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); getActionBar().setTitle(mtitle); } }; mDrawerLayout.setDrawerListener(mDrawerToggle);