帮助在ListView中自定义行布局的项目方面的onClick()事件 [英] Help regarding onClick() event on an item of ListView custom row layout

查看:95
本文介绍了帮助在ListView中自定义行布局的项目方面的onClick()事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView的行由我进行格式化。每行具有的ImageView和TextView中的混合。 我也实现我自己的适配器,我可以通过它来绘制每一行。

I have a ListView whose rows are formatted by me. Each row has a mix of ImageView and TextView. I have also implemented my own adapter and am able to draw each row through it.

现在,我想是这样的 -

Now, I would want something like this-

  • 在用户点击一个ImageView的(不是其他地方的行,但只有这个ImageView的应该为点击响应)
  • 在我了解的的ImageView点击的行的位置。

我已经尝试了这么多事情,并希望我的code要尽可能高效(以矫枉过正的方面)。 目前我可以捕捉到只有特定的ImageView点击事件,但我不知道哪一行被点击了。

I have tried many things for this and have wanted my code to be as efficient as possible (in terms of overkill). Currently i can capture the click event on that particular ImageView only, but I can't know which row was clicked.

我已经提供了行XML属性是这样 -

I have provided an attribute in the Row XML like this-

<ImageView android:id="@+id/user_image"
    android:padding="5dip" 
    android:layout_height="60dip" 
    android:layout_width="60dip"
    android:clickable="true"
    android:onClick="uImgClickHandler"/> 

和我的code,我有这样一个方法:

And in my code, I have a method like this:

public void uImgClickHandler(View v){
  Log.d("IMG CLICKED", ""+v.getId());
  LinearLayout parentRow = (LinearLayout)v.getParent();

 }

我可以得到父行(也许),但我不知道如何进一步从这里走。 是否有人可以帮忙吗?

I can get the parent row (perhaps) but am not sure how to go further from here. Can someone please help?

推荐答案

请参阅本,
我只是写了code给你的想法,而不是在正确的格式

Please refer this,
Me just writing the code to give you idea, Not in correct format

 class youaddaper extends BaseAdapter{

   public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflate = LayoutInflater.from(context);
        View v = inflate.inflate(id, parent, false);

      ImageView imageview = (ImageView) v.findViewById(R.id.imageView);
        imageview.setOnClickListener(new imageViewClickListener(position));
      //you can pass what ever to this class you want,
      //i mean, you can use array(postion) as per the logic you need to implement 
   }
   class imageViewClickListener implements OnClickListener {
   int position;
    public imageViewClickListener( int pos)
        {
            this.position = pos;
        }

    public void onClick(View v) {
      {// you can write the code what happens for the that click and 
       // you will get the selected row index in position
     }
}

}

希望它帮助你。

这篇关于帮助在ListView中自定义行布局的项目方面的onClick()事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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