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>
相关推荐
JMchen1231 小时前
现代Android图像处理管道:从CameraX到OpenGL的60fps实时滤镜架构
android·图像处理·架构·kotlin·android studio·opengl·camerax
快点好好学习吧2 小时前
phpize 依赖 php-config 获取 PHP 信息的庖丁解牛
android·开发语言·php
是誰萆微了承諾2 小时前
php 对接deepseek
android·开发语言·php
Dxy12393102163 小时前
MySQL如何加唯一索引
android·数据库·mysql
冠希陈、5 小时前
PHP 判断是否是移动端,更新鸿蒙系统
android·开发语言·php
晚霞的不甘7 小时前
Flutter for OpenHarmony从零到一:构建《冰火人》双人合作闯关游戏
android·flutter·游戏·前端框架·全文检索·交互
2601_949833397 小时前
flutter_for_openharmony口腔护理app实战+饮食记录实现
android·javascript·flutter
独自破碎E7 小时前
【滑动窗口+字符计数数组】LCR_014_字符串的排列
android·java·开发语言
stevenzqzq7 小时前
compose 中 align和Arrangement的区别
android·compose
VincentWei958 小时前
Compose:MutableState 和 mutableStateOf
android