编程语言
首页 > 编程语言> > java – Android:在适配器中触摸事件的GridView中更新图像

java – Android:在适配器中触摸事件的GridView中更新图像

作者:互联网

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

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

        // 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()一样,我使用的是imageID,所以我保持了更新.

ImageSourceObject:

public class ImageSourceObject extends ImageView {

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

另外,onTouch()在调用onPerformClick()时出错,我不知道在哪里调用&为什么打电话.我在适配器中没有实现onClickListener.在这种情况下可以做些什么呢?当onTouch()中的东西被管理时,在onClickListener中写什么.

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

任何帮助都非常感谢.

谢谢

解决方法:

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

@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;" + "\n" +  "Touch X = " + Integer.toString(xCoord) + "\n" +
                "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的边界.

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

我希望这有帮助.

标签:invalidation,android,java,gridview,image
来源: https://codeday.me/bug/20191003/1849481.html