现在的位置: 首页 > 综合 > 正文

用Drawerlayout来实现侧滑效果

2018年09月06日 ⁄ 综合 ⁄ 共 3315字 ⁄ 字号 评论关闭

        前段时间项目中用到侧滑效果,我了解了一下,比较流行的是使用sliding menu,我想android自己是否也实现了侧滑相关的api呢? 于是,我搜索了一下,了解到Drawerlayout。在2013年谷歌i/o大会上,添加了Drawerlayout控件,使用这个控件也能够实现侧滑效果,就是界面没有那么花哨而已,但是因为避免了引进第三方开源库,不失为实现侧滑的一种好方式。当学习一样新东西的时候,我们总是到android官网去学习一下。如果童鞋们上不了官网,那么我上传了附件,附件是官网的demo和android的api手册,请下载欣赏,一边欣赏,一边学习。如果你不喜欢看英语手册,那就往下看,听我唠叨。

       请看以下布局文件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" />
    <!-- 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>
要实现侧滑效果,只需要建立一个新工程,然后将加载的布局写成这个样子就可以了。是不是觉得好简单!当然,你不会那么容易就满足了。看红色部分代码,设置“start”
从左到右滑动,如果设置“end”表示从右向左滑动。
    接下来,让我们给侧滑菜单添加内容。在onCreate方法中,添加如下代码。具体的定义我就不写了。
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView)findViewById(R.id.left_drawer);
        mPlanetTitles = new String[]{
                "Mercury", "Venus", "Earth"
        };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPlanetTitles);
        mDrawerList.setAdapter(adapter);
运行之后,手指放在屏幕左边,由左往右滑动,可以看到左边的菜单出现了上述星球的列表项。
此时点击侧滑菜单,没有任何效果的,添加列表项点击事件处理即可。在MainActivity中添加如下代码:
  private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Bundle bundle = new Bundle();
            bundle.putString("drawerlayout", (String) parent.getItemAtPosition(position));
            PlanetFragment planetFragment = new PlanetFragment();
            planetFragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.content_frame, planetFragment).commit();
            mDrawerLayout.closeDrawer(mDrawerList);
        }
}
在mDrawerList.setAdapter(adapter);之后添加 mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
新建PlanetFragment.java和planet.xml。
PlanetFragment.java
public class PlanetFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.planet, null);
        TextView textView = (TextView) view.findViewById(R.id.text_view);
        textView.setText(getArguments().getString("drawerlayout"));
        return view;
    }
}

planet.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/planet_fragment">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_view"/>

</LinearLayout>
以上就完成了侧滑菜单的效果了。
如果你的Activity是带ActionBar的,需要做一些标题切换和菜单隐藏,显示的。请下载api手册,并找到Drawerlayout的那一章节,自己研究下。



附件api手册和源码下载地址:

http://pan.baidu.com/share/link?shareid=1439879773&uk=554616992

抱歉!评论已关闭.