繁体   English   中英

MotionEvent.ACTION_UP和/或ACTION_MOVE并非总是触发

[英]MotionEvent.ACTION_UP and/or ACTION_MOVE not always firing

我有一个带有一系列linearlayout文本视图的ScrollView。 当用户“触摸”这些文本视图之一时,我正在临时更改背景颜色以“显示”触摸,以便他们可以直观地确认触摸的项目。 当他们松开手指时,我将背景颜色改回默认颜色。

我遇到的一个问题是,如果他们滚动用户,则ACTION_UP似乎不会启动,因此textview的背景永远不会变回默认值。

我以为可以通过在我的TouchListener上添加一个ACTION_MOVE来解决此问题,这似乎可以更好地解决此问题,但并非总是如此。

我的代码看起来像这样...

mytextview.setOnTouchListener(mytouch);

随着...

private View.OnTouchListener mytouch = new View.OnTouchListener(){
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            v.setBackgroundColor(getResources().getColor(R.color.mytouchcolor));
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            v.setBackgroundColor(getResources().getColor(R.color.mydefaultcolor));
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            v.setBackgroundColor(getResources().getColor(R.color.mydefaultcolor));
        }
        return false;
    }
}

如果我...

  • 触摸并释放
  • 触摸,缓慢滚动,然后释放。

如果我触摸并快速滚动(或轻拂),则ACTION_UP或ACTION_MOVE似乎从未触发(根据某些LOG.I()测试)。

我该如何解决这个问题,以便在触摸解除后滚动或不滚动,背景颜色总是恢复为默认值?

有一个简单的方法:

在布局中:

<TextView
        android:id="@+id/mytextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/view_selector"
        />

view_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:drawable="@mipmap/view_press"/>
<item android:drawable="@mipmap/view_default"/>

</selector>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM