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>
相关推荐
android_cai_niao1 小时前
Android中有什么技术过时了可以直接用新技术的
android·过时技术
lingggggaaaa1 小时前
PHP原生开发篇&文件安全&上传监控&功能定位&关键搜索&1day挖掘
android·学习·安全·web安全·php
我是场2 小时前
我的NPI项目 - OTA upgrade是什么?
android·人工智能
zh_xuan2 小时前
Android 实现fragment导航
android·fragment导航
java资料站2 小时前
MySQL 增量同步脚本
android·数据库·mysql
穷人小水滴2 小时前
手机安装 Cross Profile Test App (Android)
android·手机·一加
zh_xuan2 小时前
Android Paging 3实现分页加载
android·git·github·paging 3
天***88523 小时前
安卓KMPlayer安卓版播放器,支持AC-3、WMA、MP3、AAC
android·aac
jinanwuhuaguo4 小时前
OpenClaw 2026.4.5 深度解读
android·开发语言·人工智能·kotlin·openclaw
用户69371750013844 小时前
实测!Gemma 4 成功跑在安卓手机上:离线 AI 助手终于来了
android·前端·人工智能