如何在imageview中添加滑动手势?

时间:2020-06-10 12:03:35

标签: java android android-layout android-recyclerview

请不要重复这个问题,因为我在任何地方都找不到解决方案。我正在制作一个Android应用程序,其中我从手机存储中提取图像并将其显示在recyclerview中,并且当用户单击任何图像时,该图像就会在imageView中打开。但是现在我想在imageview中添加滑动手势,实际上我已经添加了这些手势,但是我不明白如何在用户向右滑动时添加下一个图像,而在用户向左滑动时如何添加上一个图像。请帮我。任何类型的帮助将不胜感激。

我的GalleryAdapter代码:

public class GalleryRecyclerAdapter extends RecyclerView.Adapter<GalleryRecyclerAdapter.ViewHolder> {
Context context;
ArrayList<PhotoModel>photoModels;

public GalleryRecyclerAdapter(Context context, ArrayList<PhotoModel> photoModels) {
    this.context = context;
    this.photoModels = photoModels;
}

@NonNull
@Override
public GalleryRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.photo_layout, parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull GalleryRecyclerAdapter.ViewHolder holder, final int position) {
    Glide.with(context).load("file://" + photoModels.get(position).getStr_path()).skipMemoryCache(false).into(holder.imageView);
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, GalleryViewerActivity.class);
            i.putExtra("imagepath", photoModels.get(position).getStr_path());
            i.putParcelableArrayListExtra("photomodels", photoModels);
            context.startActivity(i);
        }
    });

}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    ImageView imageView;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.galleryimage);
    }
}
}

ImageViewerActivty:

public class GalleryViewerActivity extends AppCompatActivity {
String imgpath;
ImageView myImage;
private ScaleGestureDetector scaleGestureDetector;
private float mScaleFactor = 1.0f;
ArrayList<PhotoModel>photoModels;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery_viewer);
    imgpath = getIntent().getStringExtra("imagepath");
    photoModels = getIntent().getParcelableArrayListExtra("photomodels");
    myImage = (ImageView) findViewById(R.id.imageview);
    scaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
    File file = new File(imgpath);
    if(file.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        myImage.setImageBitmap(myBitmap);

    };
    myImage.setOnTouchListener(new OnSwipeTouchListener(GalleryViewerActivity.this) {
        public void onSwipeRight() {
            Toast.makeText(GalleryViewerActivity.this, "right", Toast.LENGTH_SHORT).show(); //I just want to ask what should i write here to add next image.
        }
        public void onSwipeLeft() {
            Toast.makeText(GalleryViewerActivity.this, "left", Toast.LENGTH_SHORT).show();  //and here previous image
        }

    });
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
    scaleGestureDetector.onTouchEvent(motionEvent);
    return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
        mScaleFactor *= scaleGestureDetector.getScaleFactor();
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
        myImage.setScaleX(mScaleFactor);
        myImage.setScaleY(mScaleFactor);
        return true;
    }
}

}

0 个答案:

没有答案