Android实现上下滑动隐藏/显示工具栏

对于移动端,为了留出更多的空间来显示内容,我们在向上滑动视图的时候,总是希望,顶部工具栏和底部工具栏可以随着我们向上滑动的手势而自动隐藏。而当我们浏览完内容,向下滑动视图的时候,又希望工具栏可以自动显示出来响应我们操作。这是一种很好的交互模式,现在我们来看看这种交互怎么实现。

布局很简单,上部工具栏和下部工具栏,还有中间的一个scrollView,里面是一张图片。

<RelativeLayout 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"
     tools:context=".MainActivity">

    <include
        android:id="@+id/top_menu"
        layout="@layout/top_menu"/>
    <ScrollView
        android:id="@+id/main_content"
        android:layout_below="@+id/top_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="夏季女装爆款"
                android:textColor="#ffffff"
                android:background="#000000"
                android:textSize="20sp" />
            <ImageView
                android:id="@+id/photo_pic"
                android:layout_below="@+id/title"
                android:adjustViewBounds="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/clothes"/>
            <RelativeLayout
                android:layout_below=
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Android Studio中实现下滑动的功能可以通过以下步骤来完成: 1. 首先,在你的布局文件中添加一个可滑动的视图,比如ScrollView或RecyclerView。例如,你可以在XML布局文件中添加一个ScrollView: ```xml <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 在这里添加你的其他视图 --> </ScrollView> ``` 2. 接下来,在你的Java代码中找到ScrollView的引用,并设置滑动监听器。例如,你可以在Activity的onCreate方法中添加以下代码: ```java ScrollView scrollView = findViewById(R.id.scrollView); scrollView.setOnTouchListener(new View.OnTouchListener() { private float startY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startY = event.getY(); break; case MotionEvent.ACTION_UP: float endY = event.getY(); if (startY < endY) { // 向下滑动 // 在这里添加你的处理逻辑 } else if (startY > endY) { // 向上滑动 // 在这里添加你的处理逻辑 } break; } return false; } }); ``` 在上述代码中,我们通过设置OnTouchListener来监听ScrollView的触摸事件。当用户按下手指时,我们记录下起始的Y坐标;当用户抬起手指时,我们获取当前的Y坐标,并根据起始坐标和当前坐标的差值来判断是向上滑动还是向下滑动。 你可以根据自己的需求,在相应的位置添加你的处理逻辑。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值