Android 如何使用ViewPager2在其他活动中滑动RecyclerView图像
我将图像从内部存储加载到RecyclerView,开始时我在另一个活动中成功加载了RecyclerView图像。Android 如何使用ViewPager2在其他活动中滑动RecyclerView图像,android,android-studio,android-recyclerview,viewpage,Android,Android Studio,Android Recyclerview,Viewpage,我将图像从内部存储加载到RecyclerView,开始时我在另一个活动中成功加载了RecyclerView图像。我想使用ViewPager,以便可以从一个图像滑动到另一个图像。我已经查看了许多教程,了解如何执行此操作,但它们都是关于如何通过将图像保存在drawable文件夹中并创建这些图像的数组来执行此操作的,就像在教程中一样。我还检查了与此相关的内容,但活动显示为空白,而不是一些图像。我需要关于如何做的指导或任何教程链接。以下是我到目前为止的代码:activity\u image.XML `&
我想使用ViewPager,以便可以从一个图像滑动到另一个图像。我已经查看了许多教程,了解如何执行此操作,但它们都是关于如何通过将图像保存在drawable文件夹中并创建这些图像的数组来执行此操作的,就像在教程中一样。
我还检查了与此相关的内容,但活动显示为空白,而不是一些图像。我需要关于如何做的指导或任何教程链接。
以下是我到目前为止的代码:
activity\u 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>`
<?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>
`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);
}
}*/
}
`
`
滑动图像.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>`
<?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>
`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);
}
}*/
}
ImageActivity.java
`<?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>`
<?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>
`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);
}
}*/
}
`package com.marke.statussaver2in1.activities;
导入androidx.appcompat.app.appcompat活动;
导入androidx.viewpager.widget.viewpager;
导入androidx.viewpager2.widget.viewpager2;
导入android.content.Intent;
导入android.content.pm.PackageInfo;
导入android.content.pm.PackageManager;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.view.view;
导入android.widget.ImageView;
导入android.widget.Toast;
导入com.bumptech.glide.glide;
导入com.marke.statussaver2in1.R;
导入com.marke.statussaver2in1.adapters.ImageSwiperAdapter2;
导入com.marke.statussaver2in1.model.StatusModel;
导入com.marke.statussaver2in1.utils.MyConstants;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.nio.channels.FileChannel;
导入java.util.ArrayList;
导入java.util.List;
公共类ImageActivity扩展了AppCompatActivity{
ImageView ImageView、saveBtn、repostBtn、shareBtn;
字符串image_path=“”,path=“”,package_name=“”;
私有ViewPager2 viewPager;
私有数组列表;
专用图像适配器2适配器2;
意图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u图像);
viewPager=findViewById(R.id.images\u View\u pager);
列表=新的ArrayList();
adapter2=新图像SwiperAdapter2(列表,此);
viewPager.setAdapter(适配器2);
intent=getIntent();
final int pos=getIntent().getIntExtra(“pos”,0);
viewPager.setCurrentItem(位置);
//imageView=findViewById(R.id.imageView);
saveBtn=findviewbyd(R.id.btn\u save);
repostBtn=findviewbyd(R.id.btn_repost);
shareBtn=findViewById(R.id.btn\U共享);
path=MyConstants.APP_目录;
package_name=“com.whatsapp”;
//忽略URI暴露
StrictMode.VmPolicy.Builder=新的StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
/*我以前是怎么装的
if(intent!=null){
image_path=intent.getStringExtra(“图像”);
if(图像路径!=null){
用(这个)滑动加载(图像路径)
.进入(图像视图);
}
}*/
}
`
StatusModel.java
`<?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>`
<?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>
`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);
}
}*/
}
package com.marke.statussaver2in1.model;
导入android.graphics.Bitmap;
导入java.io.File;
公共类状态模型{
私有静态最终字符串MP4=“.MP4”;
私人最终文件;
私有位图缩略图;
私有最终字符串标题、路径;
私人视频;
公共状态模型(文件文件、字符串标题、字符串路径){
this.file=文件;
this.title=标题;
this.path=path;
this.isVideo=file.getName().endsWith(MP4);
}
公共文件getFile(){
返回文件;
}
公共位图缩略图(){
返回缩略图;
}
公共空集缩略图(位图缩略图){
this.缩略图=缩略图;
}
公共字符串getTitle(){
返回标题;
}
公共字符串getPath(){
返回路径;
}
公共视频(){
返回isVideo;
}
公共视频(布尔视频){
isVideo=视频;
}
}
***ImageSwiperAdapter.java***
包com.marke.statussaver2in1.adapters;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入androidx.annotation.NonNull;
导入androidx.recyclerview.widget.recyclerview;
导入com.bumptech.glide.glide;
导入com.marke.statussaver2in1.R;
导入com.marke.statussaver2in1.model.StatusModel;
导入java.util.ArrayList;
公共类ImageSwiperAdapter扩展了RecyclerView.Adapter{
私有数组列表;
私人语境;
公共图像SwipAdapter(ArrayList列表,上下文){
this.list=列表;
this.context=上下文;
}
@非空
@凌驾
public ImageSwiper onCreateViewHolder(@NonNull ViewGroup父级,int viewType){
View=LayoutFlater.from(parent.getContext())。充气(R.layout.sliding_图像,
父母,假);
返回新的ImageSwiper(视图);
}
@凌驾
public void onBindViewHolder(@NonNull ImageSwiper holder,int位置){
Glide.with(context.getApplicationContext())
.load(list.get(position.getPath())
.插入(支架.图像视图);
}
@凌驾
public int getItemCount(){
返回list.size();
}
公共类ImageSwiper扩展了RecyclerView.ViewHolder{
私人影像视图;
公共ImageSwiper(@NonNull View itemView){
超级(项目视图);
imageView=itemView.findViewById(R.id.image\u视图);
}
}
}