AndroidStudio最新版本如何实现水平滑动?
创始人
2024-08-21 11:00:35
0
  1. 在布局文件中添加HorizontalScrollView或ViewPager组件。

  2. 监听用户的手势事件,可以使用GestureDetector类。在onTouchEvent方法中创建GestureDetector对象,并重写onFling方法。

GestureDetector mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX() < e2.getX()) { // 手势向右滑动 } else if (e1.getX() > e2.getX()) { // 手势向左滑动 } return super.onFling(e1, e2, velocityX, velocityY); } });

  1. 将GestureDetector对象与水平滚动组件绑定。在水平滚动组件上覆盖onTouchEvent方法,将所有的触控事件传递给GestureDetector进行处理。

@Override public boolean onTouchEvent(MotionEvent event) { mGestureDetector.onTouchEvent(event); return super.onTouchEvent(event); }

示例代码:

public class MainActivity extends AppCompatActivity {

private GestureDetector mGestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    HorizontalScrollView horizontalScrollView = findViewById(R.id.horizontal_scroll_view);

    mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (e1.getX() < e2.getX()) {
                // 手势向右滑动
                horizontalScrollView.scrollTo(0, 0);
            } else if (e1.getX() > e2.getX()) {
                // 手势向左滑动
                horizontalScrollView.scrollTo(horizontalScrollView.getWidth(), 0);
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    });

    horizontalScrollView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

相关内容

热门资讯

脚本推荐!有哪些免费的wpk作... 脚本推荐!有哪些免费的wpk作必弊码,wpk辅助软件(透视)都是存在有攻略(哔哩哔哩)能透视中分为三...
脚本解密!aa poker透视... 脚本解密!aa poker透视软件,aapoker透视脚本(透视)果然真的是有神器(哔哩哔哩)1、每...
透视总结!pokeplus脚本... 透视总结!pokeplus脚本,微乐自建房脚本如何下载,解密脚本(有挂教程)-哔哩哔哩;1)poke...
透视安装!拱趴大菠萝开挂方法,... 透视安装!拱趴大菠萝开挂方法,微乐自建房免费黑科技下载,专业透视(有挂秘诀)-哔哩哔哩拱趴大菠萝开挂...
透视揭露!aapoker怎么设... 透视揭露!aapoker怎么设置提高好牌几率,aapoker怎么设置抽水(透视)一贯有app(哔哩哔...
透视有挂!wpk有作必弊吗,w... 透视有挂!wpk有作必弊吗,wpk刷入池率脚本(透视)确实存在有脚本(哔哩哔哩)1、免费辅助多个强度...
透视软件!wepoker透视脚... 透视软件!wepoker透视脚本下载,陕西微乐小程序攻略,总结透视(有挂详细)-哔哩哔哩1、每一步都...
不少玩家反映!aapoker辅... 不少玩家反映!aapoker辅助怎么用,微乐小程序免费黑科技5个玩家常用方法,专业透视(有挂教程)-...
透视有挂!德普之星透视辅助软件... 透视有挂!德普之星透视辅助软件是真的吗,德普辅助软件(透视)好像是有挂(哔哩哔哩)1、上手简单,内置...
透视模拟器!wpk软件是真的吗... 透视模拟器!wpk软件是真的吗,陕西微乐挖坑小程序脚本,有挂透视(揭秘有挂)-哔哩哔哩进入游戏-大厅...