动态更改自定义列表项背景

时间:2015-05-08 07:49:17

标签: android

我正在创建一个带有收藏功能的自定义列表视图,但我不知道如何在点击时更改喜欢的图像背景。当我只是更改喜欢图标的背景而不是在滚动时自动更改另一个喜欢的图像背景的背景。请查看以下代码:

public class CustomArrayAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater = null;
    ArrayList<Customlist> list;
    DecimalFormat formatter = new DecimalFormat("#,##,###");

public CustomArrayAdapter(Activity a, ArrayList<Customlist> list) {
    activity = a;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.list = list;

}

public int getCount() {
    return list.size();
}

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

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

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

    TextView txt_unit, txt_state, txt_price, term_left, customr;
    TextView install_date;
    final ImageView fav;
    View view = convertView;

    if (convertView == null)
        view = inflater.inflate(R.layout.list_item, null);

    customr = (TextView) view.findViewById(R.id.customr);
    txt_state = (TextView) view.findViewById(R.id.txt_state);
    install_date = (TextView) view.findViewById(R.id.install_date);
    term_left = (TextView) view.findViewById(R.id.term_left);
    txt_price = (TextView) view.findViewById(R.id.txt_price);
    fav = (ImageView) view.findViewById(R.id.fav);
    txt_unit = (TextView) view.findViewById(R.id.txt_unit);

    fav.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // fav = (ImageView)v.findViewById(R)

            fav.setBackgroundResource(R.drawable.favourite_select);

            Toast.makeText(activity, "click", 1).show();

        }
    });

    // set values
    customr.setText(list.get(position).getCUSTOMER());
    txt_state.setText(list.get(position).getSTATE_NAME());
    install_date.setText(list.get(position).getINSTALL_DATE());
    term_left.setText(list.get(position).getTREM_LEFT());
    String price = formatter.format(Integer.parseInt(list.get(position)
            .getRUPEES()));



    return view;
}
}

2 个答案:

答案 0 :(得分:1)

首先,您需要在ViewHolder模式上实现适配器:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHoldler holder = null;
    if (convertView == null) {
        convertView = LayoutInflater.from(ctx).inflate(
                R.layout.frag_home_gridview_item, null, false);
        holder = new ViewHoldler();
        holder.iv = (ImageView) convertView
                .findViewById(R.id.gridview_item_label);
        holder.tv = (TextView) convertView
                .findViewById(R.id.gridview_item_name);
        convertView.setTag(holder);
    } else {
        holder = (ViewHoldler) convertView.getTag();
    }
    holder.tv.setText(getItem(position));
    holder.iv.setImageResource(this.ids[position]);
    return convertView;
}

private class ViewHoldler {
    ImageView iv;
    TextView tv;
}

第二次,使用partial refreshment mechanism更改目标View的{​​{1}}:

background

第三次,将private void refreshPartially(int position){ int firstVisiblePosition = listview.getFirstVisiblePosition(); int lastVisiblePosition = listview.getLastVisiblePosition(); if(position>=firstVisiblePosition && position<=lastVisiblePosition){ View view = listview.getChildAt(position - firstVisiblePosition); if(view.getTag() instanceof ViewHolder){ ViewHolder vh = (ViewHolder)view.getTag(); //holder.play.setBackgroundResource(resId);//Do something here. ... } } } 添加到您的AdapterView.OnItemClickListener

ListView

答案 1 :(得分:0)

您需要在ViewHolder模式上实现适配器:

http://www.vogella.com/tutorials/AndroidListView/article.html