如何更改列表视图中的图像?

时间:2016-03-04 06:33:30

标签: android listview

点击后我需要更改mt pic。

 public View getView(final int position, View itemListView, ViewGroup parent) {
        final ViewHolder holder;
        if (itemListView == null) {
            holder = new ViewHolder();
            itemListView = inflater.inflate(R.layout.lessons_single_item_details,null);

            itemListView.findViewById(R.id.btn_reading);
            holder.btn_reminders =(ImageView) itemListView.findViewById(R.id.btn_Reminders);

            itemListView.setTag(holder);
        }
        else {
            holder = (ViewHolder) itemListView.getTag();
        }      
        holder.btn_reminders.setFocusable(false);
        holder.btn_reminders.setFocusableInTouchMode(false);
        holder.btn_reminders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.btn_reminders.setBackgroundResource(R.drawable.ic_star);
            }
        });

        return itemListView;
    }

     private class ViewHolder{
        public ImageView btn_reminders;
    }

select image for change

after scroll

1 个答案:

答案 0 :(得分:0)

只需做一件事只需在你检查你的标签值之前点击项目时添加标签if if为true然后设置你选择的drawable else set unselect drawable

    public View getView(final int position, View itemListView, ViewGroup parent) {
    final ViewHolder holder;
    if (itemListView == null) {
        holder = new ViewHolder();
        itemListView = inflater.inflate(R.layout.lessons_single_item_details,null);

        itemListView.findViewById(R.id.btn_reading);
        holder.btn_reminders =(ImageView) itemListView.findViewById(R.id.btn_Reminders);

        itemListView.setTag(holder);
    }
    else {
        holder = (ViewHolder) itemListView.getTag();
    }
    if(holder.btn_reminders.getTag().equals(true))
    {
      holder.btn_reminders.setBackgroundResource(R.drawable.ic_star);
    }
    else
    {
      holder.btn_reminders.setBackgroundResource(R.drawable.ic_un_star);
    }      
    holder.btn_reminders.setFocusable(false);
    holder.btn_reminders.setFocusableInTouchMode(false);
    holder.btn_reminders.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            holder.btn_reminders.setBackgroundResource(R.drawable.ic_star);
            holder.btn_reminders.setTag(true);
        }
    });

    return itemListView;
}

 private class ViewHolder{
    public ImageView btn_reminders;
}