-1

处理 listview 项 OnLongClickListener 后,listview OnItemClickListener 不工作。当我长按列表视图项目时,lognpress 和 onitemclicked 都同时触发。

列表视图 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>

客户适配器

    <?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;
            }
        });

列表视图活动代码

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

帮帮我..谢谢

4

2 回答 2

0

根据文档

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 中处理点击事件。

于 2013-11-13T10:22:51.223 回答
0

在项目点击事件上使用此代码

RelativeLayout rl = (RelativeLayout)v;
TextView text = (TextView)rl.getChildeAt(index);
于 2013-11-13T10:25:13.627 回答