Android:在适配器中的触摸事件上更新 GridView 中的图像 [英] Android : Update Image in GridView on touch event in Adapter

查看:16
本文介绍了Android:在适配器中的触摸事件上更新 GridView 中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView 和附加的适配器.适配器在网格中填充图像.我已在 Activity 中将 setOnTouchLister 设置为 GridView 并仅在适配器中实现.在适配器中,我有一个 Integer[] imageIDs,其中包含添加的所有图像的资源 IDArrayListimgObjsArr 扩展了 ImageView &设置了其他属性.

I have a GridView and attached adapter to it. Adapter populates images in the grid. I have set setOnTouchLister to GridView in my Activity and implemented in the adapter only. In adapter I have an Integer[] imageIDs that contains resource ids of all images that are added & an ArrayList<ImageSourceObject> imgObjsArr that extends ImageView & has other properties set.

现在onTouch(),我想将所选图像的图像更改为其他图像.这是我的代码:在 onCreate 中的 Activity 中设置网格的适配器:

Now onTouch(), I want to change the image of the selected image to other one. Here's my code : SEtting adapter of grid in Activity in onCreate :

        // Set Objects in Game View
    gameView = (GridView) this.findViewById(R.id.game_gridView);
    gameObjAdapter = new GameObjectsAdapter(this, R.id.game_gridView, null);
    gameView.setAdapter(gameObjAdapter);

适配器:

public class GameObjectsAdapter extends BaseAdapter implements OnTouchListener {
    super();
    mContext = c;
    this.layoutResourceId = layoutResourceId;
    objects = data;
    gridViewResAdap = (GridView) mContext.findViewById(this.layoutResourceId);
    createObjectsArray();
    Log.d("GOA", "Created Objects Array");
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageSourceObject imgView;

    if (convertView == null) {
        imgView = new ImageSourceObject(mContext);
        imgView.setLayoutParams(new GridView.LayoutParams(20, 20));
        imgView.getScaleType();
        imgView.setScaleType(ScaleType.FIT_XY);
        imgView.setPadding(0, 0, 0, 0);
    } else {
        imgView = (ImageSourceObject) convertView;
    }

    // Chk status of touched of imgView & set image accordingly
    if (imgView.isTouched())
        imgView.setImage(R.drawable.droid_touched2);
    else 
        imgView.setImage(imageIds[position]);

    return imgView;
}   

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    int action = event.getActionMasked();
    float currentXPos = event.getX();
    float currentYPos = event.getY();
    int position = gridViewResAdap.pointToPosition((int)currentXPos, (int) currentYPos);

    // Key was Pressed Here
    if (action == MotionEvent.ACTION_UP) {
        if (position > 0) {
            // Get the object which is clicked
            ImageSourceObject isb = this.imgObjsArr.get(position);
            Log.d("GA", " Postion ID " + isb.getId() + " [] ID : " + imageIds[position]);

            // Change the status of touched & set image
            isb.setTouched(true);
            isb.setImage(R.drawable.droid_touched2); 

            // Update the ArrayList & Integer[] with this updated obj 
            this.imgObjsArr.set(position, isb);
            imageIds[position] = R.drawable.droid_touched2;
            Log.d("ISB", "++++ Object ID : " + isb.getId() + " [] ID : " + imageIds[position] + " ISB Touched :" + isb.isTouched());

            Toast.makeText(mContext, "Postion Pressed : " + (position+1), 
                Toast.LENGTH_SHORT).show();

            //this.gridViewResAdap.invalidate();
        }

        return true;
    }

    return false;
}

日志:

02-19 15:19:11.615: D/GA(2046):  Postion ID 2130837556 [] ID : 2130837556
02-19 15:19:11.617: D/ISB(2046): ++++ Object ID : 2130837559 [] ID : 2130837559 ISB Touched :true

日志说 Integer[] 中的对象 &ArrayList 都更新了 &有正确的价值观.毕竟,屏幕上的图像也不会更新.gridViewResAdap 也是从活动传递过来的网格对象.我也尝试在其上调用 invalidate() ,但没有结果.和我的 getView() 一样,我使用的是 imageIDs,所以我也保持更新.

The logs say that the object in Integer[] & ArrayList both are updated & have right values. After all this also the image is not updated on the screen. The gridViewResAdap is also the object of grid that is passed from the activity. I tried calling invalidate() on it also, but yet no results. As in my getView(), I am using imageIDs, so I have kept that also updated.

图像源对象:

public class ImageSourceObject extends ImageView {

public void setImage(int resourceId) {
    super.setImageResource(resourceId);
    this.setId(resourceId);
}

此外,onTouch() 调用 onPerformClick() 时出错,我不确定在哪里调用 &为什么要打电话.我还没有在适配器中实现 onClickListener.在这种情况下可以做什么&当事情在 onTouch() 中管理时,在 onClickListener 中写什么.

Also, onTouch() gives error to call onPerformClick(), I am not sure where to call & why to call. I have alos not implemented onClickListener in the adapter. What can be done in this case & what to write in onClickListener when things are manged in onTouch().

你能帮我知道为什么图像对象没有更新以及我哪里出错了吗?我以为我必须刷新网格,所以也调用了 invalidate(),但没有结果.

Can you help me know why the image object is not updating and where am I going wrong ? I thought I have to refresh the grid, so have also called invalidate(), but no results.

非常感谢任何帮助.

谢谢

推荐答案

在您的 ImageView 开始处理 TouchEvent 之后,它是唯一允许使用该触摸手势执行任何操作的视图.您希望您的 GridView 拦截 TouchEvent,检查 TouchEvent 的 X 和 Y 坐标(event.getX(); event.getY())并查看这些坐标是否落在您的 ImageView 对象之一的范围内.如果是,请在 ImageView 或可以触发您的 setImageDrawable() 方法的东西上设置一个标志.我能够在我的一个项目中实现类似的效果,但我必须创建一个自定义 GridView 类(public class yourGridView extends GridView),然后覆盖以下内容:

After your ImageView begins handling the TouchEvent, it is the only view allowed to do anything with that touch gesture. You want your GridView to intercept the TouchEvent, check the X and Y coordinates of the TouchEvent (event.getX(); event.getY()) and see if those coordinates fall within the bounds of one of your ImageView objects. If yes, set a flag on the ImageView or something that can trigger your setImageDrawable() method. I was able to achieve a similar effect in one of my projects but I had to create a custom GridView class (public class yourGridView extends GridView), then override the following:

@Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch(action) {
            case MotionEvent.ACTION_DOWN:
                Log.i(AGL, "InterceptAction = DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(AGL, "InterceptAction = MOVE");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.i(AGL, "InterceptAction = CANCEL");
                return false;
        }
        return true;  //returning true tells your main Activity that you want the custom GridView to handle this TouchEvent; It will then send the TouchEvent to your GridView's onTouchEvent() method for handling.
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch(action) {
            case MotionEvent.ACTION_MOVE:
                int xCoord = (int) event.getX();
                int yCoord = (int) event.getY();
                Log.i(AGL, "MOVE EVENT;" + "
" +  "Touch X = " + Integer.toString(xCoord) + "
" +
                "Touch Y = " + Integer.toString(yCoord));
                for(int i = 0; i < this.getChildCount(); i++) {
                    ImageView suspect = (ImageView) this.getChildAt(i);
                    if(suspect.getBounds().contains(xCoord, yCoord)) {
                        suspect.CHANGE_YOUR_IMAGE_HERE();
                        suspect.invalidate();
                    }
                }
                break;
        }

        return true;
    }

请注意:getBounds() 是我在自定义 ImageView 类中编写的方法.此方法返回一个 Rect 对象,该对象表示 ImageView 对象的边界框.您必须使用自己的逻辑来获取 ImageView 的边界.

PLEASE NOTE: getBounds() is a method I wrote in my custom ImageView class. This method returns a Rect object that represents the bounding box of the ImageView object. You will have to use your own logic for fetching the bounds of your ImageView.

另请注意:当我在我的项目中运行此逻辑时,我的 ImageView 在触摸手势期间快速更改图像.我还没有想出如何将其限制为每个 MotionEvent.ACTION_MOVE 一次图像更改.

ALSO NOTE: When I run this logic in my project, my ImageViews change images rapidly during the touch gesture. I haven't figured out yet how to limit it to one image change per MotionEvent.ACTION_MOVE.

我希望这会有所帮助.

这篇关于Android:在适配器中的触摸事件上更新 GridView 中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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