复选框,ImageView和GridView

时间:2015-11-25 19:44:37

标签: android gridview imageview

我有一个GridView,其中每个网格项都包含ImageViewCheckbox。现在,当我选择一个复选框时,让我们说数字5,另一个复选框(如数字12)会自动被选中。类似的取消选择。

我无法弄清楚为什么会这样。任何帮助将不胜感激。

 package com.example.vasylpaliy.mediaview;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.view.ActionMode;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.AbsListView;
 import android.widget.AdapterView;
 import android.widget.GridView;
 import android.widget.Toast;
 import com.adapter.ImageAdapter;
 import java.io.File;
  import java.util.ArrayList;
 public class ImageSelecter extends AppCompatActivity
    implements AdapterView.OnItemClickListener {
    private GridView view;
    private boolean colNum=false;
    private ArrayList<String> imagePaths;
    private final static int IMAGE_PATH_REQUEST=1;
    private ImageAdapter imageAdapter;
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.image_selector);
        view=(GridView)findViewById(R.id.gridView);
        imagePaths=getIntent().getStringArrayListExtra("images");
        getSupportActionBar().setHomeButtonEnabled(true);
        if(imagePaths!=null){
            setAdapter(false);
            setMultiChoiceMode();
            view.setOnItemClickListener(this);
        }
        setResult(RESULT_CANCELED);
    }
    private void setMultiChoiceMode(){
        view.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
        view.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            private ArrayList<String> imageItems=new ArrayList<String>();
            private boolean isChecked=false;
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                imageItems.add(imagePaths.get(position));
                mode.setTitle("Selected:"+Integer.toString(view.getCheckedItemCount()));
                if(!isChecked)
                    imageAdapter.setCheckMark();
                //imageAdapter.notifyDataSetChanged();
                Toast.makeText(getApplicationContext(),Integer.toString(position),Toast.LENGTH_SHORT).show();
            }
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                mode.getMenuInflater().inflate(R.menu.multiple_images, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch (item.getItemId())
                {
                    case R.id.multiple_image_select_all:
                    {
                        imageItems=imagePaths;
                        for(int i=0;i<imageItems.size();i++)
                            view.setItemChecked(i,true);
                        break;
                    }
                    case R.id.multiple_image_deleting:
                    {
                        if(imageItems!=null)
                            deleteItems(imageItems);
                        imageItems=null;
                        mode.setTitle("");
                        break;
                    }
                }
                return true;
            }
            @Override
            public void onDestroyActionMode(ActionMode mode) {
                Toast.makeText(getApplicationContext(),"Destroy",Toast.LENGTH_SHORT).show();
                setAdapter(false);
                //mode.finish();
            }
        });
    }
    private void setAdapter(boolean checked){
         imageAdapter=new ImageAdapter(this,imagePaths,
                R.layout.grid_item,R.id.grid_item_image,checked);
        view.setAdapter(imageAdapter);
    }
    private void deleteItems(ArrayList<String> imageItems){
        boolean isDeletingFolder=imagePaths.size()==imageItems.size();
        for(String image:imageItems){
            File file=new File(image);
            file.delete();
            imagePaths.remove(image);
        }
        setResult(RESULT_OK);
        if(isDeletingFolder)
            finish();
    }
    @Override
    public void onItemClick(AdapterView<?> parent,View view,
                            int position, long id){
        Toast.makeText(this, Integer.toString(position),Toast.LENGTH_LONG).show();
        Intent intent=new Intent(this,ImageSlider.class);
        intent.putStringArrayListExtra("images", imagePaths);
                intent.putExtra("position", position);
        startActivityForResult(intent, IMAGE_PATH_REQUEST);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode==IMAGE_PATH_REQUEST){
            if(resultCode==RESULT_OK){
                imagePaths=data.getStringArrayListExtra("images");
                setResult(RESULT_OK);
                setAdapter(false);
                setMultiChoiceMode();
            }
            else if(resultCode==RESULT_CANCELED){
                setResult(RESULT_OK);
                finish();
            }
        }
    }

这是我的适配器

package com.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import com.example.vasylpaliy.mediaview.R;
import com.squareup.picasso.Picasso;
import java.io.File;
import java.util.ArrayList;
public class ImageAdapter extends ArrayAdapter<String> {
    private LayoutInflater mInflater;
    private Picasso mPicasso;
    private int typeLayout;
    private int idImage;
    private boolean checked;
    public ImageAdapter(Context context, ArrayList<String> imagesPaths,
                        int typeLayout, int idImage, boolean checked) {
        super(context, typeLayout,imagesPaths);
        mInflater = LayoutInflater.from(context);
        mPicasso = Picasso.with(context);
        this.typeLayout=typeLayout;
        this.idImage=idImage;
        this.checked=checked;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = mInflater.inflate(typeLayout, parent, false);
        }
        ImageView imageView = (ImageView) view.findViewById(idImage);
        mPicasso.load(new File(getItem(position))).
                resizeDimen(R.dimen.image_width, R.dimen.image_size).
                centerCrop().into(imageView);
        if(checked){
            final CheckBox checkMark=(CheckBox)view.findViewById(R.id.checkImageMark);
            checkMark.setVisibility(View.VISIBLE);
           // checkMark.setButtonDrawable(R.drawable.checked24);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    checkMark.setButtonDrawable(!checkMark.isChecked() ? R.drawable.checked24_1 : R.drawable.checked24);
                    checkMark.setChecked(!checkMark.isChecked());
                }
            });
        }
        return view;
    }
    public void setCheckMark(){
        checked=!checked;
    }

}`

2 个答案:

答案 0 :(得分:0)

这是因为GridView缓存视图和OnClickListener未被清除。

您需要处理false案例

例如:

final CheckBox checkMark=(CheckBox)view.findViewById(R.id.checkImageMark);
    if(checked){
        checkMark.setVisibility(View.VISIBLE);
        // checkMark.setButtonDrawable(R.drawable.checked24);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkMark.setButtonDrawable(!checkMark.isChecked() ? R.drawable.checked24_1 : R.drawable.checked24);
                checkMark.setChecked(!checkMark.isChecked());
            }
        });
    } else {
        checkMark.setVisibility(View.GONE);
        imageView.setOnClickListener(null);
    }

答案 1 :(得分:0)

适配器视图在滚动时重用它的视图 因此,您需要一种方法来保存列表中已检查项目的状态,并在重用时清除视图 所以处理已检查和未检查的案例

if(checked){
    checkMark.setVisibility(View.VISIBLE);

} else {
    checkMark.setVisibility(View.GONE);

}

另外 我不认为已检查的变量应该是全局的,它应该是列表中每个项目的特定值