[英]I'm using RecyclerView and I want to randomly display images
我正在使用 RecyclerView 制作一個壁紙應用程序,我想知道如何進行隨機(shuffle),所以我正在尋找一種方法,但我找不到一個好的方法。 請讓我知道如何解決它。 先感謝您。
我分享我正在使用的來源。 我不是專業的開發者,但我是一個把應用作為一種愛好的人,所以我還缺乏很多。 對不起,謝謝。
主要活動
public class MainActivity extends AppCompatActivity {
FloatingActionButton option_01;
FloatingActionButton option_02;
FloatingActionButton option_03;
FloatingActionButton option_04;
static final int PERMISSIONS_REQUEST = 0x0000001;
//종료팝업 전면광고 추가
private static final String TAG = "ted";
TedAdmobDialog nativeTedAdmobDialog;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, getString(R.string.admob_app_id));
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
checkPermission();
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
RecyclerView recyclerView = findViewById(R.id.rv_images);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new SpacesItemDecoration());
ImageGalleryAdapter adapter = new ImageGalleryAdapter(this, WallPhoto.getWallPhotos());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
option_01 = findViewById(R.id.option_01);
option_01.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
startActivity(intent);
});
option_02 = findViewById(R.id.option_02);
option_02.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=Project+J+Lab"));
startActivity(intent);
});
option_03 = findViewById(R.id.option_03);
option_03.setOnClickListener(v -> {
Intent myintent = new Intent(Intent.ACTION_SEND);
myintent.setType("text/plan");
String shereBoday = "Your Boday Here";
String shereSub = "\"http://play.google.com/store/apps/details?id=" + getPackageName();
myintent.putExtra(Intent.EXTRA_SUBJECT, shereBoday);
myintent.putExtra(Intent.EXTRA_TEXT, shereSub);
startActivity(Intent.createChooser(myintent, "shere Using"));
});
option_04 = findViewById(R.id.option_04);
option_04.setOnClickListener(v -> {
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("plain/text");
String[] address = {"dhsthdwjd1@gmail.com"};
email.putExtra(Intent.EXTRA_EMAIL, address);
email.putExtra(Intent.EXTRA_SUBJECT, getPackageName());
email.putExtra(Intent.EXTRA_TEXT, "text");
startActivity(email);
});
}
@Override
public void onBackPressed() {
//종료팝업 전면광고 추가
nativeTedAdmobDialog = new TedAdmobDialog.Builder(MainActivity.this, TedAdmobDialog.AdType.NATIVE, getString(R.string.banner_ad_unit_id_native))
.setOnBackPressListener(new OnBackPressListener() {
@Override
public void onReviewClick() {
}
@Override
public void onFinish() {
finish();
}
@Override
public void onAdShow() {
log("onAdShow");
nativeTedAdmobDialog.loadNative();
}
})
.create();
nativeTedAdmobDialog.show();
}
//종료팝업 전면광고 추가
private void log(String text) {
Log.d(TAG, text);
}
class ImageGalleryAdapter extends RecyclerView.Adapter<ImageGalleryAdapter.MyViewHolder> {
private WallPhoto[] mWallPhotos;
private Context mContext;
public ImageGalleryAdapter(Context context, WallPhoto[] WallPhotos) {
mContext = context;
mWallPhotos = WallPhotos;
}
@Override
public ImageGalleryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View photoView = inflater.inflate(R.layout.item_photo, parent, false);
ImageGalleryAdapter.MyViewHolder viewHolder = new ImageGalleryAdapter.MyViewHolder(photoView);
return viewHolder;
}
@Override
public void onBindViewHolder(ImageGalleryAdapter.MyViewHolder holder, int position) {
WallPhoto WallPhoto = mWallPhotos[position];
ImageView imageView = holder.mPhotoImageView;
Picasso.get()
.load(WallPhoto.getUrl())
.resize(200, 320)
.centerCrop()
.into(imageView);
}
@Override
public int getItemCount() {
return (mWallPhotos.length);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView mPhotoImageView;
public MyViewHolder(View itemView) {
super(itemView);
mPhotoImageView = itemView.findViewById(R.id.iv_photo);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
WallPhoto WallPhoto = mWallPhotos[position];
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_TRAVEL_PHOTO, WallPhoto);
startActivity(intent);
}
}
}
}
private void checkPermission() {
PermissionListener permissionlistener = new PermissionListener() {
@Override
public void onPermissionGranted() {
}
@Override
public void onPermissionDenied(List<String> deniedPermissions) {
}
};
TedPermission.with(this)
.setPermissionListener(permissionlistener)
.setPermissions(
Manifest.permission.SET_WALLPAPER,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION)
.check();
}
}
牆圖
public class WallPhoto implements Parcelable {
public static final Creator<WallPhoto> CREATOR = new Creator<WallPhoto>() {
@Override
public WallPhoto createFromParcel(Parcel in) {
return new WallPhoto(in);
}
@Override
public WallPhoto[] newArray(int size) {
return new WallPhoto[size];
}
};
private String mUrl;
public WallPhoto(String url) {
mUrl = url;
}
protected WallPhoto(Parcel in) {
mUrl = in.readString();
}
public static WallPhoto[] getWallPhotos() {
return new WallPhoto[]{
new WallPhoto("https://i.pinimg.com/736x/55/42/b4/5542b4fce27fb35f586db30f22aa7a06.jpg"),
new WallPhoto("https://i.pinimg.com/736x/dd/46/07/dd4607dfa77d3954ebd3bc5514d3b160.jpg"),
new WallPhoto("https://i.pinimg.com/736x/b1/a6/d6/b1a6d66f1dc350a6eb07098b031a6a86.jpg"),
new WallPhoto("https://i.pinimg.com/736x/ea/43/16/ea43160566df2e94804dc8494abad75d.jpg"),
new WallPhoto("https://i.pinimg.com/736x/2c/9c/e1/2c9ce1ebebcdd9f94bffa7d3a75ba172.jpg"),
new WallPhoto("https://i.pinimg.com/736x/a7/0a/3d/a70a3d6f310f63df2556233466c0d1a5.jpg"),
new WallPhoto("https://i.pinimg.com/736x/54/b3/20/54b320780750126d3c2c014af78e2642.jpg"),
new WallPhoto("https://i.pinimg.com/736x/3f/cc/ec/3fccec232da73ea7efe334cdfa920085.jpg"),
new WallPhoto("https://i.pinimg.com/736x/3b/c1/9f/3bc19f6a11ce39f1ce534b800001e4e0.jpg"),
new WallPhoto("https://i.pinimg.com/736x/37/a8/6e/37a86eac7b57ad3f768771870aca68ca.jpg"),
};
}
public String getUrl() {
return mUrl;
}
public void setUrl(String url) {
mUrl = url;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(mUrl);
}
}
public void shuffle() {
int mWallPhotoSize = mWallphoto.length();
WallPhoto[] shuffleWallphoto = new WallPhoto[mWallPhotoSize];
boolean[] checkNumber = new boolean[mWallPhotoSize];
for (int i = 0; i < mWallPhotoSize; i++) {
int randomNumber = (int)(Math.random()*mWallPhotoSize);
if (!checkNumber[randomNumber]) {
checkNumber[randomNumber] = true;
shuffleWallphoto[i] = mWallphoto[randomNumber];
} else {
i--;
}
}
mWallphoto = shuffleWallphoto;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.