【问题标题】:Hide items in GridView if two items have same images using Android如果两个项目使用 Android 具有相同的图像,则在 GridView 中隐藏项目
【发布时间】:2014-03-13 09:49:05
【问题描述】:

我正在使用 GridView 开发一个项目,如果用户单击具有相同图像的这两个项目,则我需要隐藏项目,那么这两个项目都应该被隐藏。
第一次加载项目时,所有项目上都会有一个问号图像。 当用户单击项目时,背景图像将显示,然后如果他单击具有相同图片的另一个项目,则这两个项目都应该隐藏,如果不是,它会再次显示问号图像。
这怎么可能?
这是我的 MainActivity.java

public class MainActivity extends Activity {
    Context ctx;
    int imagesArray[];
    GridViewContent adapter;
    List<Integer> pictures;
    boolean flage=false;
    int img1=-1,img2=-1;
    public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3 };

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

        adapter= new GridViewContent(this);

        final GridView grid = (GridView) findViewById(R.id.gv_memory);
        grid.setAdapter(new GridViewContent(this));
    }
    private void shuffleArray() {
        // TODO Auto-generated method stub
        pictures= new ArrayList<Integer>();
        for (int index = 0; index < OriginalArray.length; index++)
        {
            pictures.add(OriginalArray[index]);
        }
        Collections.shuffle(pictures);

    }
    public class GridViewContent extends BaseAdapter {
        private Context context;

        //abstract change();

        public int pictureArray[]={
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,        
        };
        public GridViewContent(Context c){
            context=c;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return (pictureArray.length);
        }

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

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

        @Override
        public View getView(final int position, View convertView, ViewGroup arg2) {
            // TODO Auto-generated method stub
            convertView=LayoutInflater.from(context).inflate(R.layout.main, null);
            final ImageView myimage=new ImageView(context);
            myimage.setImageResource(pictureArray[position]);
            //myimage.setImageResource(pictures.get(pictureArray[position]));
            myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
            myimage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                //  int post1,post2;

                    myimage.setImageResource(pictures.get(position));

                if(flage==false)
                {

                img1=pictures.get(position);

                flage=true;

                }else if(flage==true){
                    //post2=position;
                img2=pictures.get(position);
                checkResult();
                //notifyDataSetChanged();
                flage=false;
                }
                //else if(f)
                }
            });

            return myimage;
        }

    }
    public void checkResult() {
        if(img1==img2)
        {
            //pictures.remove(post2);

            adapter.notifyDataSetChanged();

            Toast.makeText(MainActivity.this, "Congratualatin !!!!", Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(MainActivity.this, "Sorry!!!!", Toast.LENGTH_LONG).show();
            final GridView grid = (GridView) findViewById(R.id.gv_memory);
            grid.setAdapter(new GridViewContent(this));
        }

    }
}

【问题讨论】:

    标签: java android android-layout gridview android-listview


    【解决方案1】:

    您可以尝试记住您单击的GridView 项目的索引。当第二次点击发生时,检查记住的索引。如果已填充(意味着用户已经单击了任意图像一次),则从适配器中提取两个可绘制对象并检查它们是否相等。如果是,请将它们从适配器中删除并更新。不要忘记重置记忆索引。

    在代码中,这可能看起来像这样:

    int firstClickIndex = -1;
    
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView <? > parent, View view, int position, long id) {
            if (-1 == firstClickIndex) {
                firstClickIndex = position;
            } else {
                final int fstDrawable = adapter.get(firstClickIndex);
                final int sndDrawable = adapter.get(position);
    
                if (fstDrawable == sndDrawable) {
                    // Remove stuff, then reset firstClickIndex.
                } else {
                    // Reset firstClickIndex.
                }
    
            }
        }
    });
    

    【讨论】:

    • 先生如何在运行时从gridview中删除这些项目,请帮助我。
    • 您在适配器中实现了一个remove(int index) 方法,该方法应该删除您的pictureArray 中的相应图片(您最好使用List)。最后,您调用 notifyDataSetChanged() 更新 GridView UI,您就完成了。
    • 先生,我尝试了很多次但没有解决问题,先生,如果您将重置和更新问题的代码发送给我,我将非常感谢您。
    • 尽你最大的努力,用你得到的新代码更新你的帖子,我们会从那里拿走它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多