当前位置: 首页 > 知识库问答 >
问题:

比较GridView图像项目点击在Android运行时

宇文元明
2023-03-14

我正在开发一个记忆板游戏。规则如下:首先,牌面朝下。当用户单击第一张卡时,它会翻转,当用户单击第二张卡时,它也会翻转,此时这两张卡会相互比较。如果它们是一样的,它们就被从棋盘上取下,如果不是的话,它们将再次面对下。

我有一个GridView和实现翻转和比较的卡片在"onItemclick",但我没有得到预期的结果,因为比较结果总是掩盖第二张卡片的翻转。

例如,如果这两张卡是匹配的,则在用户有机会查看第二张卡之前,只要单击第二张卡,它们就会消失,因此用户知道存在匹配。如果他们不匹配,在用户有机会看到第二张卡之前,他们都会再次被拒绝。我知道有一个更好的方法来完成这件事,但是我已经没有主意了。非常感谢你的帮助。代码如下:

public class MemoryGridViewActivity extends Activity{
    private static final String TAG="MemoryGridView";
    private static final Integer[] images = {R.drawable.image_0, R.drawable.image_1,R.drawable.image_0, R.drawable.image_1};
    private static final Integer coverImageId = R.drawable.back;
    List<Card> Cards;

    private GridView boardGridView;
    private CardAdapter mCardAdapter;
    private int numClick = 0;
    private int firstClick = -1;
    private int secondClick= -1;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Cards = new ArrayList<Card>();
    for (int i=0; i < images.length; i++){
        Card tmpCard = new Card(images[i]);
        Cards.add(tmpCard);
    }

    boardGridView = (GridView)findViewById(R.id.gridview);
    mCardAdapter = new CardAdapter(this, Cards);
    boardGridView.setAdapter(mCardAdapter);
    boardGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            numClick++;
            Card myCard = Cards.get(position);
            if (!myCard.getState()){
                myCard.setState(true);
                ImageView cardImage = (ImageView) view.findViewById(R.id.cardView);
                cardImage.setImageResource(myCard.getImageId());
            }

            if(numClick %2 != 0){
                firstClick = position;
                Log.i(TAG, "first card turned up. firstClick = "+firstClick+" secondClick = "+secondClick);

            }else{
                secondClick = position;
                Log.i(TAG, "second card turned up. firstClick = "+firstClick+" secondClick = "+secondClick);
            }
            //compare two cards
            if(firstClick != -1 && secondClick!= -1) {
                Card firstCard = Cards.get(firstClick);
                Card secondCard = Cards.get(secondClick);
                if(firstCard.getImageId()== secondCard.getImageId()){
                    Log.i(TAG,"two cards are the same");
                    parent.getChildAt(firstClick).setVisibility(View.GONE);
                    parent.getChildAt(secondClick).setVisibility(View.GONE);

                }else {
                    Log.i(TAG, "two cards are different");
                    firstCard.setState(false);
                    ImageView cardImage1 = (ImageView) parent.getChildAt(firstClick).findViewById(R.id.cardView);
                    cardImage1.setImageResource(coverImageId);

                    secondCard.setState(false);
                    ImageView cardImage2 = (ImageView) parent.getChildAt(secondClick).findViewById(R.id.cardView);
                    cardImage2.setImageResource(coverImageId);
                    firstClick = secondClick = -1;

                }
            }
        }
    });
}

这是卡片。java类

    public class Card{

     private static final String TAG="Card";
     private int imageId;
     private boolean state = false;

     public Card(int img_id){
         this.imageId = img_id;
     }

     public void setImageId (int img_id){
         this.imageId = img_id;
     }

     public int getImageId(){
         return imageId;
     }

     public boolean getState(){
         return state;
     }
     public void setState(boolean flag){
         this.state = flag;
     }

最后,这是CardAdapter。JAVA

public class CardAdapter extends BaseAdapter{
 private static final String TAG = "CardAdapter";
 private static final int PADDING= 8;
 private static final int WIDTH = 200;
 private static final int HEIGHT = 200;

 private Context mContext;
 private List<Card> mThumbIds;
 private int coverImageId = R.drawable.back;

public CardAdapter(Context c, List<Card> ids){
    this.mContext = c;
    this.mThumbIds = ids;
}

private class ViewHolder {
    ImageView thumbnail;
    TextView txtDesc;
}

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

@Override
public Object getItem(int position){
    return mThumbIds.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent){
        //Log.i(TAG, "Entered getView");
        ViewHolder holder = new ViewHolder();
        if (convertView == null){

        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        // Inflate the View for this Card from Card.xml
        convertView = mInflater.inflate(R.layout.card, null);
        convertView.setPadding(PADDING, PADDING, PADDING, PADDING);
        convertView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT));

        holder.thumbnail = (ImageView)convertView.findViewById(R.id.cardView);
        convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        //Fill in the Card data
        Card tmpCard = (Card) getItem(position);
        if (!tmpCard.getState()) {
            holder.thumbnail.setImageResource(coverImageId);
        }else{
            holder.thumbnail.setImageResource(tmpCard.getImageId());
        }

        return convertView;
    }

}

共有1个答案

姚煜
2023-03-14

取代

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        numClick++;
        Card myCard = Cards.get(position);
        if (!myCard.getState()){
            myCard.setState(true);
            ImageView cardImage = (ImageView) view.findViewById(R.id.cardView);
            cardImage.setImageResource(myCard.getImageId());
        }

        if(numClick %2 != 0){
            firstClick = position;
            Log.i(TAG, "first card turned up. firstClick = "+firstClick+" secondClick = "+secondClick);

        }else{
            secondClick = position;
            Log.i(TAG, "second card turned up. firstClick = "+firstClick+" secondClick = "+secondClick);
        }

        mCardAdapter.notifyDatasetChanged();

        if (firstClick != -1 && secondClick != -1) {
        new CountDownTimer(TIME_DELAY, DURATION_OF_TICK) {

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {

                Card firstCard = Cards.get(firstClick);
                Card secondCard = Cards.get(secondClick);
                if (firstCard.getImageId() == secondCard.getImageId()) {
                    Log.i(TAG, "two cards are the same");
                    parent.getChildAt(firstClick).setVisibility(View.GONE);
                    parent.getChildAt(secondClick).setVisibility(View.GONE);

                } else {
                    Log.i(TAG, "two cards are different");
                    firstCard.setState(false);
                    ImageView cardImage1 = (ImageView) parent.getChildAt(firstClick).findViewById(R.id.cardView);
                    cardImage1.setImageResource(coverImageId);

                    secondCard.setState(false);
                    ImageView cardImage2 = (ImageView) parent.getChildAt(secondClick).findViewById(R.id.cardView);
                    cardImage2.setImageResource(coverImageId);
                    firstClick = secondClick = -1;

                    mCardAdapter.notifyDatasetChanged();
                }
        }
    }.start();
   }

其中TIME_DELAY是要显示打开的卡片的持续时间。你可以把任何价值放在DURATION_OF_TICK。

 类似资料:
  • 我正在设计一个Gridview,其中每个项目都有一个TextView和ImageView。我试图找到关于如何使ImageView可点击以及GridView项目本身(两者都会触发不同的方法())的教程(逐步更新和清晰的教程)。 这就是我目前掌握的: Activity.java GV适配器.java 结果: 当Item1(Textview)被点击时,它应该点击整个Item并触发GridView的OnI

  • 问题内容: 如本教程所述,我在GridView中显示了图像。我希望能够单击单个图像并执行其他事件,所以我需要知道单击了哪个图像。 我必须在ImageAdapter类中添加imageView.onKeyDown(keyCode,event)吗?这是当前存在的代码: 如何显示点击的图片?如何创建适当的处理程序? 问题答案: 对于GridView,可以使用setOnItemClickListener方法

  • 我是Android Studio的新手,你能帮我吗?从下面的片段代码,我试着比较两个文本,其中一个是来自按钮,在点击test_ans按钮后,将转到'true'activity,如果他们是相同的。但不幸的是该应用程序被停止了:( 我试过delete@override public void onClick(View View),getText()变成红色,是onClick出了问题吗?请帮忙;(

  • 是否有一种方法可以将GridView中的项目与线/箭头(最好在背景中)连接起来? 我在GridView中拥有动态数量的项目(按钮),并且必须根据数据库的数据将某些按钮与其他按钮连接起来。 GridView很可能比屏幕大,因此包含线/箭头的视图必须与GridView同时滚动。 [问题]:Android:网络关系图更详细地描述了我的问题。

  • 问题内容: 与数据库中的图像进行比较的最佳方法是什么?我试图比较它们(@Image是Image类型): 但是收到错误“数据类型image和image在等于运算符中不兼容”。 问题答案: 由于IMO图像数据类型是二进制数据,并且存储数据的空间很大,因此比较图像字段的最简单方法是哈希比较。因此,您需要在表上存储“照片”列的哈希值。

  • D:\gatsby站点 盖茨比起动器-default@0.1.0开发D:\gatsby站点 盖茨比发展 错误 加载插件“gatsby plugin sharp”时出现问题。也许你需要安装它的软件包?使用--verbose查看实际错误。 错误#10226 CONFIG 找不到在“D:\gatsby site\gatsby config.js”中声明的“gatsby plugin sharp”插件。