【问题标题】:Android onClick and onTouchEvent conflict , how to handle it?Android onClick 和 onTouchEvent 冲突,如何处理?
【发布时间】:2015-10-13 06:53:25
【问题描述】:

我正在尝试在 Android 中设计 SwipeMenu Listview。但是我在将听众提供给适配器的父级相对布局时遇到了问题。所以,SwipeMenu 没有到来,并且来自 SwipeMenuListView,onTouchEvent 没有被调用。

我按照tutorial 设计 SwipeMenuListView。

我的代码:

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(getApplicationContext(),
                    R.layout.item_list_app, null);
            new ViewHolder(convertView);
        }
        final ViewHolder holder = (ViewHolder) convertView.getTag();
        ApplicationInfo item = getItem(position);

        holder.rlt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getApplicationContext(), "Click "+position, Toast.LENGTH_SHORT).show();

            }
        });



        holder.iv_icon.setImageDrawable(item.loadIcon(getPackageManager()));
        holder.tv_name.setText(item.loadLabel(getPackageManager()));
        return convertView;
    }

XML:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" >

<ImageView
    android:id="@+id/iv_icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/iv_icon"
    android:text="name"
    android:textColor="@android:color/black"
    android:textSize="18sp" />  </RelativeLayout>

在我的 getView 中,如果我将删除 onclick 侦听器,则 swipemenu 正在工作,但如果有侦听器,则 touchevent 不起作用。

所以这是我问题的上述代码,请给我一些解决方案。

【问题讨论】:

  • 您是否尝试过实现 OnItemClickListener,因为 rlt 布局上的 OnClickListener 基本上应该做同样的事情?
  • 是的 OnItemClickListener 工作正常,但我正在尝试从适配器的 OnClickListener 中做同样的事情
  • 为什么要在行上使用点击监听器而不是只在项目上使用(如果你说这个可以正常工作)?也许你可以在这里做一些事情来避免你原来的问题,比如让适配器实现 OnItemClickListener 并将其设置为 ListView 的侦听器,或者在适配器的构造函数中传递 ListView 并在那里设置侦听器

标签: android android-listview onclick touch-event


【解决方案1】:

我制作了一个库,它有很多功能,例如:

  • PullToRefresh
  • 滑动菜单
  • 加载更多分页
  • 带有固定标题项的SectionIndexer。

你可以从Github链接https://github.com/AbdulRehmanNazar/Android-PullToRefresh-with-SwipeMenu-ListView-and-Section-Indexer找到源代码 享受编码

【讨论】:

    【解决方案2】:

    只需将android:descendantFocusability="blocksDescendants" 添加到您的相对布局中即可。这应该可以。喜欢:

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rlt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:descendantFocusability="blocksDescendants" >
    
    <ImageView
    android:id="@+id/iv_icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/ic_launcher" />
    
    <TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/iv_icon"
    android:text="name"
    android:textColor="@android:color/black"
    android:textSize="18sp" />  
    </RelativeLayout>
    

    【讨论】:

    • 谢谢,我已经添加了您的代码,但是它不起作用,是否还需要添加一些Java代码?请告诉我!!!
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2020-08-16
    • 1970-01-01
    • 2017-07-14
    • 2013-09-01
    • 2015-04-07
    相关资源
    最近更新 更多