在Gridview中更新图像的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Gridview中更新图像的问题相关的知识,希望对你有一定的参考价值。

我正在android中构建一个简单的洪水游戏,我正在努力使gridview或者更确切地说“ImageAdapterGridView”在点击时更新图像。当用户点击一个单元格时,游戏会记录单元格的颜色并使左上角的单元格颜色相同。问题是我无法重绘网格。我在网上找到的所有解决方案似乎都以不同的方式填充网格。

public class GameActivity extends AppCompatActivity {

public int clickedCell;

public String colour = "error";

public int[] numbers = new int[64];

public Context mContext;

public GridView androidGridView;

public Integer[] imageIDs = {
        R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
        R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
        R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
        R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
        R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
        R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
        R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
        R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
};

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

    Random random= new Random();

    for(int i = 0; i < numbers.length; i++) {
        int number= random.nextInt(6);
        numbers[i]= number;
    }

    for(int i =0; i < numbers.length; i++)  {
        if(numbers[i]==0)   {
            imageIDs[i] = R.drawable.blue;
        }
        else if(numbers[i]==1)   {
            imageIDs[i] = R.drawable.orange;
        }
        else if(numbers[i]==2)   {
            imageIDs[i] = R.drawable.pink;
        }
        else if(numbers[i]==3)   {
            imageIDs[i] = R.drawable.purple;
        }
        else if(numbers[i]==4)   {
            imageIDs[i] = R.drawable.red;
        }
        else if(numbers[i]==5)   {
            imageIDs[i] = R.drawable.yellow;
        }
    }

    androidGridView = (GridView) findViewById(R.id.gridview_android_example);
    androidGridView.setAdapter(new ImageAdapterGridView(this));

    androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent,
                                View v, int position, long id) {

            ImageAdapterGridView IAGV = new ImageAdapterGridView(mContext);

            clickedCell = position;
            if(numbers[clickedCell]==0) {
                imageIDs[0] = R.drawable.blue;
                colour = "blue";
                Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected.  It is " + colour, Toast.LENGTH_SHORT).show();
                IAGV.getView(0, null, parent);
            }
            else if(numbers[clickedCell]==1)    {
                imageIDs[0] = R.drawable.orange;
                colour = "orange";
                Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected.  It is " + colour, Toast.LENGTH_SHORT).show();
                IAGV.getView(0, null, parent);
            }
            else if(numbers[clickedCell]==2)    {
                imageIDs[0] = R.drawable.pink;
                colour = "pink";
                Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected.  It is " + colour, Toast.LENGTH_SHORT).show();
                IAGV.getView(0, null, parent);
            }
            else if(numbers[clickedCell]==3)    {
                imageIDs[0] = R.drawable.purple;
                colour = "purple";
                Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected.  It is " + colour, Toast.LENGTH_SHORT).show();
                IAGV.getView(0, null, parent);
            }
            else if(numbers[clickedCell]==4)    {
                imageIDs[0] = R.drawable.red;
                colour = "red";
                Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected.  It is " + colour, Toast.LENGTH_SHORT).show();
                IAGV.getView(0, null, parent);
            }
            else if(numbers[clickedCell]==5)    {
                imageIDs[0] = R.drawable.yellow;
                colour = "yellow";
                Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected.  It is " + colour, Toast.LENGTH_SHORT).show();
                IAGV.getView(0, null, parent);
            }
        }
    });
}

public class ImageAdapterGridView extends BaseAdapter {

    public ImageAdapterGridView(Context c) {
        mContext = c;
    }

    public int getCount() {
        return imageIDs.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView mImageView;

        if (convertView == null) {
            mImageView = new ImageView(mContext);
            mImageView.setLayoutParams(new GridView.LayoutParams(130, 130));
            mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mImageView.setPadding(1, 4, 1, 4);
        } else {
            mImageView = (ImageView) convertView;
        }
        mImageView.setImageResource(imageIDs[position]);
        return mImageView;
    }
}

}

这是我的主要课程。目前我正在尝试重新调用getView()类,希望这将重新绘制图像,但我不知道这是否可行。如果需要任何其他信息,请告诉我。

非常感谢。

答案

将适配器提取到变量中:

//// extract adapter into a variable instead of creating inline
// androidGridView.setAdapter(new ImageAdapterGridView(this));

ImageAdapterGridView adapter = new ImageAdapterGridView(this);
androidGridView.setAdapter(adapter);

然后你可以调用.notifyDataSetChanged刷新你的onclick里面的视图:

adapter.notifyDataSetChanged();

您可以在onClick方法的底部执行一次。


您还在Click处理程序中创建了第二个适配器:

ImageAdapterGridView IAGV = new ImageAdapterGridView(mContext);

这是完全创建一个新的适配器,所以你会有两个适配器,但一个没有连接到视图。


另一个小问题是它的名称有点令人困惑,因为它是一个适配器但命名为它是一个视图。我可能会把它命名为ImageGridViewAdapter

以上是关于在Gridview中更新图像的问题的主要内容,如果未能解决你的问题,请参考以下文章

如何在Android中加载带有动画的cardview GridView?

在Gridview中更新图像的问题

图像GridView不会失效

Android drawSelectorOnTop 与 GridView

片段中的网格视图

GridView - 片段 - LongClick