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

时间:2015-02-14 12:29:58

标签: android listview android-listview

我有一个扩展BaseAdapter的自定义适配器。在getView()方法中,当我使用“position / index”来检查它是否是第一个和最后一个项目时,listview项目不会为每个项目呈现正确的背景可绘制,因为它是可循环使用的列表。因此,在滚动列表视图时,项目获得与index == 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;
}
}

3 个答案:

答案 0 :(得分:1)

写else条件为其他索引设置drawable而不是第一个和最后一个索引:

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);
}

答案 1 :(得分:0)

以这种方式尝试

    @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); 
} }

答案 2 :(得分:0)

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

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

    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));
    }