本文介绍了关于 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 所在行的位置.

为此我尝试了很多方法,并希望我的代码尽可能高效(就矫枉过正而言).目前我只能捕获该特定 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.

我在 Row 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"/>

在我的代码中,我有一个这样的方法:

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?

推荐答案

请参考这个,
我只是写代码给你想法,格式不正确

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() 事件的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 23:13
查看更多