Android: 更改列表视图中第一项和最后一项的可绘制背景?

Android: Change the background drawable of first and last items in a listview?

我有一个扩展 BaseAdapter 的自定义适配器。在 getView() 方法上,当我使用 "position/index" 检查它是否是第一个和最后一个项目时,listview 项目不会为每个项目呈现适当的可绘制背景,因为它是一个可回收列表。因此,在滚动列表视图时,项目获得与索引 == 0 相同的可绘制背景。

@override
void getView(int index, View convertview, Viewgroup parent) {
    if(index == 0) {
        row.setBackgroundDrawable(drawable, null, null, null);
    } else {
        row.setBackgroundDrawable(drawable2, null, null, null);
    }
}

我如何有效且高效地为他们实现这种不同的可绘制资源?

编辑: 在下面添加了适配器代码:

public class iAdapter extends BaseAdapter {
    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;
    private TextView listItem;

    public iAdapter(Activity a, String[] d) {
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

@Override
public View getView(int index, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if(convertView==null) {
        rowView = inflater.inflate(R.layout.custom_layout, null);
    }
    if(index == 0) {
        rowView.setCompoundDrawablesWithIntrinsicBounds(activity.getResources().getDrawable(R.drawable.custom_header_image, null, null, null);
    } else if(index == data.length-1) {
        rowView.setCompoundDrawablesWithIntrinsicBounds(activity.getResources().getDrawable(R.drawable.custom_header_image2, null, null, null);
    }

    listItem = (TextView) rowView.findViewById(R.id.custom_text);
    listItem.setText(data[index]);
    return rowView;
}

@Override
public int getCount() {
    return data.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}
}

以这种方式尝试

    @override 
private void getView(int index, View convertview, Viewgroup parent) { 
if(index == 0){ 
row.setBackgroundDrawable(drawable);
 }else if(index == get count()-1) { 
row.setBackgroundDrawable(drawable1);
 }else{ 
row.setBackgroundDrawable(drawable2); 
} }

写其他条件为其他索引而不是第一个和最后一个索引设置可绘制对象:

if(index == 0) {
rowView.setCompoundDrawablesWithIntrinsicBounds
       (activity.getResources().getDrawable(R.drawable.custom_header_image,
                                null,  null, null);
} else if(index == data.length-1) {
    rowView.setCompoundDrawablesWithIntrinsicBounds
      (activity.getResources().getDrawable(R.drawable.custom_header_image2, 
      null, null, null);
}else  {
    rowView.setCompoundDrawablesWithIntrinsicBounds
      (activity.getResources().getDrawable(other_drawable_id, 
      null, null, null);
}

试试这个代码,我希望你有解决方案,

我认为,它的位置有问题,所以您应该尝试这段代码。

    if(index == 0) {
        rowView.setCompoundDrawablesWithIntrinsicBounds(activity.getResources().getDrawable(R.drawable.custom_header_image, null, null, null));
    } else if((index+1) == data.length) {
        rowView.setCompoundDrawablesWithIntrinsicBounds(activity.getResources().getDrawable(R.drawable.custom_header_image2, null, null, null));
    } else { 
        rowView.setCompoundDrawablesWithIntrinsicBounds(activity.getResources().getDrawable(other_drawable_id, null, null, null));
    }