Android 使ListView不可单击,以便能够单击其父视图上的视图

Android 使ListView不可单击,以便能够单击其父视图上的视图,android,listview,android-listview,listadapter,Android,Listview,Android Listview,Listadapter,我在另一个视图上放置了一个ListView,我希望列表只显示一些变化的数据,用户不应该与列表进行任何交互。 列表的父视图上有一些按钮,我需要能够通过列表访问这些按钮。 我在这里尝试了许多建议的解决方案,但没有一个对我有效 您可以看到我尝试的各种解决方案(解决方案组合)的注释 有什么建议吗 扩展的列表视图: public class ItemList extends ListView { private MyListAdapater myListAdapater; private

我在另一个视图上放置了一个
ListView
,我希望列表只显示一些变化的数据,用户不应该与列表进行任何交互。 列表的父视图上有一些按钮,我需要能够通过列表访问这些按钮。 我在这里尝试了许多建议的解决方案,但没有一个对我有效

您可以看到我尝试的各种解决方案(解决方案组合)的注释

有什么建议吗

扩展的
列表视图

public class ItemList extends ListView {

    private MyListAdapater myListAdapater;
    private Item[] items;

    public ItemList(Context context, int rows) {
        super(context);
        items = new Item[rows];
        for (int i = 0; i < items.length; i++) {
            items[i] = new item(context, "Bla Bla Bla");
        }


        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        layoutParams.setMargins(pxToDp(8), 0, 0, pxToDp(12));
        setLayoutParams(layoutParams);
        myListAdapater = new MyListAdapater(context, items);
        setAdapter(myListAdapater);
        setDivider(null);
        int rowMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
        setDividerHeight(pxToDp(8));
        setSelector(R.color.Transparent);
//        setOnTouchListener(new OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return false;
//            }
//        });
//        setFocusable(false);
//        setClickable(false);
//        setFocusableInTouchMode(false);
//        requestDisallowInterceptTouchEvent(true);
        setSelection(myListAdapater.getCount() - 1);
    }



    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }

    protected int pxToDp(int px) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, getResources().getDisplayMetrics());
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        return true;
//    }

   }
  • 列表中的项目本身就是视图
多谢各位

编辑:

我尝试用xml添加列表,但仍然无法单击列表后面的视图

我尝试了一个具有以下布局的测试项目:

?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.listtest.MainActivity"
    android:background="@color/Blue">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click"
        android:layout_gravity="center"
        android:onClick="onClickTestButton"/>

    <ListView
        android:id="@+id/test_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="12dp"
        android:layout_marginLeft="8dp"
        android:divider="@drawable/divider"
        android:dividerHeight="8dp"
        android:listSelector="@color/Transparent"
        android:stackFromBottom="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false" >
    </ListView>

</FrameLayout>
现在这很好,我可以在测试项目中单击列表后面的按钮,但是,当我在原始项目中执行相同的操作时,它不起作用

任何人都可以告诉我为什么会发生这种事


谢谢

我认为您可能必须删除列表视图的on itemclicked listener,并设置个人视图的listener。为什么我需要列表中个人视图的listener,我不希望它们可以单击?以下是我已经尝试过的答案,以及该帖子中的所有其他答案。我尝试了每一个解决方案本身,并结合其他解决方案,似乎没有什么工作为我。当我尝试为列表设置setOnClickListener(null)时,我遇到一个异常,说我可能想要setOnItemClickListener(null),所以我尝试了,但也没有任何帮助。在父视图中将调用传递给同一个方法,这难道不像覆盖onTouch那么容易吗?
?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.listtest.MainActivity"
    android:background="@color/Blue">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click"
        android:layout_gravity="center"
        android:onClick="onClickTestButton"/>

    <ListView
        android:id="@+id/test_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="12dp"
        android:layout_marginLeft="8dp"
        android:divider="@drawable/divider"
        android:dividerHeight="8dp"
        android:listSelector="@color/Transparent"
        android:stackFromBottom="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false" >
    </ListView>

</FrameLayout>
myListAdapter = new MyListAdapater(this, items);
testList = (ListView) findViewById(R.id.test_list);
testList.setAdapter(myListAdapter);
testList.setOnItemClickListener(null);
testList.setEnabled(false);