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>
相关推荐
火柴就是我14 小时前
让我们实现一个更好看的内部阴影按钮
android·flutter
砖厂小工20 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心21 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心21 小时前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker1 天前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴1 天前
Android17 为什么重写 MessageQueue
android
阿巴斯甜2 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker2 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95272 天前
Andorid Google 登录接入文档
android
黄林晴2 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack