【问题标题】:OnTouch() & onClick() Events for a VIew within a ScrollViewScrollView 中的视图的 OnTouch() 和 onClick() 事件
【发布时间】:2013-09-23 07:07:33
【问题描述】:

我在滚动视图中有 10 个图像视图

点击图片后,我想执行一个操作

但是当我尝试滚动 ScrollView 中的图像时,T​​ouch_down 和 Touch_UP 一起被视为单击

帮帮我 我知道解决方案很简单 但我认为我缺少一些逻辑

我把我的代码放在这里

  public class CustomScrollView extends ScrollView {

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;

    }

    @Override
    public boolean onTouchEvent(MotionEvent p_event) {
        if (p_event.getAction() == MotionEvent.ACTION_MOVE && getParent() != null) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        return super.onTouchEvent(p_event);
    }
 }

在这个 ScrollView 中我添加了一个 ImageView

【问题讨论】:

  • 请大家提出一个不使用 ListView 的解决方案
  • 没有代码没人能帮你
  • 添加ImageView的代码在哪里?

标签: android onclick scrollview touch-event


【解决方案1】:

好吧,我会尝试回答.. 我认为(在我看来),你应该只对 ImageView 使用 onclick,所以当你在 ScrollView 上滚动时,ImageView 不会被点击。 这是我的代码示例

关于layout.xml

<ImageView
            android:id="@+id/ur_img_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:contentDescription="@string/description"
            android:onClick="onClickEvent"
            android:src="@drawable/yourDrawable"
            />

然后在你的 activity.java 上创建事件导入 android.view.View,创建一个类似于你在 .xml 文件中使用的方法,(在示例 android:onClick=" onClickEvent")

public void onClickEvent(View v){
      //do your event here
}

就是这样。

【讨论】:

  • 我已经尝试过这个问题仍然存在记住我正在使用自定义滚动视图,其代码存在于问题中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
相关资源
最近更新 更多