android滑动看新闻

java 复制代码
package com.example.myapplication5;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class TouchActivity extends AppCompatActivity {

    private TextView content;
    // 在 Activity 类中添加
    private GestureDetector gestureDetector;
    String[] news={
            "第一页",
            "第二页",
            "第三页",
            "第四页",
            "第五页"





    };
    int index=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_touch);
        content = findViewById(R.id.content);
        content.setText(news[0]);
        // 初始化手势检测器
        gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if(e1.getX() >= e2.getX()  ) {
                    if(index<=news.length-2){
                        index++;
                        content.setText(news[index]);
                    }


                }else{
                    //右滑减小index
                    if(index>=1){
                        index--;
                        content.setText(news[index]);
                    }
                }
                return true;
            }
        });

    }
    // 重写 onTouchEvent 方法
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
}
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="左滑查看下一页\n右滑查看上一页"
        />

</LinearLayout>
相关推荐
袁美丽..8 分钟前
Android studio的adb和终端的adb互相抢占端口
android·adb·android studio
鹏多多.1 小时前
flutter-使用fluttertoast制作丰富的高颜值toast
android·前端·flutter·ios
守城小轩1 小时前
Firefox Android 开发环境搭建全流程(四)
android·firefox·chrome devtools·指纹浏览器·浏览器开发
袁美丽..1 小时前
Android --- AOSP源码导入Android Studio
android·android studio
LiuYaoheng2 小时前
【Android】View 的基础知识
android·java·笔记·学习
出海小纸条2 小时前
Google Play 应用被拒-数据安全表单无效(设备上的应用)
android
和煦的春风2 小时前
简单讨论下lmkd 查杀机制
android
Android轮子哥2 小时前
月下载 40 万次的框架是怎么练成的
android
三少爷的鞋2 小时前
Kotlin 协程真的是线程框架吗?
android
三雒3 小时前
ART堆内存系列二:从堆中排除大对象
android·性能优化