【问题标题】:My listView is not clickable but works when I press enter in emulator?我的 listView 不可点击,但当我在模拟器中按 enter 时可以工作?
【发布时间】:2017-05-24 00:42:12
【问题描述】:

不确定发生了什么,但是当我使用手机时,我的列表视图中的自定义适配器项目(一个图像视图和 2 个文本视图)不可点击。奇怪的是,如果我在模拟器中按下回车键,它会聚焦并点击进入下一个活动,这是我触摸它时想要做的事情。当我按下物理设备上的列表视图对象时,没有焦点或任何操作.我是 android 开发的新手,正在使用约束布局,而我一直在使用的教程使用相对布局。这是我的代码:

列表类:

 ProductListAdapter adapter = new ProductListAdapter(this, R.layout.content_list,products);

        //hook up listview to adapter
        ListView list = (ListView) findViewById(R.id.listView);
        list.setAdapter(adapter);

        //4. to click on idividual items and pass to details view
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //make an intent
                Intent intent = new Intent(ListActivity.this,DetailsActivity.class);

                startActivity(intent);

这是我的列表上下文布局中的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.admin.study2.ListActivity"
    tools:showIn="@layout/activity_list"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp"

    >



    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintVertical_bias="1.0"
        tools:choiceMode="singleChoice"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />


    <ImageView
        android:id="@+id/clothImage"
        android:layout_width="87dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toTopOf="@+id/priceText"
        app:layout_constraintLeft_toLeftOf="@+id/listView"
        app:layout_constraintTop_toTopOf="@+id/listView"
        tools:layout_constraintBottom_creator="1"


        tools:layout_constraintTop_creator="1" />

    <TextView
        android:id="@+id/nameText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="211dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="211dp"
        android:layout_marginTop="31dp"
        android:text=""
        app:layout_constraintLeft_toLeftOf="@+id/listView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintTop_creator="1"
        android:layout_marginEnd="8dp" />

    <TextView
        android:id="@+id/priceText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="56dp"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.69"
        app:layout_constraintLeft_toLeftOf="@+id/listView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/nameText"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_constraintTop_creator="1" />



</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 那么实际上你想要做什么你想让它可以点击还是不可以点击?
  • 是的,我想让它可点击
  • 不使用调试器时会发生什么?你是否卡在某个断点处?
  • 对不起,我只是在使用我的设备时没有调试我的困惑。在我的安卓手机上,我无法点击我的列表项。它支持能够集中注意力并可以点击并移动到下一个活动,但没有任何反应
  • 修复了 layout_width 和 height 必须设置为包裹内容才能正常工作的问题

标签: android xml listview


【解决方案1】:

修复了 layout_width 和 layout_height 都必须设置为在列表视图中包装内容的问题。在我之前,它们被设置为零

    tools:showIn="@layout/activity_list">



    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:descendantFocusability="blocksDescendants"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1"


        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp" />


    <ImageView
        android:id="@+id/clothImage"
        android:layout_width="87dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        app:layout_constraintBottom_toTopOf="@+id/priceText"
        app:layout_constraintLeft_toLeftOf="@+id/listView"


        app:layout_constraintTop_toTopOf="@+id/listView"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintTop_creator="1" />

    <TextView
        android:id="@+id/nameText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="211dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="211dp"
        android:layout_marginTop="31dp"
        android:text=""
        android:focusableInTouchMode="false"
        app:layout_constraintLeft_toLeftOf="@+id/listView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintTop_creator="1"
        android:focusable="false"/>

    <TextView
        android:id="@+id/priceText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="56dp"
        android:text=""
        android:focusableInTouchMode="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.69"
        app:layout_constraintLeft_toLeftOf="@+id/listView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/nameText"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_constraintTop_creator="1"
        android:focusable="false"
        />


</android.support.constraint.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多