Skip to content
This repository has been archived by the owner on Jan 22, 2023. It is now read-only.

Commit

Permalink
修复不加载所有图片的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zerorooot committed Mar 12, 2021
1 parent 695a7a8 commit 056b64f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
package github.zerorooot.sixpan.activity;


import androidx.annotation.RequiresApi;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.view.WindowManager;

import java.util.ArrayList;

import github.zerorooot.sixpan.R;
import github.zerorooot.sixpan.adapter.PictureAdapter;
import github.zerorooot.sixpan.bean.FileBean;
import github.zerorooot.sixpan.customizeActivity.ZoomageView;
import github.zerorooot.sixpan.databinding.ActivityPictureBinding;
import github.zerorooot.sixpan.viewModel.FileViewModel;

public class PictureActivity extends AppCompatActivity {
public ActivityPictureBinding binding;
private PictureActivity pictureActivity;
private int start = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityPictureBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

pictureActivity = this;
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("bundle");
ArrayList<FileBean> photoList = bundle.getParcelableArrayList("list");
int position = bundle.getInt("position");
start = bundle.getInt("start");

String token = bundle.getString("token");
FileViewModel fileViewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this)).get(FileViewModel.class);
Expand All @@ -51,14 +41,22 @@ protected void onCreate(Bundle savedInstanceState) {
adapter.setActivity(this);
adapter.setFileViewModel(fileViewModel);
adapter.submitList(photoList);

binding.viewPager.setAdapter(adapter);
binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
binding.textViewItem.setText((position + 1) + "/" + photoList.size());
binding.textViewTitle.setText(photoList.get(position).getName());
//load more
if ((position + 1) == photoList.size()) {
fileViewModel.getPictureFile(photoList.get(position).getParentPath(), start, fileViewModel.getLimitCount()).observe(pictureActivity, fileBeans -> {
photoList.addAll(fileBeans);
adapter.submitList(photoList);
binding.textViewItem.setText((position + 1) + "/" + photoList.size());
start = start + photoList.size();
});
}
}

});
Expand All @@ -67,6 +65,4 @@ public void onPageSelected(int position) {
binding.viewPager.setCurrentItem(position, false);

}


}
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package github.zerorooot.sixpan.adapter;

import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
Expand Down Expand Up @@ -71,8 +66,19 @@ public void onBindViewHolder(@NonNull PictureViewHolder holder, int position) {
.load(s)
.placeholder(R.drawable.ic_baseline_image_24)
.thumbnail(0.1f)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
holder.swipeRefreshLayout.setRefreshing(false);
return false;
}
})
.into(holder.pagerPhoto);
holder.swipeRefreshLayout.setRefreshing(false);

});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,16 @@ private void viewPhoto(FileBean fileBean) {
break;
}
}
viewPhoto((ArrayList<FileBean>) photoList, position);
viewPhoto((ArrayList<FileBean>) photoList, position, liveData.getValue().size());

}

private void viewPhoto(ArrayList<FileBean> photoList, int position) {
private void viewPhoto(ArrayList<FileBean> photoList, int position, int start) {
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("list", photoList);
bundle.putString("token", fileViewModel.getToken());
bundle.putInt("position", position);
bundle.putInt("start", start);
Intent intent = new Intent(requireActivity(), PictureActivity.class);
intent.putExtra("bundle", bundle);
startActivity(intent);
Expand Down Expand Up @@ -867,7 +868,7 @@ public void forceReadText(FileBean fileBean, TextDialog textDialog) {
public void forceViewImage(FileBean fileBean) {
ArrayList<FileBean> fileBeanList = new ArrayList<>();
fileBeanList.add(fileBean);
viewPhoto(fileBeanList, 1);
viewPhoto(fileBeanList, 1, 2);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,59 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
});
}

/**
* 获取图片·
*
* @param parentPath 文件路径
*/
public MutableLiveData<List<FileBean>> getPictureFile(String parentPath, int start, int limit) {
MutableLiveData<List<FileBean>> liveData = new MutableLiveData<>();
String url = ApiUrl.LIST;
JsonObject bodyJson = new JsonObject();
bodyJson.addProperty("parentPath", parentPath);
bodyJson.addProperty("limit", limit);
bodyJson.addProperty("start", start);
network(bodyJson, url).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
errorLog("error-getFile", e);
new Handler(Looper.getMainLooper()).post(() -> {
Toast.makeText(getApplication(), "网路连接失败,请重试!!!", Toast.LENGTH_SHORT).show();
});
}

@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
String body = Objects.requireNonNull(response.body()).string();
JsonObject object = new Gson().fromJson(body, JsonObject.class);
JsonArray dataList = object.getAsJsonArray("dataList");
//{"success":false,"status":401,"reference":"UNAUTHORIZED","message":"Unauthorized"}
//登录失败
if (Objects.isNull(dataList)) {
new Handler(Looper.getMainLooper()).post(() -> {
Toast.makeText(getApplication(), "token过期,请重新登录", Toast.LENGTH_SHORT).show();
});
return;
}

if (dataList.size() == 0) {
new Handler(Looper.getMainLooper()).post(() -> {
Toast.makeText(getApplication(), "加载完毕~", Toast.LENGTH_SHORT).show();
});
}
ArrayList<FileBean> fileBeanLinkedList = new ArrayList<>();
for (int i = 0; i < dataList.size(); i++) {
FileBean fileBean = setTimeAndPathAndSize(dataList, i);
if (fileBean.getMime().contains("image")) {
fileBeanLinkedList.add(fileBean);
}
}
liveData.postValue(fileBeanLinkedList);
}
});
return liveData;
}

/**
* 更新缓存
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ public MutableLiveData<Boolean> checkToken(String token) {
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {

new Handler(Looper.getMainLooper()).post(() -> {
Toast.makeText(getApplication(), e.getMessage(), Toast.LENGTH_SHORT).show();
});
}

@Override
Expand Down

0 comments on commit 056b64f

Please sign in to comment.