Android:在 OnItemClick 之后替换 GridView 数组中的图像 [英] Android: Replacing images in GridView array after OnItemClick

查看:18
本文介绍了Android:在 OnItemClick 之后替换 GridView 数组中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大致像这样的网格视图(每个图像最终都会有所不同):

I have a grid view that looks roughly like this (each image will be a different in the end):

当用户单击数组中的任何图像时,我希望该图像更改为:

When the user clicks any image in the array, I want that image to change to this:

如果他们再次点击它会变成这样:

If they click again it changes to this:

然后再次点击返回:

到目前为止,这是我的代码,只是使用 Imageadapter 创建了一个 GridView:

Here's my code so far, just creating a GridView with Imageadapter:

public class GridScroll extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            // CHANGE IMAGE HERE
            Toast.makeText(GridScroll.this, "" + position, Toast.LENGTH_SHORT).show();


        }
    });
}

}

还有:

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.lifestyle_5,R.drawable.lifestyle_6,
        R.drawable.lifestyle_7,R.drawable.lifestyle_8,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.lifestyle_1,R.drawable.lifestyle_2,
        R.drawable.lifestyle_3,R.drawable.lifestyle_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.lifestyle_1,R.drawable.lifestyle_2,
        R.drawable.lifestyle_3,R.drawable.lifestyle_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.lifestyle_1,R.drawable.lifestyle_2,
        R.drawable.lifestyle_3,R.drawable.lifestyle_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4,
        R.drawable.icon_4, R.drawable.icon_4, 
        R.drawable.icon_4, R.drawable.icon_4, 

};

推荐答案

为此,我们需要做两件事:

To do this, we need to do two things:

1.单击时更改项目的可绘制对象.onItemClick(...) 中,更改传递给您的 View 的可绘制对象.此视图将与您在适配器的 getView(...) 中创建的视图相同.

1. Change the drawable of the item when it is clicked. In onItemClick(...), change the drawable for the View that is passed to you. This View will be the same one that you created in getView(...) of your adapter.

<强>2.确保该项目下次出现在屏幕上时以正确的可绘制对象显示.为此,请跟踪每个项目的状态.每次在 getView(...) 中为项目创建视图时,为其状态分配正确的可绘制对象.

2. Make sure that the item is shown with the correct drawable the next time it comes on screen. To do this, keep track of the state of each item. Every time you make a view for the item in getView(...), assign it the correct drawable for its state.

这是一个例子.我假设 ImageAdapter 是 ArrayAdapter 的子类.如果没有,那么您将需要修改此代码以使用您正在执行的操作.

Here is an example. I am assuming ImageAdapter is a subclass of ArrayAdapter. If not, then you will need to modify this code to work with what you are doing.

把这些放在某处:

private static final int WHITE = 0;
private static final int TEAL = 1;
private static final int MAROON = 2;
private List<Integer> mStates = new ArrayList<Integer>();

这在你的 ImageAdapter 中:

// Map each state to its graphics.
private Map<Integer, Integer> mStateResources = new HashMap<Integer, Integer>();
mStateResources.put(WHITE, R.drawable.white);

public void add(...) {
    super.add(...);

    // The new item will start as white.
    mStates.add(WHITE);
}

public View getView(int position, View convertView, ViewGroup parent) {
    //ImageView image = ...

    // Set the correct image for the state of this item.
    int state = mStates.get(position);
    int resId = mStateResources.get(state);
    image.setImageResource(resId);
}

在您的 OnItemClickListener 中:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    // Change the image and state for this item.
    int nextState;
    switch(mStates.get(position)) {
    case WHITE:
        nextState = TEAL;
        break;
    case TEAL:
        nextState = MAROON;
        break;
    case MAROON:
        nextState = WHITE;
        break;
    }

    // Set the new state and image for this item.
    mStates.put(position, nextState);
    int resId = mStateResources.get(nextState);
    image.setImageResource(resId);
}

这篇关于Android:在 OnItemClick 之后替换 GridView 数组中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆