你的照片。xml
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/picture_fragment"
android:layout_height="match_parent"
android:background="#F1F1F1">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcv_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:spanCount="1"
android:visibility="gone"
tools:listitem="@layout/item_picture" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<androidx.constraintlayout.widget.Group
android:id="@+id/group_no_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:constraint_referenced_ids="imv_no_data,tv_no_data" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imv_no_data"
android:layout_width="@dimen/_100sdp"
android:layout_height="@dimen/_100sdp"
android:layout_marginBottom="@dimen/_12sdp"
android:padding="@dimen/_8sdp"
android:src="@drawable/ic_no_image"
app:layout_constraintBottom_toTopOf="@id/tv_no_data"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_no_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_16sdp"
android:text="@string/no_image"
android:textColor="@color/color_828282"
android:textSize="@dimen/_14sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:visibility="gone"
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_16sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_no_data" />
</androidx.constraintlayout.widget.ConstraintLayout>
零碎的照片。JAVA
public class PictureFragment extends BaseFragment<FragmentPictureBinding> {
private static final int REQUEST_PERMISSION_DELETE = 376;
private PictureAdapter pictureAdapter;
private int pos;
SwipeRefreshLayout swipeRefresh;
@Override
protected void initView() {
pictureAdapter = new PictureAdapter(new ArrayList<>(), getContext());
pictureAdapter.setCallBackAdapter(item -> openMedia(item.getPath()));
pictureAdapter.setCallBackPicture(new PictureAdapter.CallBackPicture() {
@Override
public void onClickMore(String s, int adapterPosition, View v) {
showPopupMenuMore(s, adapterPosition, v);
}
});
binding.rcvPicture.setAdapter(pictureAdapter);
binding.swipeRefresh.setOnRefreshListener(() -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
getData();
}else {
if (getActivity() instanceof MainActivity){
((MainActivity) getActivity()).askPermissionStorageMain();
}
}
} else {
getData();
}
binding.swipeRefresh.setRefreshing(false);
});
binding.swipeRefresh.setColorSchemeResources(R.color.color_accent,
android.R.color.holo_green_dark,
android.R.color.holo_orange_dark,
android.R.color.holo_blue_dark);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
getData();
}
} else {
getData();
}
}
public void onCLickMore(String item, int pos, View view) {
showPopupMenuMore(item, pos, view);
}
private void showPopupMenuMore(String item, int pos, View view) {
PopupMenu popupMenu = new PopupMenu(getContext(), view);
popupMenu.inflate(R.menu.popup_menu_more_picture);
popupMenu.show();
// popupMenu.getMenu().getItem(1).setEnabled(PreferencesHelper.getBoolean(PreferencesHelper.KEY_SAVE_AS_GIF, true));
popupMenu.setOnMenuItemClickListener(menuItem ->{
switch (menuItem.getItemId()) {
case R.id.share_picture:
shareMedia(item);
break;
case R.id.delete_picture:
this.pos = pos;
deletePhoto(item);
break;
}
return true;
});
}
private void getData() {
getAllFilesInPicture()
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(this::mapFiles)
.subscribe(datas -> {
if (datas.isEmpty()) {
binding.rcvPicture.setVisibility(View.GONE);
binding.groupNoData.setVisibility(View.VISIBLE);
} else {
binding.rcvPicture.setVisibility(View.VISIBLE);
binding.groupNoData.setVisibility(View.GONE);
pictureAdapter.addDatas(datas);
}
},throwable -> {
});
}
public Single<File[]> getAllFilesInPicture() {
return Single.create(sub -> {
File[] listFile = Storage.getFilesImageInStorage(requireContext());
if (listFile != null) {
sub.onSuccess(listFile);
} else {
sub.onSuccess(new File[]{});
}
});
}
public Single<List<VideoFile>> mapFiles(@NonNull File[] listFile) {
return Single.create(sub -> {
long previousDate = 0;
List<VideoFile> videoFiles = new ArrayList<>();
Arrays.sort(listFile, (f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified()));
List<String> pictureFile = new ArrayList<>();
for (int i = listFile.length - 1; i >= 0; i--) {
VideoFile videoFile = new VideoFile();
if (!Toolbox.isSameDay(previousDate, listFile[i].lastModified())) {
VideoFile header = new VideoFile();
header.setHeader(true);
header.setLastModified(listFile[i].lastModified());
videoFiles.add(header);
}
videoFile.setPath(listFile[i].getAbsolutePath());
videoFile.setName(listFile[i].getName());
videoFile.setSize(listFile[i].length());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(new File(listFile[i].getPath()).getAbsolutePath(), options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
videoFile.setResolution(imageWidth+"x"+imageHeight);
videoFile.setSize(listFile[i].length());
videoFile.setLastModified(listFile[i].lastModified());
videoFiles.add(videoFile);
previousDate = listFile[i].lastModified();
pictureFile.add(listFile[i].getAbsolutePath());
}
sub.onSuccess(videoFiles);
});
}
@Override
protected void initControl() {
}
private void itemToItem(String filepath){
}
private void openMedia(String filePath) {
Uri fileUri = FileProvider.getUriForFile(
getContext(), getContext().getPackageName() + ".provider",
new File(filePath));
try {
Intent openVideoIntent = new Intent();
openVideoIntent.setAction(Intent.ACTION_VIEW)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK)
.setDataAndType(
fileUri,
getContext().getContentResolver().getType(fileUri));
getContext().startActivity(openVideoIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
private void shareMedia(String filePath) {
Uri fileUri = FileProvider.getUriForFile(
getContext(), getContext().getPackageName() + ".provider",
new File(filePath));
Intent shareIntent = new Intent()
.setAction(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_STREAM, fileUri)
.setType(filePath.endsWith(".mp4") ? "video/mp4" : "image/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(shareIntent);
}
protected void deletePhoto(String path) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Delete");
alert.setMessage("Do you want to delete this image ?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//This will delete my Image from recycleView
deleteMedia(path);
//I Wan't to add here code for refreshing recycle view
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
private void deleteMedia(String filePath) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
new File(filePath).delete();
//removeItem(pos);
MediaScannerConnection.scanFile(getActivity(),
new String[]{filePath}, new String[]{"video/mp4"},
(path1, uri) -> {
});
} else {
MediaScannerConnection.scanFile(getActivity(),
new String[]{filePath}, new String[]{"video/mp4"},
(path1, uri) -> {
if (uri != null) {
try {
if (getActivity().getContentResolver().delete(uri, null, null) != -1) {
//removeItem(pos);
}
swipeRefresh.setRefreshing(false);
} catch (SecurityException e) {
List<Uri> uris = new ArrayList<>();
uris.add(uri);
requestDeletePermission(uris);
}
}
});
}
}
private void requestDeletePermission(List<Uri> uriList) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
PendingIntent pi = MediaStore.createDeleteRequest(getActivity().getContentResolver(), uriList);
startIntentSenderForResult(pi.getIntentSender(), REQUEST_PERMISSION_DELETE, null, 0, 0,
0, null);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
private void removeItem(int pos) {
getBaseActivity().runOnUiThread(() -> {
pictureAdapter.getList().remove(pos);
pictureAdapter.notifyItemRemoved(pos);
if (pictureAdapter.getList().get(pictureAdapter.getList().size() - 1).isHeader()) {
pictureAdapter.getList().remove(pictureAdapter.getList().size() - 1);
pictureAdapter.notifyItemRemoved(pictureAdapter.getList().size() - 1);
}
if (pictureAdapter.getList().isEmpty()) {
binding.rcvPicture.setVisibility(View.GONE);
binding.groupNoData.setVisibility(View.VISIBLE);
binding.progress.setVisibility(View.GONE);
} else {
binding.rcvPicture.setVisibility(View.VISIBLE);
binding.groupNoData.setVisibility(View.GONE);
}
//Have?
});
}
@Override
protected boolean isNeedRefresh() {
return true;
}
@Override
protected FragmentPictureBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
return FragmentPictureBinding.inflate(LayoutInflater.from(getContext()));
}
@Override
public void onReceivedEvent(RxBusType type, Object data) {
switch (type) {
case SCREEN_SHOT:
case NOTI_MEDIA_CHANGE:
getData();
break;
}
}
}
我在循环视图的picture_item上添加了弹出菜单,当我们按下弹出菜单的删除按钮时,我发送该图像的字符串路径来删除照片(项目);和删除图像的方法。
删除图像后,我们必须通过拉刷新手动刷新片段,但在这种情况下,可以添加任何自动刷新片段的方法吗?
请参阅deletePhoto方法,我在其中删除了照片,但当我尝试用removepos方法刷新时,它崩溃了
我也尝试了stackoverflow的许多方法,但没有成功,请帮助我
您已经完成了所有操作,取消对removeItem方法的注释,并添加pictureAdapter。notifyDataSetChanged()
在removeItem方法的末尾
这将重新加载适配器的数据集
如果知道已删除元素的位置,可以使用
pictureAdapter.notifyItemRemoved(position)
这将告诉适配器您从特定位置的数据集中删除了一项
实现循环ScrollView。有以下特色: 1、循环的scrollview 2、类似于tableview的编程方式 3、可定制化的内容 4、灵活运用可用于移步加载图片 5、结构化,可扩展性高 [Code4App.com]
我想在点击recyclerview项目时更新Imageview的图像。下面我发布了我的布局截图: 小图像在recycler视图中,大绿色图像在recycler视图之外。 }//这是活动类 } 我想,当用户点击任何回收器视图时,图像意味着小图像,它将显示在回收器视图之外的大图像视图中。
我设法创建了一个自定义列表视图,并为每一行添加了一个删除按钮。删除按钮运行良好,当我单击按钮时,该行会从数据库中删除。问题是,当我删除一行时,它会保留在列表中,直到我通过移动到另一个片段来刷新片段并返回,然后它从列表中消失。我想要的是,当我删除一行时,它会立即从列表视图中消失。我尝试了一些解决方案来刷新整个片段或删除完成后的列表视图,但它们不起作用! 这是我为适配器类中的删除按钮编写的代码 之后
Vuejs的视图,使用了"指令"(directive). 下面分别来说. 注意: 无论是v-if 还是v-for, 都要与某个标签结合使用. 这点跟JSP, PHP, Rails很不同. 循环: v-for <tr v-for="blog in blogs"> <td >{{blog.title }}</td> </tr> 上面代码,会被渲染成: <tr> <td>...</td>
我的循环视图滚动太慢了。当我通过触摸Recyclerview开始滚动时,它会滞后,但当从上方的视图开始滚动时不会滞后。我还禁用了recyclerview上的嵌套滚动。 这是我的布局: 这是一个滞后的视频。就像滚动跳过了一些布局。
似乎刷新问题之前讨论过,但没有一个解决方案对我有效。 我正在尝试做的事情:我正在使用FragmentStatePagerAdapter。适配器的每个位置都保存一个片段,该片段具有类似国际象棋的线性布局,每个单元格由CheckedTextView表示。 问题:即使正确存储和分配了值,视图仍然不能刷新,视图中单元格的值也不会得到更新。 我尝试过的建议都不起作用:1。调用Adapter.NotifyDa