阻止访问GridView

时间:2015-06-20 16:08:40

标签: java android gridview onclick

我有一个小记忆游戏,你可以连续点击2个图像。图像显示在GridView中。点击2次后,我将比较这些翻转的图像。如果它们不相同,则执行以下代码:

                else{
                    // new try
                    Handler mHandler = new Handler();
                    mHandler.postDelayed(update, 2000);
                }
                clickCounter = -1;
                }
}

private Runnable update = new Runnable() {
       public void run() {

         viewHolder[0].setImageResource(R.drawable.card_back);
         viewHolder[1].setImageResource(R.drawable.card_back);
       }
    };

因此,用户可以看到点击的图像2秒钟,然后他们将切换回卡片。但是如何在这两秒内阻止GridView / ImageView上的用户操作。否则,在没有比较的情况下,可以在这两秒内发现任意数量的图像。

我试过像:

gridview.setClickable(false);

gridview.setEnabled(false);

但我总是遇到错误。我很感激你的帮助。

ImageAdaper.java

final class ImageAdapter extends BaseAdapter {

    private final List<Item> mItems = new ArrayList<Item>();
    private final LayoutInflater mInflater;
    private int clickCounter = -1;
    private int countPairs = 1;
    private Context mContext;
    private ImageView[] viewHolder;
    private Integer[] pieces;

    public ImageAdapter(Context context) {

        mContext = context;
        viewHolder = new ImageView[2];
        List ipieces = new ArrayList();
        for(int i=0; i<6; i++) {
            ipieces.add(i);
            ipieces.add(i);
        }
        Collections.shuffle(ipieces);
        pieces = (Integer[]) ipieces.toArray(new Integer[0]);

        mInflater = LayoutInflater.from(context);

        mItems.add(new Item(R.drawable.pic_1));
        mItems.add(new Item(R.drawable.pic_2));
        mItems.add(new Item(R.drawable.pic_3));
        mItems.add(new Item(R.drawable.pic_4));
        mItems.add(new Item(R.drawable.pic_5));
        mItems.add(new Item(R.drawable.pic_6));

        Collections.shuffle(mItems);
    }

    @Override
    public int getCount() {
        return pieces.length;
    }

    @Override
    public Item getItem(int i) {
        return mItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return mItems.get(i).drawableId;
    }

    @Override
    public View getView(final int i, final View view, ViewGroup viewGroup) {
        View v = view;
        ImageView picture;

        if (v == null) {
            v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
            v.setTag(R.id.picture, v.findViewById(R.id.picture));
        }

        picture = (ImageView) v.getTag(R.id.picture);

        picture.setImageResource(R.drawable.card_back);
        picture.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                switchImage(i,v);
            }
        });    

        return v;
        }

    public void switchImage(int position, View view){

                ImageView Image = (ImageView) view;

                if(clickCounter == -1){
                    // first click on an image
                    // convert images to avoid OOME
                    convertImage(Image,position);
                    clickCounter = position;
                    viewHolder[0] = Image;
                    Log.d("ImageAdapter", "FIRST CLICK!!!!" + Integer.toString(position));
                }
                else{
                    //second click
                    viewHolder[1] = Image;
                    Log.d("ImageAdapter", "SECOND CLICK!!!!! " + Integer.toString(position));   
                    // convert images to avoid OOME
                    convertImage(Image,position);

                    //both pictures are matching
                    if(pieces[clickCounter]==pieces[position]){
                        Log.d("ImageAdapter", "MATCH!!!!!!!!!! ");
                        if(countPairs<6)Toast.makeText(mContext, "Du hast ein Paar gefunden!",Toast.LENGTH_SHORT).show();
                        deleteClicker();
                        countPairs++;
                    }
                    else{
                        // new try
                        Handler mHandler = new Handler();
                        mHandler.postDelayed(update, 2000);
                    }
                    clickCounter = -1;
                    }
    }

    private Runnable update = new Runnable() {
           public void run() {

             viewHolder[0].setImageResource(R.drawable.card_back);
             viewHolder[1].setImageResource(R.drawable.card_back);
           }
        };

        public void deleteClicker(){

            viewHolder[0].setEnabled(false);

            viewHolder[0].setOnClickListener(null);
            viewHolder[1].setOnClickListener(null);
            if(countPairs == 6){
                Toast.makeText(mContext, "Juhu! Du hast gewonnen",Toast.LENGTH_SHORT).show();
            }
        }

    public void convertImage(ImageView Image, int position){

        Item item = getItem(pieces[position]);
        BitmapFactory.Options options = new BitmapFactory.Options();
            options.inDither = false;
            options.inJustDecodeBounds = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            options.inSampleSize = 3;

            Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(),item.drawableId,options);
            Image.setImageBitmap(icon);
    }


    private static class Item {

        public final int drawableId;

        Item(int drawableId) {
            this.drawableId = drawableId;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试在switchImage或堆栈中的任何位置使用一些布尔值。像这样:

private boolean interactionEnabled = true; // code added
public void switchImage(int position, View view){
    if (!interactionEnabled) return; // code added
     //... your code here

    else{
        interactionEnabled = false; // code added
        Handler mHandler = new Handler(); 
        mHandler.postDelayed(update, 2000);
    }                  
}

private Runnable update = new Runnable() {
    public void run() {
         viewHolder[0].setImageResource(R.drawable.card_back);
         viewHolder[1].setImageResource(R.drawable.card_back);
         interactionEnabled = true; // code added
    }
};