java - 如何为 Android 中的 ListView 的每个项目设置背景图片?

标签 java android listview templates

<?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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:src="@drawable/icon" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textStyle="bold"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/bottomtext"
            android:singleLine="false"
        />
    </LinearLayout>
</LinearLayout>

这是我当前的行。如果我创建了一个 .JPEG,并且我希望它适用于每个项目...我将如何更改此 .xml 文件?我会将图像放在哪里?在 Assets 中?

最佳答案

如果您希望每个列表项都有单独的背景,您必须声明您自己的自定义适配器。

它从 BaseAdapter 派生,最重要的部分是 getView(int, View, ViewGroup) 方法。

您必须了解 android 如何在您滚动列表时重新使用已经存在的列表项 View 元素。这意味着:在任何时刻,只会生成与屏幕上同时显示的 View 一样多的 View 。

这种不生成太多 View 的最佳策略会导致一个问题,即在调用 getView 时,您必须根据每个列表项的当前位置为其设置背景。如果您尝试仅在生成 View 时静态设置背景,它会再次出现,可能附加到错误的元素。

getView 方法要么将“convertView”作为它的第二个参数,要么不带(null)。如果调用您的方法时将 convertView 设置为某物,则意味着:“为现在需要的项目重新使用此 View ”。

此处使用的技术在 API 演示(列表部分)中有很好的描述,并且还有一个视频博客。

这是可能的做法:

public class MyAdapter extends BaseAdapter {

    public View getView(int position, View convertView, ViewGroup parent) {
        // position is the element's id to use
        // convertView is either null -> create a new view for this element!
        //                or not null -> re-use this given view for element!
        // parent is the listview all the elements are in


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.your_layout, null);

            // here you must do whatever is needed to populate the elements of your
            // list element layout
            ...
        } else {
            // re-use the given convert view

            // here you must set all the elements to the required values
        }

        // your drawable here for this element 
        convertView.setBackground( ... );

        // maybe here's more to do with the view
        return convertView;
    }
}

基本上就是这样。如果只有几张背景图,我可能也会将它们缓存起来,这样您就不必一遍又一遍地阅读资源!

玩得开心!

关于java - 如何为 Android 中的 ListView 的每个项目设置背景图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2272193/

相关文章:

Android ViewBinding 与 CustomView

android - 如何更改选定的列表项背景,android

sql - 有没有办法在 ListView 中将表的两列显示为两行?

java - Java 编译是如何工作的?

java - Hibernate、MySQL 和名为 "Repeat"的表 - 奇怪的行为

android - 我可以将图像设置为布局底部的背景吗

android - 来自服务的持久性 android 通知

wpf - wpf中的列表框拖放

java - Hibernate @ManyToOne Collection 不会动态更新

java - 如何在springframework中查找注解的文档