zoukankan      html  css  js  c++  java
  • 如何通过返回键缩回侧滑栏

    DrawerLayout这个布局可以实现qq的侧滑功能,可以通过触屏来实现在侧滑栏从左向右拉动出来,从右向左将侧滑栏缩回去,效果如下

    似乎很炫的样子,当侧滑栏还在的情况下,点击back键想变成下面的样子

    结果居然整个页面都退出了,不对啊,我只是想把侧滑栏缩回去,这是为什么呢?

    原因很简单:

                   因为侧滑栏是在DrawerLayout这个布局里面的,点击back键自然就退出DrawerLayout这个布局

    解决思路:

                1、当侧滑栏可见时,点击back键,只将侧滑栏缩回

                2、侧滑栏不可见时,点击back键,退出DrawerLayout整个布局

    接下来就监听Back键吧,其他的实现功能程序太多就不展示了,只列出了如何通过监听back键实现将侧滑栏缩回的功能

                  

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(keyCode == KeyEvent.KEYCODE_BACK ) {
    RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.start_drawer);
    if(relativeLayout.getVisibility()==View.VISIBLE){
    //当左边的菜单栏是可见的,则关闭
    drawerLayout.closeDrawer(relativeLayout);
    Log.d("ss","隐藏滑动菜单");
    } else {
    finish();
    }
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }

     注:这里relativeLayout是实现侧滑栏的布局,drawerLayout是整个布局,relativeLayout是在drawerLayout这个布局中的

    布局文件:

    <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    >


    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:id="@+id/main_drawer"
    ></RelativeLayout>



    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp">
    <android.support.v4.view.ViewPager

    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:id="@+id/viewpager">

    </android.support.v4.view.ViewPager>
    </RelativeLayout>
    <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="220dp"
    >
    <android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/refresh">
    <android.support.v7.widget.RecyclerView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/recycler"
    />

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

    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:onClick="doclick"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:src="@drawable/floatview"

    />
    </android.support.design.widget.CoordinatorLayout>
    <-- 这个是实现侧滑栏的那个相对布局,是通过Fragment动态加载进来的,这里就不细讲了 -->
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/start_drawer"
    android:layout_gravity="start"
    ></RelativeLayout>
    </android.support.v4.widget.DrawerLayout>


    左边菜单栏的打开和关闭:
    openDrawer(View v);
    closeDrawer(View v);
    这里需要注意的一点时,菜单栏关闭时,不需要写程序去开启菜单栏,直接通过手动触屏拉动就可以了。

  • 相关阅读:
    Linux内存分析
    mysql 分表
    安装YCM
    c/c++ 之静态库
    ubuntu20 宽带连接
    数据对齐
    计算机中浮点数的表示
    整数的表示
    信息的储存
    SparseTable ST表
  • 原文地址:https://www.cnblogs.com/xhf-wonder/p/6796924.html
Copyright © 2011-2022 走看看