0

I have a custom listview item like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:state_activated="true"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/listicon_imageview"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:scaleType="centerInside"
        android:clickable="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_alignTop="@+id/listicon_imageview"
        android:layout_toRightOf="@+id/listicon_imageview"
        android:text="Burnt offering"
        android:textStyle="bold"
        android:ellipsize="end"
        android:singleLine="true"
        android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>

Then I am setting Mulitple choice selection:

    getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);

In the bindItemViewHolder method of my CursorAdapter I set the Listener for the ImageView:

    itemViewHolder.icon.setImageDrawable(drawable);
    itemViewHolder.icon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = (int)view.getTag();
            mListView.setItemChecked(position, !isPositionChecked(position));
        }
    });

Once the ImageView is clicked, the whole list item should highlight, whereas once the item iteself is clicked, some other Activity will be started. This is similar to the Gmail App, where the user clicks the icons to select for delete. This is what I need, too.

The problem is, however, I cannot get the item to highlight upon click on the ImageView (the cursor/list positions are correct). How can I get to see the highlight on my entire custom list item?

tw-S
  • 579
  • 1
  • 5
  • 23

2 Answers2

0

By default, ListView selection mode is NONE. Try changing it to CHOICE_MODE_SINGLE:

listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

or in xml:

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice">
</ListView>
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • Sorry, I forgot the mention that I call getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL) programically already (I edited my post) – tw-S Feb 25 '15 at 12:45
0

I solved my problem by overriding getView in my Adapter:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views

    v.setBackgroundColor(Color.TRANSPARENT); //default color
    if (mSelection.get(position) != null) {
        v.setBackgroundColor(Color.LTGRAY);// this is a selected position so make it red
    }
    return v;
}

which is taken from Luksprog's answer in Multiple selection in custom ListView with CAB (I voted up).

Community
  • 1
  • 1
tw-S
  • 579
  • 1
  • 5
  • 23