Android 如何根据列表项的内容更改其呈现?

Android 如何根据列表项的内容更改其呈现?,android,android-layout,colors,android-listview,android-xml,Android,Android Layout,Colors,Android Listview,Android Xml,我正在使用ListFragment和CursorAdapter来显示项目列表。XML布局非常简单:它只包含标题、描述和注释数 相反,我想在右侧显示评论的数量。如果可能,我想添加一个图像或彩色框作为背景框。此外,我想根据评论的数量更改图像/颜色 这是我当前使用的布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

我正在使用
ListFragment
CursorAdapter
来显示项目列表。XML布局非常简单:它只包含标题、描述和注释数

相反,我想在右侧显示评论的数量。如果可能,我想添加一个图像或彩色框作为背景框。此外,我想根据评论的数量更改图像/颜色

这是我当前使用的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" android:padding="@dimen/padding_small" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TextView>

    <TextView
        android:id="@+id/description"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TextView>

    <TextView
        android:id="@+id/comments_count"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TextView>

</LinearLayout>
下面是我根据Thiago Moreira Rocha的示例实现准备的颜色函数

if (comments_count == 0) {
    holder.comments_count.getParent().setBackgroundColor(Color.WHITE);
}
else if (comments_count != 0) {
    float saturation = (comments_count * 15) / 100.f;
    // The value gets pinned if out of range.
    int color = Color.HSVToColor(new float[] {110f , saturation, 1f});
    holder.comments_count.getParent().setBackgroundColor(color);
}
您将如何实现布局和上下文相关行为


注意:


我创建了第二个问题来讨论选择。如果您想向该主题添加信息,请参阅新帖子。

在适配器的
getView
方法中(在您的情况下,
bindView
)获取将绑定的视图的父视图,并使用
view.setBackGroundColor
更新其背景色

public void bindView(View view, Context context, Cursor cursor) {
   //usual stuff
   //...
   Color newColor = colorFunction(comments_count);//calculate your new color
   View v = (View)view.getParent();
   v.setBackgroundColor(newColor);
}
在本例中,我使用
ArrayAdapter
创建了一个列表,列表中有一个名称,后跟一个随机的数字。我将这个数字包装在一个
线性布局中
,并根据它的值更改其父布局

检查我下面的例子

public class MainActivity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ColorAdapter(this, R.layout.row, mockData));
}

String[] mockData = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6",
        "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12" };

public class ColorAdapter extends ArrayAdapter<String> {

    public ColorAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;

        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.row, parent, false);
        }

        TextView label = (TextView) row.findViewById(R.id.title);
        label.setText(mockData[position]);

        TextView desc = (TextView) row.findViewById(R.id.description);
        desc.setText("Description: "+ mockData[position]);

        TextView value = (TextView) row.findViewById(R.id.comments_count);

        //Here is the object you need to change the colors.
        LinearLayout background = (LinearLayout) value.getParent();

        Random random_number = new Random();
        int comments_count = random_number.nextInt(256);

        value.setText(comments_count+"");

        //Calculates a random color
        int newBackgroundColor = colorFunction(comments_count); 

        //Set the new background color on the comments_count parent
        background.setBackgroundColor(newBackgroundColor);

        return row;
    }

    private int colorFunction(int commentsNumber) {
        if (commentsNumber == 0) {
            return Color.WHITE;
        }
        else if(commentsNumber < 0) {
            return new Random().nextInt(256);
        }
        else {
            int color;
            Random rnd = new Random(); 
            color = Color.argb(commentsNumber, rnd.nextInt(commentsNumber), rnd.nextInt(256), rnd.nextInt(commentsNumber));

            return color;
        }
    }
}
public类MainActivity扩展了ListActivity{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(新的ColorAdapter(this,R.layout.row,mockData));
}
字符串[]mockData={“项目1”、“项目2”、“项目3”、“项目4”、“项目5”、“项目6”,
“项目7”、“项目8”、“项目9”、“项目10”、“项目11”、“项目12”};
公共类ColorAdapter扩展了ArrayAdapter{
公共颜色适配器(上下文,int textViewResourceId,
字符串[]对象){
超级(上下文、textViewResourceId、对象);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
if(行==null){
LayoutInflater充气机=getLayoutInflater();
行=充气机。充气(R.layout.row,父级,false);
}
TextView标签=(TextView)row.findViewById(R.id.title);
label.setText(mockData[position]);
TextView desc=(TextView)row.findViewById(R.id.description);
desc.setText(“说明:”+mockData[位置]);
TextView值=(TextView)row.findViewById(R.id.comments\u count);
//这是您需要更改颜色的对象。
LinearLayout背景=(LinearLayout)值。getParent();
随机数=新随机数();
int comments\u count=随机数.nextInt(256);
value.setText(注释数+“”);
//计算随机颜色
int newBackgroundColor=颜色函数(注释数);
//在注释上设置新的背景色
背景:setBackgroundColor(newBackgroundColor);
返回行;
}
私有int colorFunction(int commentsNumber){
如果(commentsNumber==0){
返回颜色。白色;
}
else if(注释编号<0){
返回新的Random().nextInt(256);
}
否则{
内色;
随机rnd=新随机();
color=color.argb(commentsNumber,rnd.nextInt(commentsNumber),rnd.nextInt(256),rnd.nextInt(commentsNumber));
返回颜色;
}
}
}
}

row.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="10dip" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.7" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/title"
            android:ellipsize="end"
            android:lines="1"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_weight="1"
        android:background="@drawable/border"
        android:orientation="vertical" >

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

            <TextView
                android:id="@+id/comments_count"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:padding="10dip"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

border.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="4dp" android:color="#000000" />
    <solid android:color="#ffffff" />
    <padding android:left="15dp" android:top="15dp"
            android:right="15dp" android:bottom="15dp" />
    <corners android:radius="4dp" /> 
</shape>

结果列表(纵向和横向)如下所示


更改外观或更改xml很简单。对于后台更改,请尝试重写适配器的getView()函数,该函数允许您控制单个行。有很多关于如何使用它的例子。希望这有帮助…这就是我在示例中的意思!在
bindView
方法中使用您的颜色函数来更新父对象的背景视图!!!我将更新我的答案。您的示例看起来很有希望,但是,我使用的是
CursorAdapter
而不是
ArrayAdapter
。我在问题中添加了相关代码。对不起,布局也是问题的一部分。请尝试构建我在问题中绘制的布局,好吗?我无法将箱子放在右侧。此外,当评论框左侧的文本长度发生变化时(类似于我从图片中看到的),它的位置也会发生变化。这看起来很不错。然而,我注意到盒子的宽度不是固定的。这意味着当文本比其他文本长时,它会改变其大小,或者我将设备置于横向模式。如果你想要一个固定值,你可以尝试这里建议的相同方法
<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="4dp" android:color="#000000" />
    <solid android:color="#ffffff" />
    <padding android:left="15dp" android:top="15dp"
            android:right="15dp" android:bottom="15dp" />
    <corners android:radius="4dp" /> 
</shape>