在Listview中未触发Onitemclick侦听器

时间:2013-08-20 05:20:51

标签: android android-listview

我有一个listview,我正在使用OnItemClickListener作为listview。同时,在我的自定义适配器的getview方法中,我使用onclicklistener进行整个行视图。我发现在自定义适配器中使用Onclicklisnter时,我的onItemClickListener不会被触发。我想同时使用这两个监听器。任何人都可以帮我做吗?提前谢谢。

我的listview听众,

list_specialists.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                HashMap<String, String> trends_data= new HashMap<String, String>();
                trends_data = specobj.get(position);
                specialist.setText(trends_data.get(KEY_SPEC_TITLE));

                new GetTime(BookingActivity.this).execute();


                }
            });

我的自定义适配器中的getview方法

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = convertView;
    final ViewHolder holder;

    if (rowView == null) {
        rowView = inflater.inflate(R.layout.row_timeslot, parent, false);
        holder = new ViewHolder();
        holder.title = (TextView) rowView.findViewById(R.id.textView2);
        holder.layout = (RelativeLayout) rowView
                .findViewById(R.id.relativeLayout1);
        holder.check = (org.holoeverywhere.widget.CheckBox) rowView
                .findViewById(R.id.checkBox1);

        rowView.setTag(holder);
    } else {
        holder = (ViewHolder) rowView.getTag();
    }

    HashMap<String, String> trends_data = new HashMap<String, String>();
    trends_data = trendsobj.get(position);

    holder.check.setId(position);

    holder.check.setChecked(checkarry[position]);

    // for managing the state of the boolean
    // array according to the state of the
    // CheckBox

    holder.check.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            for (int i = 0; i < checkarry.length; i++) {
                if (position == i) {
                    checkarry[i] = true;
                } else {
                    checkarry[i] = false;
                }

                RefreshList();
            }
        }
    });

    holder.layout.setOnClickListener(new View.OnClickListener() { // Here I am using onclicklistener for rows's parent layout.

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub




            for (int i = 0; i < checkarry.length; i++) {
                if (position == i) {
                    checkarry[i] = true;
                } else {
                    checkarry[i] = false;
                }

                RefreshList();
            }



        }
    });

    holder.title.setText(trends_data.get(BookingActivity.KEY_TIME_TITLE));



    return rowView;
}

rowlayout xml文件...

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:focusable="false"
    android:background="@drawable/list_selector"
    android:orientation="vertical"
    android:padding="16dp" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/activity_googlecards_card_imageview"
        android:text="Rate "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#757572" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/textView3"
        android:text="AED 200.00"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/mainservice" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/activity_googlecards_card_imageview"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/activity_googlecards_card_imageview"
        android:gravity="center"
        android:text="Specialist Name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#6945B2"
        android:textStyle="bold" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/activity_googlecards_card_imageview"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <ImageView
        android:id="@+id/activity_googlecards_card_imageview"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        android:src="@drawable/list_dummy" />

</RelativeLayout>*

2 个答案:

答案 0 :(得分:1)

请参阅此链接。这篇文章给你最好的想法。 Link

答案 1 :(得分:0)

如果列表行中有clickalbe项,其click()动作将发生而不是onItemClickListener。在这里,行项目中有一个复选框,如果您仍想使用onItemClickListener,则必须使CheckBox在布局中不可聚焦,如下所示:

android:focusable="false"
android:focusableInTouchMode="false"