【问题标题】:Listview onclick is not workingListview onclick 不起作用
【发布时间】:2013-11-13 10:18:13
【问题描述】:

listview OnItemClickListener 在处理listview 项OnLongClickListener 后不起作用。当我在列表视图项目中长按时,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>

自定义适配器

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

帮帮我..谢谢

【问题讨论】:

  • 分享你的java代码。
  • 请为列表视图项目使用上下文菜单 longclick mikeplate.com/2010/01/21/…
  • 我现在分享了java代码
  • 您是否在列表视图的 onItelclick 之后在日志中打印了任何行?
  • 没有在onItemClick之后会调用另一个Activity

标签: android listview android-listview onclick onlongclicklistener


【解决方案1】:

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

【讨论】:

  • 我已经尝试过,但列表视图 onclick 不起作用 onLongprss 立即起作用 onClick 正在触发
  • 我已经尝试了这两种情况(false,true),即使当我按下 List Item onLongClick 时 ListView OnItemClick 也不工作,LongClick 被触发并且立即 ListView OnItemClick 也被触发 ...
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多