Listview onclick无效

时间:2013-11-13 10:18:13

标签: android listview android-listview onclick onlongclicklistener

listview OnItemClickListener在处理listview项OnLongClickListener后无法正常工作。当我长时间按下listview项目时,lognpress和onitemclicked都会同时触发。

listview xml文件

         <ListView
                    android:id="@+id/activity_main_menu_listview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/event_adapter_text_color"
                    android:cacheColorHint="#00000000" >
                </ListView>

Custome适配器

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="92dp"
        android:focusableInTouchMode="false" >

        <ImageView
            android:id="@+id/list_view_event_log"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/icon_dummy_image" />

        <View
            android:id="@+id/list_view_event_date"
            android:layout_width="58dp"
            android:layout_height="52dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="14dp"
            android:layout_toRightOf="@+id/list_view_event_log"
            android:background="@drawable/ic_calendar_bground"
            android:clickable="false" />

        <TextView
            android:id="@+id/list_view_event_month_textView"
            android:layout_width="wrap_content"
            android:layout_height="15dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:layout_toRightOf="@+id/list_view_event_log"
            android:gravity="center"
            android:text="@string/app_name"
            android:textColor="@color/white_color" />

        <TextView
            android:id="@+id/list_view_event_date_textView"
            android:layout_width="wrap_content"
            android:layout_height="18dp"
            android:layout_below="@+id/list_view_event_month_textView"
            android:layout_gravity="center"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="2dp"
            android:layout_toRightOf="@+id/list_view_event_log"
            android:text="@string/app_name"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/list_view_event_day_textView"
            android:layout_width="wrap_content"
            android:layout_height="14dp"
            android:layout_below="@+id/list_view_event_date_textView"
            android:layout_gravity="center"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="25dp"
            android:layout_toRightOf="@+id/list_view_event_log"
            android:text="@string/app_name"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/list_view_event_time_textView"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_below="@+id/list_view_event_date"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="4dp"
            android:layout_toRightOf="@+id/list_view_event_log"
            android:text="@string/app_name"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/list_view_event_title"
            android:layout_width="fill_parent"
            android:layout_height="20dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/list_view_event_date"
            android:clickable="false"
            android:singleLine="true"
            android:text="@string/app_name"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/list_view_event_location"
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:layout_below="@+id/list_view_event_title"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="4dp"
            android:layout_toRightOf="@+id/list_view_event_date"
            android:clickable="false"
            android:text="@string/app_name"
            android:textColor="@color/event_adapter_text_color"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/list_view_event_price"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/list_view_event_location"
            android:layout_marginBottom="4dp"
            android:layout_marginRight="10dp"
            android:clickable="false"
            android:text="@string/app_name"
            android:textSize="18sp" />

    </RelativeLayout>

自定义适配器代码

public View getView(int position, View convertView, ViewGroup parent) {
        mView = convertView;
        EventUtil eventUtil = mEventUtil.get(position);

        mView = mLayoutInflater.inflate(R.layout.row_event_adapter, null);

        TextView eventTitleView = (TextView) mView
                .findViewById(R.id.list_view_event_title);
        TextView eventDescView = (TextView) mView
                .findViewById(R.id.list_view_event_location);
        TextView eventDateView = (TextView) mView
                .findViewById(R.id.list_view_event_price);

        // new ImageFeach().execute(mEventUtil.getEvent_Image_Url());

        eventTitleView.setText(eventUtil.getEvent_Title());

        // event title sorting
        eventTitleView.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {

                // Toast.makeText(mContext, "LongClick",
                // Toast.LENGTH_LONG).show();
                if (mEventUtil != null) {
                    Collections.sort(mEventUtil, new EventComparator());

                    notifyDataSetChanged();
                }

                return false;
            }
        });

        // event description sorting
        eventDescView.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {

                // Toast.makeText(mContext, "LongClick",
                // Toast.LENGTH_LONG).show();
                if (mEventUtil != null) {
                    Collections.sort(mEventUtil, new Comparator<EventUtil>() {

                        @Override
                        public int compare(EventUtil event1, EventUtil event2) {

                            return event1.getEvent_location().compareTo(
                                    event2.getEvent_location());
                        }

                    });

                    notifyDataSetChanged();
                }
                return false;
            }
        });

        // event price sorting
        eventDateView.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {

                // Toast.makeText(mContext, "LongClick",
                // Toast.LENGTH_LONG).show();
                if (mEventUtil != null) {
                    Collections.sort(mEventUtil, new Comparator<EventUtil>() {

                        @Override
                        public int compare(EventUtil event1, EventUtil event2) {

                            return event1.getEvent_Price().compareTo(
                                    event2.getEvent_Price());
                        }

                    });
                }
                notifyDataSetChanged();

                return false;
            }
        });

Listview活动代码

mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view,
                    int position, long arg) {
}
}

帮帮我..谢谢

2 个答案:

答案 0 :(得分:0)

根据documentation

onLongClick() - This returns a boolean to indicate whether you have consumed
the event and it should not be carried further. That is, return true to indicate 
that you have handled the event and it should stop here; return false if you have 
not handled it and/or the event should continue to any other on-click listeners.

您需要在onLongClick中返回false,以便您也可以在onClick中处理click事件。

答案 1 :(得分:0)

项目点击事件

使用此代码

RelativeLayout rl = (RelativeLayout)v;
TextView text = (TextView)rl.getChildeAt(index);