在GridView中检查项目

时间:2015-10-01 12:03:11

标签: java android gridview

我想检查我在GridView中单击的每个项目,在我持续保持图片并开始onItemCheckedStateChanged之后..我的意思是我想以编程方式更改我要检查的每个图像的每个背景,我不知道怎么样... 这是我的GridViewAdapter:

public class GridViewAdapter extends BaseAdapter {
// Declare variables
ImageView image;
private Activity activity;
private String[] filepath;
private String[] filename;

private static LayoutInflater inflater = null;

public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
    activity = a;
    filepath = fpath;
    filename = fname;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


public int getCount() {
    return filepath.length;

}

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

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


public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.gridview_item, null);
    // Locate the TextView in gridview_item.xml
    TextView text = (TextView) vi.findViewById(R.id.text);
    // Locate the ImageView in gridview_item.xml
     image = (ImageView) vi.findViewById(R.id.grid_image);





    // Set file name to the TextView followed by the position
    File file = new File(filepath[position]);
    Picasso.with(activity).load(file).placeholder(R.drawable.rtrt).fit().centerCrop().into(image);


    // Decode the filepath with BitmapFactory followed by the position


    // Set the decoded bitmap into ImageView
  //  image.setImageBitmap(bmp);
    return vi;
}

}

这是onItemCheckedStateChanged的主要活动:

public class MultiChoiceModeListener implements
            GridView.MultiChoiceModeListener {
        @Override
        public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {

            selectCount = grid.getCheckedItemCount();
            int checkedItemPosition = grid.getCheckedItemPosition();

            switch (selectCount) {
                case 1:
                    mode.setSubtitle("One picture selected");

                    break;
                default:
                    mode.setSubtitle("" + selectCount + " pictures selected");
                    break;
            }
        }


        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
            mode.setTitle("Select pictures");
            mode.setSubtitle("One picture selected");


            return true;

        }

        @Override
        public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
            return true;
        }

        @Override
        public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {

            return true;
        }

        @Override
        public void onDestroyActionMode(android.view.ActionMode mode) {

        }
    }

我在这个问题上被困了2天,任何帮助都会被贬低!谢谢!

1 个答案:

答案 0 :(得分:0)

确保要为每个ImageView或TextView替换为您需要的不同参数 替换设置“隐形”的可见性,因此当您单击“项目”时,它将更改为“可见”。

XML对象如下所示:

<ImageView
android:id="@+id/grid_image"/>

<ImageView
android:id="@+id/check_grid_image"
android:visibility="invisible"/>

GridViewAdapter:

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

    ViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.inflate(R.layout.gridview_item, null);
        holder = new ViewHolder();
        holder.image = (ImageView)convertView.findViewById(R.id.grid_image);
        holder.checkImage = (ImageView)convertView.findViewById(R.id.check_grid_image);
        convertView.setTag(holder);
    }
    GridView gridView = (GridView)parent;
    if (gridView.isItemChecked(position)) {
        holder.checkImage.setVisibility(View.VISIBLE);
    }
    else {
        holder.checkImage.setVisibility(View.INVISIBLE);
    }
// Picasso omitted

    return convertView;
}

private static class ViewHolder {
    ImageView image;
    ImageView checkImage;
}

ActivityMain:

protected AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        ImageView checkImage = (ImageView)view.findViewById(R.id.check_grid_image

        if (mGridView.isItemChecked(position)) {
            // Action when Item checked
            checkImage.setVisibility(View.VISIBLE);
        }
        else {
            // Reverse Action
            checkImage.setVisibility(View.INVISIBLE);
        }
    }
};

这在我的项目中有效,但请告诉我这是否对您有用!