ListView - Highlight sub layout of item

后端 未结 1 1604
被撕碎了的回忆
被撕碎了的回忆 2021-01-23 08:06

I have been searching for the straight answer to this question for quite some time now. What I want to know is how to implement a custom highlight on a ListView that will only h

相关标签:
1条回答
  • 2021-01-23 08:29

    I found a solution, I don't know if it's the best solution, but it got what I wanted done.

    Put simply, I've found that adding the selector code inside the card_layout.xml file does not work. What you should do is create a layout xml file for each state that you wish to implement on the button or list item and embed that layout in the selector itself, like in the answer for this SO post here: Android Using layer-list for button selector.

    Since I didn't want to bother finding out colors and transparencies I would need to implement the background for three different states I've basically placed a RelativeLayout which lies over top of the entire list item to act as the select-able part of the item.

    The code for this is here:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="72dp">
    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/card_layout">
        <LinearLayout android:id="@+id/linearLayout_thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_vertical">
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:id="@+id/imageView_thumbnail"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:orientation="vertical"
            android:layout_toRightOf="@+id/linearLayout_thumbnail"
            android:gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView_title"
                android:paddingLeft="5dp"
                android:textSize="18dp"
                android:text="SOME TEXT" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView_subtitle"
                android:paddingLeft="5dp"
                android:textSize="12dp"
                android:text="SOME TEXT" />
        </LinearLayout>
    
        <TextView
            android:id="@+id/textView_extra"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="12dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="@drawable/list_item_selector"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>
    </FrameLayout>
    

    So, now this is working, screenshot here: Proper item selection.

    0 讨论(0)
提交回复
热议问题