安卓:更换图像GridView控件数组OnItemClick后 [英] Android: Replacing images in GridView array after OnItemClick

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

问题描述

我有一个网格视图,看起来大致是这样的(每幅图像将在年底不同):

当用户点击任何图像阵列中,我想他的形象来改变这样的:

如果他们再点击它改变这样的:

然后再次单击恢复为:

下面是我的code,到目前为止,只创建与Imageadapter一个GridView:

 公共类GridScroll延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
    gridview.setAdapter(新ImageAdapter(本));
    gridview.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
            //改变形象在这里
            Toast.makeText(GridScroll.this,+位置,Toast.LENGTH_SHORT).show();


        }
    });
}
 

}

 公开查看getView(INT位置,查看convertView,ViewGroup中父){
    ImageView的ImageView的;
    如果(convertView == NULL){//如果它不回收,初始化一些属性
        ImageView的=新ImageView的(mContext);
        imageView.setLayoutParams(新GridView.LayoutParams(85,85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8,8,8,8);
    } 其他 {
        ImageView的=(ImageView的)convertView;
    }

    imageView.setImageResource(mThumbIds [位置]);
    返回ImageView的;
}

//引用到我们的形象
私人整数[] 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,

};
 

解决方案

要做到这一点,我们需要做两件事情:

1。更改项目的绘制被点击时。 onItemClick(...),改变绘制的传递给你的视图。此视图将是您在 getView(...)您的适配器。

创建的同一个

2。确保该项目显示了正确的绘制下一次谈到在屏幕上。 要做到这一点,跟踪每个项目的状态。每次你该项目的视图, getView(...),分配给它正确绘制它的状态。


下面是一个例子。我假设ImageAdapter是ArrayAdapter的一个子类。如果没有,那么你就需要修改这个code与你在做什么工作。

把这些地方:

 私有静态最终诠释白色= 0;
私有静态最终诠释TEAL = 1;
私有静态最终诠释栗色= 2;
私人列表<整数GT; mStates =新的ArrayList<整数GT;();
 

这正好在你的ImageAdapter:

  //地图每个国家它的图形。
私人地图<整数,整数GT; mStateResources =新的HashMap<整数,整数GT;();
mStateResources.put(白色,R.drawable.white);

公共无效添加(...){
    super.add(...);

    //新项目将开始为白色。
    mStates.add(白色);
}

公共查看getView(INT位置,查看convertView,ViewGroup中父){
    // ImageView的图像= ...

    //设置正确的图像为这个项目的状态。
    INT状态= mStates.get(位置);
    INT渣油= mStateResources.get(州);
    image.setImageResource(渣油);
}
 

在OnItemClickListener:

 公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
    //改变形象和国家为这个项目。
    INT nextState;
    开关(mStates.get(位置)){
    案例WHITE:
        nextState = TEAL;
        打破;
    案例TEAL:
        nextState =栗色;
        打破;
    栗色的情况下:
        nextState =白色;
        打破;
    }

    //设置新的状态和形象为这个项目。
    mStates.put(位置,nextState);
    INT渣油= mStateResources.get(nextState);
    image.setImageResource(渣油);
}
 

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:

And then clicking again reverts back to:

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();


        }
    });
}

}

And:

    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. 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. 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.


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.

Put these somewhere:

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>();

This goes in your 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);
}

In your 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);
}

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

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