0

我有ListView一个自定义适配器来提供自定义的View. 每个列表项都会“改变”它所在的屏幕一侧(就像您围绕垂直轴翻转它一样)。这是我正在谈论的一个例子:

在此处输入图像描述

照片中较深的灰色框覆盖了包含用户名和照片的aProfilePictureView和 a 。TextView如果用户未登录,则没有姓名和照片(如您在照片中的第二个列表项中所见)。正如您从照片中看到的那样,背景图像环绕视图中第一项和第三项的内容(从左侧开始到右侧结束)。问题是:面向另一个方向的项目忽略了我的“wrap_content”调用,并且正在“匹配”父视图(如您在项目二中所见)。我相信它正确地包装了内容,但是有没有办法从屏幕右侧开始并将内容包装到左侧?

这是XML该项目视图的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right|center_vertical">

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentRight="true"
    android:gravity="right" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.namespace.RobotoTextView
            android:id="@+id/list_item_user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:textStyle="bold"
            android:textColor="@color/white"
            custom:typeface="roboto_light"/>

        <com.namespace.RobotoTextView
            android:id="@+id/list_item_user_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="36sp"
            android:textStyle="bold"
            android:textColor="@color/waf_warm_yellow_orange"
            custom:typeface="roboto_bold"/>

    </LinearLayout>

    <com.facebook.widget.ProfilePictureView
            android:id="@+id/list_item_user_image"
            android:layout_width="69dp"
            android:layout_height="69dp"
            android:layout_margin="10dp" />

</LinearLayout>

那么,如何将视图正确对齐到右侧并让背景图像正确环绕其内容?

4

2 回答 2

1

为左侧和右侧创建单独的列表项布局。在您的适配器中...

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return position % 2;
}

public View getView(int position, View convertView, ViewGroup parent) {
    int viewType = getItemViewType(position);
    int layoutRes = viewType == 0 ? R.layout.list_item_left : R.layout.list_item_right;

    View row = inflater.inflate(layoutRes);
    /* ... */
}

基本上,您的适配器报告两种不同的视图类型,而您的 getView 在它们之间交替。(如果有其他逻辑来确定左与右,请在 中实现getItemViewType()。)

于 2013-04-26T01:20:06.163 回答
0

我的问题的答案很简单,在我的自定义适配器中,我根据它是偶数还是奇数列表视图项来设置背景图像资源。而不是这样做,我只需要制作一个图像视图并将该图像资源设置为背景图像,并使用android:layout_alignParentRight="trueandroid:layout_alignParentLeft="true" 属性将其设置到屏幕的正确一侧。

于 2013-04-26T17:26:08.607 回答