Android GridView动态setItemChecked

时间:2015-06-02 17:16:02

标签: android gridview

我有自定义Gridview。当Activity开始时,我想动态设置CheckedItem。我试过gridView.setItemChecked(5, true);,但它没有用。我该怎么办?

public class MainActivity extends Activity {
    GridView gridView;
    ArrayList<Item> gridArray = new ArrayList<Item>();
    CustomGridViewAdapter customGridAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //set grid view item
        Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.home);
        Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.personal);

        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"House"));
        gridArray.add(new Item(userIcon,"Friend"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"Personal"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"Building"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"xyz"));


        gridView = (GridView) findViewById(R.id.gridView1);
        customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);
        gridView.setAdapter(customGridAdapter);
        gridView.setItemChecked(5, true);
    }

适配器

public class CustomGridViewAdapter extends ArrayAdapter<Item> {
    Context context;
    int layoutResourceId;
    ArrayList<Item> data = new ArrayList<Item>();

    public CustomGridViewAdapter(Context context, int layoutResourceId,
            ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RecordHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new RecordHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
            holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
            row.setTag(holder);
        } else {
            holder = (RecordHolder) row.getTag();
        }

        Item item = data.get(position);
        holder.txtTitle.setText(item.getTitle());
        holder.imageItem.setImageBitmap(item.getImage());
        return row;

    }

    static class RecordHolder {
        TextView txtTitle;
        ImageView imageItem;

    }
}

物品

public class Item {
    Bitmap image;
    String title;

    public Item(Bitmap image, String title) {
        super();
        this.image = image;
        this.title = title;
    }
    public Bitmap getImage() {
        return image;
    }
    public void setImage(Bitmap image) {
        this.image = image;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}    

GridView的

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:columnWidth="80dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:listSelector="@drawable/selector_"
    android:choiceMode="singleChoice">
</GridView>

2 个答案:

答案 0 :(得分:0)

gridView.setItemChecked(5, true);确实有效!如果您想验证,可以使用以下API来询问checked项目位置,这将为您提供正确的值(在您的情况下为5)。

int i = gridView.getCheckedItemPosition();

现在,您可能对选中项目的某些直观表示感兴趣。但是你在getView(...)中的内容是TextViewImageView,并且两者都隐含地没有配备检查/取消选中功能。如果您明确使用某些Widgets,例如CheckBox implements Checkable界面,则可以清楚地看到差异!

enter image description here

有一篇非常好的帖子here肯定会帮助您实施。

答案 1 :(得分:0)

构建自定义适配器类,如下所示。如果要发送一些预先选择的,请将其传递给构造函数。

class CustomListAdapter extends BaseAdapter {
    Context context;
    static ArrayList<Object> items;
    ArrayList<Integer> imgTag;

    public CustomListAdapter(Context context, ArrayList<Object> items) {
        imgTag = new ArrayList<>();
        this.context = context;
        CustomListAdapter.items = items;
    }

    public void set_selected(int tag) {
        imgTag.add(tag);
    }

    public ArrayList<Integer> selected_items() {
        return imgTag;
    }

    public void remove_all() {
        imgTag = new ArrayList<>();
    }

    public void select_all() {
        remove_all();
        for (int i = 0; i < items.size(); i++) {
            imgTag.add(i);
        }
    }

    public void remove_selected(int tag) {
        for (int i = 0; i < imgTag.size(); i++) {
            if (tag == imgTag.get(i)) {
                imgTag.remove(i);
                break;
            }
        }
    }

    public boolean isSelected(int tag) {
        for (int i = 0; i < imgTag.size(); i++) {
            if (tag == imgTag.get(i)) {
                return true;
            }
        }
        return false;
    }

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

    @Override
    public View getView(int arg0, View convertView, ViewGroup arg2) {
        ImageView img;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.picture_block, null);
        if (convertView != null) {
            if (!this.isSelected(arg0)) {
                convertView.findViewById(R.id.check_img).setChecked(true);
            } else {
                convertView.findViewById(R.id.check_img).setChecked(false);
            }
            img = (ImageView) convertView.findViewById(R.id.original_img);
        } else {
            img = (ImageView) convertView.findViewById(R.id.original_img);
            View view = img;
            if (!this.isSelected(arg0)) {
                convertView.findViewById(R.id.check_img).setChecked(true);
            } else {
                convertView.findViewById(R.id.check_img).setChecked(false);
            }
            img = (ImageView) view;
        }
        convertView.setTag(arg0);
        img.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Ion.with(img)
                .placeholder(R.drawable.mw_background_100x100)
                .error(R.drawable.mw_background_100x100)
                .load(items.get(arg0).getFile());
        return convertView;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}