【问题标题】:Android CardView with ListView inside - onTouchListener on CardView not working内部带有 ListView 的 Android CardView - CardView 上的 onTouchListener 不起作用
【发布时间】:2016-09-02 23:14:58
【问题描述】:

我有一个卡片视图,里面有一个列表视图。我也需要列表视图中的单击项目,并且我也希望能够使用 ontouchlistener 移动整个卡片视图。我在cardview上设置了一个onTouchListener,但它不起作用。

代码:

把它放在 build.gradle 中:

compile 'com.android.support:cardview-v7:22.0+'

主活动:

package com.XXXXXXXX.testontouch;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView mylistview;
    private CardView mycardview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mycardview = (CardView)findViewById(R.id.mycardview);
        mylistview = (ListView) findViewById(R.id.myListView);

        List<String> your_array_list = new ArrayList<String>();
        your_array_list.add("foo");
        your_array_list.add("bar");
        your_array_list.add("foo");
        your_array_list.add("bar");
        your_array_list.add("foo");
        your_array_list.add("bar");
        your_array_list.add("foo");
        your_array_list.add("bar");
        your_array_list.add("foo");
        your_array_list.add("bar");
        your_array_list.add("foo");
        your_array_list.add("bar");
        your_array_list.add("foo");
        your_array_list.add("bar");


        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                your_array_list );

        mylistview.setAdapter(arrayAdapter);
        mycardview.setCardElevation(20);
        mycardview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                System.out.println("TOuchedddd");
                return true;
            }
        });

    }
}

TOuchedddd 从不打印。 如果我从卡片视图中删除 ListView,它就会开始工作。

因此,listView 以某种方式首先消耗了触摸事件,而不是父 cardview。

MainActivity 的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.pranapps.testontouch.MainActivity">



<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:id="@+id/mycardview"
    card_view:cardUseCompatPadding="true">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/myListView"
        android:dividerHeight="0.2dp"
        android:overScrollMode="always"
        android:smoothScrollbar="true"
        android:groupIndicator="@null"
        ></ListView>


</android.support.v7.widget.CardView>

</RelativeLayout>

我已经为 android:clickable、a​​ndroid:focusable 和 android:focusableInTouchMode 尝试了 true 和 false。不走运。

【问题讨论】:

  • 你想点击列表视图中的项目吗?
  • 是的,我也需要列表视图中的点击项,我也希望能够使用 ontouchlistener 移动整个卡片视图。
  • 您可以尝试为“MotionEvent 事件”使用 switch case:switch(event.getAction()) {case MotionEvent.ACTION_DOWN:.........
  • 那行不通。 System.out 甚至在 switch 语句之前出现,甚至没有执行。就好像 ontouchlistener 甚至没有在父视图上执行

标签: android listview ontouchlistener android-cardview


【解决方案1】:

在 Android 中,触摸事件会如您所愿地从孩子传给父母。但是,父母可以选择拦截针对其孩子之一的所有触摸事件,并决定否决将事件分派给孩子。这正是您想要的,如果我理解正确,您的触摸事件应该被称为触摸卡片视图时发生的任何事情,然后在需要时将触摸事件分派给您的子列表视图。

要拦截来自 CardView 的触摸事件,您需要创建一个自定义视图来继承它并重写 onInterceptTouchEvent 方法:

package com.pranapps.testontouch;

import android.content.Context;
import android.util.AttributeSet;
import android.support.v7.widget.CardView;
import android.view.MotionEvent;
import android.view.View;

public class CardViewTouchThief extends CardView {

    public CardViewTouchThief(Context context) {
        super(context);
    }
    public CardViewTouchThief(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CardViewTouchThief(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        /* 
        * This method determines whether we want to intercept the motion. 
        * If we return true, onTouchEvent will be called. 
        */ 
        return true; 
    } 

} 

然后您使用 CardViewTouchThief,您通常会在 XML 布局中使用 CardView:

<com.pranapps.testontouch.CardViewTouchThief
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/mycardview"
card_view:cardUseCompatPadding="true">

    <ListView
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/myListView"
    android:dividerHeight="0.2dp"
    android:overScrollMode="always"
    android:smoothScrollbar="true"
    android:groupIndicator="@null"/>

</com.pranapps.testontouch.CardViewTouchThief>

并且在您的活动中放置您自己的逻辑来处理您希望将触摸事件发送到列表视图的任何时间。

mycardview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            Log.d("MainActivity", "TOuchedddd");
            if(mylistview!=null){
                //Route all touch event to listview without logic
                mylistview.onTouchEvent(event);
            }
            return true;
        }
    });

Here是固定项目源码。

【讨论】:

  • 非常感谢你!!!!你是超级英雄!!!!这绝对是惊人的!希望我接下来的 Android 之旅一切顺利,再次感谢!
  • 我会在 21 小时内奖励赏金
  • 很高兴为您提供帮助!
  • 您的解决方案确实有效,但是当我将卡片视图的实际移动和列表视图的滚动结合起来时,它似乎不起作用:(您能看看这个新项目吗?dropbox.com/sh/8s3v9ydcj6jvpl8/AACZ2VRP2N9R1ec7pxrsAn0ga?dl=0
  • 其实我在这里问了一个单独的问题,你能回答吗?:stackoverflow.com/questions/37358015/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多