0

I have a listview that uses base adapter and two layouts to display items. i have implemented multichoicemodelistener on this listview.

The issue is when i long press listview items, some are getting selected while others are not. i have realized that the ones getting selected are using a different row layout from the ones not getting selected.

layout of listview rows that are getting selected. >>>> 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 of listview rows that are not getting selected >>>> 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>

how i have implemented multichoice mode listener

list_View.setMultiChoiceModeListener(new Selector());

Selector class

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();
    }
}

Why are some of my listview items using LAYOUT 2 not getting selected? whenever i click any item using that layout, no selection occurs and even debug info isn't been printed out.

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74

1 Answers1

0

I have realized what was making listview items using LAYOUT 2 to be unselectable. the problem was that i had made textview's text in layout 2 to be selectable.

inside layout 2

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

i changed it to

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

change is android:textIsSelectable="false"

And all listview's items using this layout became selectable.

SO WHY DOES IT AFFECT LISTVIEW ITEM SELECTION?

According to android docs

When you call this method ( in Xml android:textIsSelectable="true" or programmatically textview.setTextIsSelectable(true)), it sets the flag focusable,focusableInTouchMode,clickable and longClickable to the same value.

This means if i set `android:textIsSelectable="true"':

  • android:longClickable is automatically set to true and according to android docs

SetLongClickable enables or disable long click events for this view. when a view is long clickable, it reacts to the user holding down the button for a longer duration than a tap. this event can either launch the listener or a context menu.

  • android:clickable is also automatically set to true and according to android docs

setClickable enables or disables click event for this view. when a view is clickable,it will change its state to "pressed" on every click.

Therefore longclicks, and click events get consumed by textview [they are passed to this textview] instead of listview thus listview item selection with this textview fails to occur.

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74