Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

android - OnItemClickListener and OnClickListener in ListView

I have a custom view for each row in a custom ListAdapter and I am trying to perform onClick action and get the row position in the list where the click came from.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingLeft="10dip" android:id="@+id/itemRoot" android:clickable="false">

    <TextView android:layout_width="wrap_content" android:text="TextView"
        android:layout_height="wrap_content" android:id="@+id/itemTxt"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"></TextView>
    <TextView android:layout_toRightOf="@+id/itemTxt" android:text="TextView"
        android:layout_height="wrap_content" android:layout_alignBottom="@+id/itemTxt"
        android:id="@+id/amountTxt" android:paddingLeft="6dip"
        android:layout_centerVertical="true" android:layout_width="match_parent"></TextView>
    <ImageView android:id="@+id/delBtn" android:layout_height="wrap_content"
        android:src="@drawable/delete" android:layout_width="wrap_content"
        android:layout_alignParentRight="true" android:paddingRight="10dip"
        android:layout_centerVertical="true"></ImageView>

</RelativeLayout>

I want to figure out when TextView or ImageView is clicked, I also need to know what row in the list it came form. I have tried using OnItemClickListener and it works fine to get the row where the click comes from. However, once I register an OnClick listener for the views, the onItemClicked() method gets skipped and onClick() is executed straight away, but there is no way of getting the row position that the view is in(or at least not that I know if).

If I set clickable to false for the views, then onItemClicked get called and I tried manually calling performClick() on the given view. But this only works for the root element (RelativeLayout), and if click comes from TextView inside the layout the click doesn't propagate.

I can't really figure out how to get both position in the list and perform onClick action.

Any thoughts are welcome.

Alex

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could assign the proper OnClickListener to each ImageView and TextView from inside your ListAdapter's overridden getView method.

Inside that method you know the item's position, so you can pass it to the custom listener classes, and use it there as you want:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    // TODO: instantiate the layout 
    // here I call a super method
    final View view = super.getView(position, convertView, parent);
    final TextView textView = (TextView)view.findViewById(R.id.itemTxt);
    textView.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Log.i("Click", "TextView clicked on row " + position);
        }
    });
    final ImageView imageView = (ImageView)view.findViewById(R.id.delBtn);
    imageView.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Log.i("Click", "ImageView clicked on row " + position);
        }
    });
    return view;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...