如何在android中的gridview滚动中保留复选框状态

时间:2014-06-03 12:43:32

标签: android android-gridview android-checkbox

  • 我正在做什么 ::我有gridview我正在显示图片我gridview的适配器有图像和checkbox
  • 正在发生的事情 ::当我选中复选框并向下滚动gridview向下&当我scrollback时,所选的checkbox就是 未选择的
  • 问题 ::如何防止此问题我需要在代码中进行哪些更改

grid_view_image_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/flag"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentRight="true"
        android:scaleType="centerCrop"
        android:background="#000000"
        android:padding="1dp" />

    <CheckBox
        android:id="@+id/ch_bx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="CheckBox" />

</RelativeLayout>

AdapterGridViewImage.java

public class AdapterGridViewImage extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public AdapterGridViewImage(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        //imageLoader = new ImageLoader(context);
    }

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

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

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

    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView flag;
        CheckBox ch_bx;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.grid_view_image_item, parent, false);
        resultp = data.get(position);
        flag = (ImageView) itemView.findViewById(R.id.flag);
        ch_bx=(CheckBox) itemView.findViewById(R.id.ch_bx);
        //Picasso.with(context).load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim()).into(flag);

         Picasso.with(context)
          .load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim())
          .resizeDimen(R.dimen.wanted_size,R.dimen.wanted_size).centerCrop().into(flag);

        return itemView;
    }
}

1 个答案:

答案 0 :(得分:0)

请修改您的代码。
1)我们必须使用ViewHolder类进行封装 2)我们必须设置复选框的标记值,这样当视图重新创建时,我们可以从标记中获取值。

 public AdapterGridViewImage(Context context,
                        ArrayList<HashMap<String, String>> arraylist) {
                    this.context = context;
                    data = arraylist;
                    //imageLoader = new ImageLoader(context);
                    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            }

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

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

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

            public View getView(final int position, View convertView, ViewGroup parent) 
            {
                final ViewHolder holder;
                if(convertView==null)
                {
                    holder=new ViewHolder();
                    convertView = inflater.inflate(R.layout.grid_view_image_item, parent, false);
                    holder.flag = (ImageView) convertView.findViewById(R.id.flag);
                    holder.ch_bx=(CheckBox) convertView.findViewById(R.id.ch_bx);
holder.ch_bx.setTag(false);
                    convertView.setTag(holder);
                }
                else
                {
                    holder=(ViewHolder)convertView.getTag();
                }

                resultp = data.get(position);

                if((Boolean)holder.ch_bx.getTag())
                {
                    holder.ch_bx.setChecked(true);
                }
                else
                {
                    holder.ch_bx.setChecked(false);
                }

                holder.ch_bx.setOnClickListener(new OnClickListener()
                {

                    @Override
                    public void onClick(View v)
                    {
                        if(holder.ch_bx.isChecked())
                        {
                            holder.ch_bx.setTag(true);
                        }
                        else
                        {
                            holder.ch_bx.setTag(false);
                        }
                    }
                });
                //Picasso.with(context).load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim()).into(flag);

                 Picasso.with(context)
                  .load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim())
                  .resizeDimen(R.dimen.wanted_size,R.dimen.wanted_size).centerCrop().into(holder.flag);

                return itemView;
            }
            class ViewHolder
            {
                ImageView flag;
                CheckBox ch_bx;
            }