DrawerLayout:安卓原生侧滑控件的使用

DrawerLayout最好为界面的根布局,官网是这样说的,否则可能会出现触摸事件被屏蔽的问题

 1.主内容视图一定要是DrawerLayout的第一个子视图
 2.主内容视图宽度和高度需要match_parent
 3.必须显示指定侧滑视图的android:layout_gravity属性
 android:layout_gravity = “start”时,从左向右滑出菜单
 android:layout_gravity = “end”时,从右向左滑出菜单
 不推荐使用leftright!!!

侧滑视图的宽度以dp为单位,不建议超过320dp(为了总能看到一些主内容视图)
设置侧滑事件:mDrawerLayout.setDrawerListener(DrawerLayout.DrawerListener);


案例:


布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.drawerlayout_demo.MainActivity">

    <!--主页面内容-->
    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

    <!--侧滑内容,比较复杂的话可以写到其他布局,用include-->
    <ListView
        android:id="@+id/lv_left"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#ff0"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"></ListView>

</android.support.v4.widget.DrawerLayout>

MainActivity代码:
public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

    private FrameLayout fl_content;

    private ListView lv_left;

    private List<String> title;

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

        initView();

        initData();

        initLeft();

        initContent();

        initListener();

    }

    private void initListener() {

        lv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, title.get(position), Toast.LENGTH_SHORT).show();

                ContentFragment fragment = new ContentFragment();
                Bundle bundle = new Bundle();
                bundle.putString("title", title.get(position));
                fragment.setArguments(bundle);
                getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, fragment).commit();

                closeDrawerLayout(); //关闭侧滑
            }
        });

        //侧滑监听
        mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });

        mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                super.onDrawerStateChanged(newState);
            }
        });
    }

    /**
     * 打开侧滑
     */
    public void openDrawerLayout() {
        mDrawerLayout.openDrawer(lv_left);
    }

    /**
     * 关闭侧滑
     */
    public void closeDrawerLayout() {
        mDrawerLayout.closeDrawer(lv_left);
    }

    private void initContent() {
        //设置默认值
        ContentFragment fragment = new ContentFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title.get(0));
        fragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, fragment).commit();
    }

    private void initLeft() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, title);
        lv_left.setAdapter(adapter);
    }

    private void initData() {
        title = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            title.add("标题" + i);
        }
    }

    private void initView() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        fl_content = (FrameLayout) findViewById(R.id.fl_content);
        lv_left = (ListView) findViewById(R.id.lv_left);
    }


}
主页面Fragment:
package com.example.drawerlayout_demo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * Created by UF_PC on 2017/8/11.
 */
public class ContentFragment extends Fragment {

    private View mView;
    private TextView tv_content;
    private ImageButton btn_open_close_left;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mView = View.inflate(getActivity(), R.layout.fragment_content, null);

        initView();

        initListener();

        String title = getArguments().getString("title");
        tv_content.setText(title);
        return mView;
    }

    private void initListener() {
        btn_open_close_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity) getActivity()).openDrawerLayout();
            }
        });
    }

    private void initView() {
        tv_content = (TextView) mView.findViewById(R.id.tv_content);
        btn_open_close_left = (ImageButton) mView.findViewById(R.id.btn_open_close_left);
    }


}

Fragment布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

    <ImageButton
        android:id="@+id/btn_open_close_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"/>

</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值