开发者

List items don't highlight uniformly when tapped

开发者 https://www.devze.com 2023-02-20 18:40 出处:网络
What would cause only a portion of a list view item to highlight when tapped? I\'m working on an Android app where the user navigates through a series of lists, and some of the list items don\'t high

What would cause only a portion of a list view item to highlight when tapped?

I'm working on an Android app where the user navigates through a series of lists, and some of the list items don't highlight properly. The list items have an image on the left followed by text. If the text in the item mostly fills the row, the entire row highlights when tapped. If the text is short, however, then only part of the row highlights when tapped. I'd really like the entire row to highlight regardless of the length of the text. My list item layout looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:id="@+id/zoneIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:paddingLeft="5dp">
    </ImageView>
    <TextView 
        android:id="@+id/itemtitle"
        android:layout_width="fill_parent"
        and开发者_如何转开发roid:textColor="@color/listItemColor" 
        android:textSize="14sp" 
        android:layout_gravity="top|left" 
        android:layout_height="fill_parent" 
        android:padding="13dp" 
        android:textStyle="bold">
    </TextView>
</LinearLayout>

I thought that setting the layout_width of the text view to fill_parent would cause the text view to fill the remainder of the item's width, and that the whole row should highlight. Is there something else I need to set to control the width of the overall item? What am I doing wrong?

Edit: I also have another problem which may be related: When I scroll the list (which normally has a white background), the area occupied by the list items turns black during the scrolling, and then appears normal again when scrolling stops. The black area is the same size as the area that would be highlighted (except that it's all the items rather than just one), which is to say that there are white spaces on the right side of some of the items while scrolling. I'd like to prevent the list from turning black while scrolling, but perhaps that's another question. I mention it because it makes me think that some list items aren't filling their parent, but I'm still not sure why.


The black background is controlled by android:cacheColorHint (XML) or ListView.setCacheColorHint (Java) -- it used for drawing a solid color underneath the list while scrolling for performance reasons.

The list item should highlight in full regardless of the size of the TextView since the LinearLayout fills the parent. However you should certainly change the height of the LinearLayout towrap_content` -- it doesn't make much sense for it to fill a parent since the height of a list (and therefore the maximum size of a list item) is unbounded.

The height of the TextView and ImageView should probably be set to wrap_content as well. See if those changes resolve your issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号