使用ImageViews的RecyclerView,放大触摸 [英] RecyclerView with ImageViews, zoom on touch

查看:192
本文介绍了使用ImageViews的RecyclerView,放大触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android很新,我遇到了这个问题。我有一个包含ImageViews的RecyclerView。我想要做的是,当我触摸某个项目时,它应该将图像放大到原始大小。 RecyclerView目前占据了屏幕的五分之一。我的适配器如下:

I am kind of new to Android, and I am stuck with this problem. I have a RecyclerView which holds ImageViews. What I want to do is, when I touch an item, it should enlarge the image to its original size. RecyclerView currently covers one-fifth of the screen. My Adapter is as follows:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnTouchListener {

    private ArrayList<Bitmap> pieces;
    private RecyclerViewOnTouchListener touchListener;

    public interface RecyclerViewOnTouchListener {
        void onTouch(View v, MotionEvent event);
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
    ImageView imageViewPiece;

    public MyViewHolder(View itemView) {
        super(itemView);
        this.imageViewPiece = (ImageView) itemView.findViewById(R.id.pieceImage);
        }
    }

    public MyAdapter(ArrayList<Bitmap> pieces, RecyclerViewOnTouchListener touchListener) {
        this.pieces = pieces;
        this.touchListener = touchListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Inflate layout for recycler view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view, parent, false);

        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
        ImageView imageView = holder.imageViewPiece;
        imageView.setOnTouchListener(this);
        imageView.setTag(listPosition);
        imageView.setImageBitmap(pieces.get(listPosition));
    }

    @Override
    public int getItemCount() {
        return pieces.size();
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView imageView = (ImageView) v.findViewById(R.id.pieceImage);

        if (touchListener != null) {
            touchListener.onTouch(imageView, event);
        }
        return false;
    }

}

我片段中的相关部分class如下:

And the related part in my fragment class is as following:

MyAdapter.RecyclerViewOnTouchListener onTouchListener = new MyAdapter.RecyclerViewOnTouchListener() {

    @Override
    public void onTouch(ImageView v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_DOWN:
                // Enlarge image to its original size and remove it from the list, also it should be able to move in the recycler view.
                break;
            case MotionEvent.ACTION_UP:
                // Put image back to its position at list
                break;
            default:
                break;
        }
    }
};

我认为它的逻辑应该是这样的,但我卡住了。我希望我已经写了足够的关于这个问题的信息。

I suppose its logic should be something like this but I am stuck. I hope I have written enough information about the problem.

我的片段如下所示: http://i.imgur.com/alEG7OP.png

我想触摸右边的一块我这样做,它应该放大到原来的大小,当我停止触摸时,它应该返回到与其他大小相同的列表。我也希望能够移动它。

I want to touch a piece on the right and when I do it, it should enlarge to its original size and when I stop touching, it should go back to the list with the same size as the others. I also want to be able to move it.

推荐答案

首先做一些小修正:

从onBindViewHolder()中删除这一行:

Remove this line from onBindViewHolder():

 imageView.setOnTouchListener(this);

将其改为MyViewHolder:

Put it instead in MyViewHolder:

this.imageViewPiece.setOnTouchListener(this);

因为你只需要初始化监听器1,而不是每次绑定调用。

Because you just need to initialize the listener 1, not every bind call.

更改你的OnTouchListener:

Change your OnTouchListener:

@Override
 public boolean onTouch(View v, MotionEvent event) {
    if (touchListener != null) {
        touchListener.onTouch((ImageView) v, event);
    }
    return false;
}

由于您知道在onTouch中收到的物品是您注册的物品。

Since you know that the item you receive in the onTouch is the one you register.

那么你可以这样做,你可以做一些调整:

Then you can do something like that, you may do some adjusments:

MyAdapter.RecyclerViewOnTouchListener onTouchListener = new MyAdapter.RecyclerViewOnTouchListener() {
    Bitmap bitmap;
    int originalPosition, width, height;
    ImageView imageView;
    RelativeLayout.LayoutParams params;
    @Override
    public void onTouch(ImageView v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                int x = (int)event.getRawX();
                int y = (int) event.getRawY();
                params.leftMargin =  x - width / 2;
                params.topMargin =  y - height / 2;
                imageView.setLayoutParams(params);
                break;
            case MotionEvent.ACTION_DOWN:
                originalPosition = (int)v.getTag();
                bitmap = recycleAdapter.pieces.remove(originalPosition);
                recycleAdapter.nottifyDataSetChanged();
                width = bitmap.getWidth(); //maybe you need the drawable to get the intrinsic original dimens
                height = bitmap.getHeight();
                imageView = new ImageView(v.getContext());
                params = new RelativeLayout.LayoutParams(width, height);
                imageView.setImageBitmap(bitmap);
                RelativeLayout container; //here you need to bind this view to your container of fragment, better if it RelativeLayout to do dragging
                container.addView(imageView, params);
                // what you need to do is inflate here
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                container.removeView(imageView);
                recycleAdapter.pieces.add(originalPosition, bitmap);
                recycleAdapter.nottifyDataSetChanged();
                break;
            default:
                break;
        }
    }


};

这篇关于使用ImageViews的RecyclerView,放大触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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