如何使用ViewPager2在另一个活动中刷卡RecyclerView图像

问题描述

我将图像从内部存储器加载到RecyclerView,开始时我成功地在另一个活动中加载了RecyclerView图像。
我想使用ViewPager,以便可以从一个图像滑动到另一个图像。我已经检查了许多有关如何执行此操作的教程,但是它们都涉及如何通过将图像保存在drawable文件夹中并创建这些图像的数组来进行操作,就像在this教程中一样。
我还检查了this,这在某种程度上是相关的,但是活动显示空白而不是某些图像。我需要有关操作方法或任何教程链接的指导。
以下是到目前为止的代码

activity_image.XML

`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".activities.ImageActivity">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/images_View_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        />

</RelativeLayout>`


sliding_images.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        />

</RelativeLayout>


ImageActivity.java

`package com.marke.statussaver2in1.activities;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.marke.statussaver2in1.R;
import com.marke.statussaver2in1.adapters.ImageSwiperAdapter2;
import com.marke.statussaver2in1.model.StatusModel;
import com.marke.statussaver2in1.utils.MyConstants;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

public class ImageActivity extends AppCompatActivity {

    ImageView imageView,saveBtn,repostBtn,shareBtn;
    String image_path = "",path = "",package_name = "";
    private ViewPager2 viewPager;
    private ArrayList<StatusModel> list;
    private ImageSwiperAdapter2 adapter2;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);

        viewPager = findViewById(R.id.images_View_pager);
        list = new ArrayList<>();
        adapter2 = new ImageSwiperAdapter2(list,this);

        viewPager.setAdapter(adapter2);
        intent = getIntent();
        final int pos = getIntent().getIntExtra("pos",0);
        viewPager.setCurrentItem(pos);


        //imageView = findViewById(R.id.imageView);
        saveBtn = findViewById(R.id.btn_save);
        repostBtn = findViewById(R.id.btn_repost);
        shareBtn = findViewById(R.id.btn_share);

        path = MyConstants.APP_DIRECTORY;
        package_name = "com.whatsapp";

        //Ignore URI Exposure
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());


        /*how I loaded it prevIoUsly

       if (intent != null) {
            image_path = intent.getStringExtra("image");
            if (image_path != null) {
                Glide.with(this).load(image_path)
                        .into(imageView);

            }
        }*/

     }

`
StatusModel.java

package com.marke.statussaver2in1.model;

import android.graphics.Bitmap;

import java.io.File;

public class StatusModel {

    private static final String MP4 = ".mp4";
    private final File file;
    private Bitmap thumbnail;
    private final String title,path;
    private boolean isVideo;

    public StatusModel(File file,String title,String path) {
        this.file = file;
        this.title = title;
        this.path = path;
        this.isVideo = file.getName().endsWith(MP4);
    }

    public File getFile() {
        return file;
    }

    public Bitmap getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(Bitmap thumbnail) {
        this.thumbnail = thumbnail;
    }

    public String getTitle() {
        return title;
    }

    public String getPath() {
        return path;
    }

    public boolean isVideo() {
        return isVideo;
    }

    public void setVideo(boolean video) {
        isVideo = video;
    }
}

<br>***ImageSwiperAdapter.java***<br>

package com.marke.statussaver2in1.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.marke.statussaver2in1.R;
import com.marke.statussaver2in1.model.StatusModel;

import java.util.ArrayList;

public class ImageSwiperAdapter extends RecyclerView.Adapter<ImageSwiperAdapter.ImageSwiper> {

    private ArrayList<StatusModel> list;
    private Context context;

    public ImageSwiperAdapter(ArrayList<StatusModel> list,Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ImageSwiper onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sliding_images,parent,false);

        return new ImageSwiper(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageSwiper holder,int position) {

        Glide.with(context.getApplicationContext())
                .load(list.get(position).getPath())

                .into(holder.imageView);


    }


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

    public class ImageSwiper extends RecyclerView.ViewHolder {

        private ImageView imageView;


        public ImageSwiper(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.image_view);


        }


    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)