【问题标题】:Why are some listview items not getting selected?为什么某些列表视图项目没有被选中?
【发布时间】:2016-03-16 11:08:19
【问题描述】:

我有一个列表视图,它使用基本适配器和两个布局来显示项目。我已经在这个列表视图上实现了 multichoicemodellistener。

问题是当我长按列表视图项目时,有些项目被选中,而另一些则没有。我已经意识到被选中的那些正在使用与未被选中的不同的行布局。

被选中的列表视图行的布局。 >>>> LAYOUT 1

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:activatedBackgroundIndicator">


<LinearLayout
    android:id="@+id/chat_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:orientation="vertical">


    <com.x.y.SquareImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:layout_gravity="center" />

       <Textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#020202"
        android:textSize="17sp"
        android:textIsSelectable="false" />
</LinearLayout>

</RelativeLayout>

未选中的列表视图行的布局 >>>> LAYOUT 2

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:activatedBackgroundIndicator">


<LinearLayout
    android:id="@+id/chat_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginLeft="15dp">

    <Textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#020202"
        android:textSize="17sp"
        android:textIsSelectable="true" />
</LinearLayout>

</RelativeLayout>

我是如何实现多选模式监听器的

list_View.setMultiChoiceModeListener(new Selector());

选择器类

private class Selector implements AbsListView.MultiChoiceModeListener {


    @Override
    public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {

        mode.getMenuInflater().inflate(R.menu.chat_activity_menu, menu);
        return true;
    }

    @Override
    public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
        int checkedCount = list_View.getCheckedItemCount();
        Log.d("Selector","checked items are "+checkedCount);
        mode.setSubtitle(checkedCount+" selected");
    }

    @Override
    public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {

        return true;
    }

    @Override
    public void onDestroyActionMode(android.view.ActionMode mode) {
        adapter.removeSelection();
    }
}

为什么我的一些使用 LAYOUT 2 的列表视图项目没有被选中?每当我单击使用该布局的任何项目时,都不会发生选择,甚至不会打印出调试信息。

【问题讨论】:

  • 试试这个可能对你有帮助。 stackoverflow.com/questions/35765399/…
  • @AnandKumarJha 这是一个好方法,但在我的情况下是不可接受的。如果你能帮助解决其中的错误,我正在使用的方法是最好和合适的。
  • @AnandKumarJha 我已经意识到了这个问题。看看我下面的回答

标签: android android-layout listview


【解决方案1】:

我已经意识到是什么导致使用 LAYOUT 2 的列表视图项目无法选择。问题是我将 layout 2 中的 textview 文本设置为可选择的。

内部布局2

   <Textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#020202"
    android:textSize="17sp"
    android:textIsSelectable="true"
     />

我改成

<Textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#020202"
    android:textSize="17sp"
    android:textIsSelectable="false"
     />

更改为android:textIsSelectable="false"

并且所有使用此布局的列表视图项目都可以选择。

为什么它会影响列表视图项目选择?

根据安卓文档

当您调用此方法时(在 Xml 中 android:textIsSelectable="true" 或以编程方式textview.setTextIsSelectable(true)),它设置 将 focusable、focusableInTouchMode、clickable 和 longClickable 标记为 相同的值。

这意味着如果我设置 `android:textIsSelectable="true"':

  • android:longClickable 自动设置为 true 并根据 android 文档

SetLongClickable 启用或禁用此视图的长按事件。 当一个视图长时间可点击时,它会对用户按住 按钮的持续时间比点击时间长。此活动可以启动 侦听器或上下文菜单。

  • android:clickable 也自动设置为 true 并根据 android 文档

setClickable 启用或禁用此视图的点击事件。当一个 视图是可点击的,它会在每次点击时将其状态更改为“按下” 点击。

因此,长按和点击事件被 textview [它们传递给此 textview] 而不是 listview 消耗,因此无法使用此 textview 选择 listview 项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    相关资源
    最近更新 更多