1

嗨,我正在扭动一个可检查的 gridView 及其工作。但不是它的意图(像往常一样)一切都像我想要的那样工作,除了在绘制复选框时存在延迟。

网格将变为真实,但图像不会绘制,直到我检查另一个网格。取消检查虚假陈述也是如此。

这可能是一件非常简单和愚蠢的事情,但我觉得我盯着它有点盲目。许多坦克。

   public class CheckableImageView extends ImageView implements Checkable {


    private boolean mChecked=false;

    public CheckableImageView(final Context context,
            final AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
        Toast.makeText(getBaseContext(), "toggle  "+mChecked,                       Toast.LENGTH_SHORT)
                .show();

    }

    @Override
    public boolean isChecked() {
        Toast.makeText(getBaseContext(), "checked", Toast.LENGTH_SHORT)
                .show();
        return mChecked;
    }

    @Override
    public void setChecked(final boolean checked) {
        if (mChecked == checked)
            return;
        mChecked = checked;
        refreshDrawableState();

    }

     @Override
     protected void onDraw(Canvas canvas) {
     super.onDraw(canvas);
     if(mChecked) {
     Bitmap check = BitmapFactory.decodeResource(
     getResources(), R.drawable.cecked);
     canvas.drawBitmap(check, 0,0, new Paint());

     }
     }

适配器///////////////////////////////////////////////// ///////////////7

             public class ImageAdapter extends BaseAdapter {
    private Context context;

    public ImageAdapter(Context c) {
        context = c;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        // return randomList.size();
        return 12;
    }

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

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

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


        CheckableImageView imageView;
        if (convertView == null) {
            imageView = new CheckableImageView(context, null);
            imageView.setLayoutParams(new GridView.LayoutParams(240, 240));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5, 5, 5, 5);

        } else {
            imageView = (CheckableImageView) convertView;


        }

        imageView.setImageResource(randomList.get(position));


        return imageView;
    }

}

OnItemClick/////////////////////////////////////

            @Override
        public void onItemClick(AdapterView parent, View v, int position,
                long id) {


              cv = (CheckableImageView) v;
              cv.toggle();
              cv.refreshDrawableState();

        }
    });
4

4 回答 4

0

切换后在 onClicklistener 中的图像视图上调用 invalidate()。完美运行,刚刚尝试过。

于 2014-11-09T12:03:48.333 回答
0

您需要致电:

notifyDataSetChanged();

当您更改与适配器相关的数据或信息时,在适配器上。这将导致 Grid 失效并根据您在 Adapter 的 getView() 方法中覆盖的内容重绘。

于 2013-05-19T17:14:07.787 回答
0

您可以通过在ImageAdapter.getView.

    public View getView(final int position, final View convertView, final ViewGroup parent) {
        final ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(getApplicationContext()) {
                @Override
                protected void onDraw(Canvas canvas) {
                    super.onDraw(canvas);
                    if(((GridView)parent).isItemChecked(position)) {
                        //...
                    }
                }
            };
        } else {
            imageView = (ImageView) convertView;
        }
        //...
    }
于 2013-10-25T20:20:28.843 回答
0

checkable 还需要覆盖 OnCreateDrawableState 以实现 drawable 需要更改并需要重绘。

请参阅使 RelativeLayout 可检查

于 2013-10-25T20:25:33.400 回答