记录一次Android侧滑需求代码

点击/滑动界面显示,不多说,上代码,性能未知

效果图

点击/滑动前界面     滑动后效果    

 

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:clipChildren="true"
    android:orientation="vertical"

    tools:openDrawer="start">
    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:textAlignment="center"
        android:gravity="center"
        android:background="@color/colorAccent"
        android:layout_height="40dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:text="我是标题" />
    </androidx.appcompat.widget.LinearLayoutCompat>

        <AbsoluteLayout
            android:clipChildren="true"
            android:id="@+id/content_field"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff">
            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="370dp"
                android:background="@android:color/holo_blue_dark"
                android:layout_height="match_parent">
            </androidx.appcompat.widget.LinearLayoutCompat>
              <androidx.appcompat.widget.LinearLayoutCompat
                  android:layout_width="match_parent"
                  android:gravity="center"
                  android:id="@+id/right"
                  android:layout_x="370dp"
                  android:orientation="vertical"
                  android:layout_height="match_parent">
                <androidx.appcompat.widget.LinearLayoutCompat
                    android:layout_width="match_parent"
                    android:background="@android:color/white"
                    android:layout_height="40dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_weight="1"
                        android:text="标题1"
                        android:gravity="center"
                        android:layout_height="40dp"></TextView>
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_weight="1"
                        android:text="标题2"
                        android:gravity="center"
                        android:layout_height="40dp"></TextView>
                    <TextView
                        android:id="@+id/title4"
                        android:layout_width="match_parent"
                        android:layout_weight="1"
                        android:text="标题3"
                        android:gravity="center"
                        android:layout_height="40dp"></TextView>
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_weight="1"
                        android:text="标题4"
                        android:gravity="center"
                        android:layout_height="40dp"></TextView>
                </androidx.appcompat.widget.LinearLayoutCompat>
                  <androidx.appcompat.widget.LinearLayoutCompat
                      android:layout_width="match_parent"
                      android:layout_weight="1"

                      android:layout_height="match_parent">
                      <View
                          android:layout_width="match_parent"
                          android:layout_weight="1"
                          android:background="@android:color/holo_red_dark"
                          android:layout_height="match_parent"></View>
                      <View
                          android:layout_width="match_parent"
                          android:layout_weight="1"
                          android:background="@android:color/holo_orange_dark"
                          android:layout_height="match_parent"></View>
                      <View
                          android:layout_width="match_parent"
                          android:layout_weight="1"
                          android:background="@android:color/holo_green_light"
                          android:layout_height="match_parent"></View>
                      <View
                          android:layout_width="match_parent"
                          android:layout_weight="1"
                          android:background="@android:color/holo_blue_dark"
                          android:layout_height="match_parent"></View>
                  </androidx.appcompat.widget.LinearLayoutCompat>
              </androidx.appcompat.widget.LinearLayoutCompat>
        </AbsoluteLayout>

</androidx.appcompat.widget.LinearLayoutCompat>

activity

import android.animation.ObjectAnimator;
import android.graphics.Point;
import android.graphics.Rect;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.ui.AppBarConfiguration;

public class MainActivity3 extends AppCompatActivity {
    LinearLayoutCompat rightLayout;
    private float startX, startY, endX, endY;
    private ObjectAnimator animatorjin, animatortui;
    private TextView title4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        rightLayout = findViewById(R.id.right);
        title4 = findViewById(R.id.title4);
        animatorjin = ObjectAnimator.ofFloat(rightLayout, "translationX", 0, -830);
        animatortui = ObjectAnimator.ofFloat(rightLayout, "translationX", -830, 0);
        animatorjin.setDuration(200);
        animatortui.setDuration(200);
        Log.e("标题", checkIfVisable() + "");
        rightLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    /**
                     * 点击的开始位置
                     */
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        startY = event.getY();
                        Log.e("起始位置:", event.getX() + "," + event.getY());
                        break;
                    /**
                     * 触屏实时位置
                     */
                    case MotionEvent.ACTION_MOVE:
                        //  Log.e("实时位置:", event.getX() + "," + event.getY());
                        break;
                    /**
                     * 离开屏幕的位置
                     */
                    case MotionEvent.ACTION_UP:
                        endX = event.getX();
                        endY = event.getY();
                        Log.e("结束位置:", event.getX() + "," + event.getY() + (startX - endX > 300f));
                        if (checkIfVisable()) {//可见
                            if ((endX - startX > 300f) || (startX == endX && startY == endY))
                                animatortui.start();
                        } else {//不可见
                            if ((startX - endX > 300f) || startX == endX && startY == endY)
                                animatorjin.start();
                        }
                        break;
                    default:
                        break;
                }
                /**
                 *  注意返回值
                 *  true:view继续响应Touch操作;
                 *  false:view不再响应Touch操作,故此处若为false,只能显示起始位置,不能显示实时位置和结束位置
                 */
                return true;
            }
        });
    }

    boolean checkIfVisable() {
        Point p = new Point();
        int screenWidth = p.x;
        int screenHeight = p.y;
        Rect rect = new Rect(0, 0, screenWidth, screenHeight);
        int[] location = new int[2];
        title4.getLocationInWindow(location);
        if (title4.getLocalVisibleRect(rect)) {
            return true;
        } else {
            return false;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值