点击Horizo​​ntalScrollView事件 [英] Click event of HorizontalScrollView

查看:291
本文介绍了点击Horizo​​ntalScrollView事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个horizo​​ntalscrollview里面,我有一大堆的图片。现在我需要检测情况下,任何图像的被点击其中的Click事件。总之,我需要放置在里面horizo​​ntalscrollview的形象,它被点击的索引。

 < Horizo​​ntalScrollView
    机器人:ID =@ + ID / horizo​​ntalScrollView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@绘制/ BGCOLOR>    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=横向>        < ImageView的
            机器人:ID =@ + ID / imageview1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:paddingRight =20dp
            机器人:SRC =@绘制/ image1的/>       < ImageView的
            机器人:ID =@ + ID / imageview2
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:paddingRight =20dp
            机器人:SRC =@绘制/图像2/>

我试过围绕OnTouchListener事件打,但即使滚动时被触发得到。而OnClickListener甚至没有得到触发,无论无论你点击。

  horizo​​ntalScrollView1.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            的System.out.println(测试滚动型);
        }
    });

有没有一种解决方法,以获得horizo​​ntalscrollview内被点击图像present的指数?我想它可以通过资料库窗口小部件来实现,但是这是在API16 pcated德$ P $。任何人都可以共享的方式通过Horizo​​ntalScrollView做到这一点?


解决方案

附加的 OnClickListener 到每个的ImageView Horizo​​ntalScrollView 小部件。根据查看传入的onClick(),你就会知道哪些的ImageView 被点击了。

具体做法是:


  • 添加安卓的onClick =heyMyImageGotClickedWhoHoo来在布局每个的ImageView 元素


  • 实现一个公共无效heyMyImageGotClickedWhoHoo(视图V)方法正在加载此布局的活动。在人体中,你可以使用 v.getId(),并比较了的ImageView 控件的ID(如 R.id.imageview1 ),以确定哪些点击得到了


或者,你可以为每个的ImageView 专用的方法,而不是路线他们都之一。在这种情况下,每个安卓的onClick 属性将有一个不同的值(例如, omgTheFirstImageWasClicked ),用匹配的方法在活动中(例如,公共无效omgTheFirstImageWasClicked(视图v))。

I have a horizontalscrollview inside which i have a bunch of images. Now i need to detect the click event in case any of the image is clicked within it. In short, i need the index of the image placed inside the horizontalscrollview, which was clicked.

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bgcolor" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageview1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingRight="20dp"
            android:src="@drawable/image1" />

       <ImageView
            android:id="@+id/imageview2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingRight="20dp"
            android:src="@drawable/image2" />

I tried playing around OnTouchListener event, but that was getting triggered even while scrolling. And the OnClickListener was not even getting triggered no matter wherever you click.

horizontalScrollView1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("Testing scrollview");
        }
    });

Is there a workaround to get the index of the clicked image present inside the horizontalscrollview? I guess it can be achieved via "Gallery" widget but that is deprecated in API16. Can anyone share an approach to accomplish this via HorizontalScrollView?

解决方案

Attach an OnClickListener to each of the ImageView widgets in the HorizontalScrollView. Based on the View passed into onClick(), you will know which ImageView was clicked.

Specifically:

  • Add android:onClick="heyMyImageGotClickedWhoHoo" to each ImageView element in your layout.

  • Implement a public void heyMyImageGotClickedWhoHoo(View v) method in the activity that is loading this layout. In the body, you can use v.getId() and compare it to the ImageView widget IDs (e.g., R.id.imageview1) to determine which got clicked.

Or, you can create dedicated methods for each ImageView, rather than route them all to one. In that case, each android:onClick attribute would have a different value (e.g., omgTheFirstImageWasClicked), with a matching method in the activity (e.g., public void omgTheFirstImageWasClicked(View v)).

这篇关于点击Horizo​​ntalScrollView事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆