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>
相关推荐
Doro再努力8 小时前
【Linux操作系统10】Makefile深度解析:从依赖推导到有效编译
android·linux·运维·服务器·编辑器·vim
Daniel李华9 小时前
echarts使用案例
android·javascript·echarts
做人不要太理性10 小时前
CANN Runtime 运行时组件深度解析:任务调度机制、存储管理策略与维测体系构建逻辑
android·运维·魔珐星云
我命由我1234510 小时前
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响
android·java·开发语言·java-ee·android studio·android-studio·android runtime
朗迹 - 张伟11 小时前
Tauri2 导出 Android 详细教程
android
lpruoyu11 小时前
【Android第一行代码学习笔记】Android架构_四大组件_权限_持久化_通知_异步_服务
android·笔记·学习
独自破碎E12 小时前
【BISHI15】小红的夹吃棋
android·java·开发语言
李堇15 小时前
android滚动列表VerticalRollingTextView
android·java
lxysbly16 小时前
n64模拟器安卓版带金手指2026
android
游戏开发爱好者819 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview