当我从片段中删除任何视频或图像时,我正在使用recyclerview来显示sd卡中的本地图像和视频,但该图像或图像仍保留在那里。我必须返回以显示数据更改。我的回收者视图不会自动刷新,也不会自动刷新。谁能告诉我如何解决 这是我的代码。
RecyleAdapter.java
public class SavedRecycleAdapter extends RecyclerView.Adapter<SavedRecycleAdapter.MyViewHolder> {
private static String DIRECTORY_SAVE_MEDIA = "/storage/emulated/0/Folder/";
private ArrayList <File> filesList;
private Activity activity;
Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView videosThumbnail, picturesThumbnail;
ImageButton imageDelete, imageShare, videoShare, videoDelete;
CardView videoCardView, imageCardView;
public MyViewHolder(View itemView) {
super(itemView);
picturesThumbnail = itemView.findViewById(R.id.imagesThumbnailSaved);
videosThumbnail = itemView.findViewById(R.id.videosThumbnailSaved);
imageDelete = itemView.findViewById(R.id.imageDeleteSaved);
imageShare = itemView.findViewById(R.id.imageShareSaved);
videoDelete = itemView.findViewById(R.id.videoDeleteSaved);
videoShare = itemView.findViewById(R.id.videoShareSaved);
videoCardView = itemView.findViewById(R.id.videoCardViewSaved);
imageCardView = itemView.findViewById(R.id.imageCardViewSaved);
}
}
public SavedRecycleAdapter(ArrayList <File> filesList, Activity activity) {
this.filesList = filesList;
this.activity = activity;
}
@Override
public SavedRecycleAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.saved_item_view, parent, false);
return new SavedRecycleAdapter.MyViewHolder(inflatedView);
}
@Override
public void onBindViewHolder(final SavedRecycleAdapter.MyViewHolder holder, int position) {
final File currentFile = filesList.get(position);
if (currentFile.toString().endsWith(".mp4")) {
holder.videoCardView.setVisibility(View.VISIBLE);
holder.imageCardView.setVisibility(View.GONE);
Glide.with(activity)
.load(Uri.fromFile(new File(currentFile.getAbsolutePath())))
.into(holder.videosThumbnail);
holder.videoDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(currentFile.getAbsolutePath());
boolean deleted = file.delete();
//
}
});
holder.videosThumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(activity, VideoViewerActivity.class);
intent.putExtra("video", currentFile.getAbsolutePath());
activity.startActivity(intent);
}
});
holder.videoShare.setOnClickListener(new View.OnClickListener() {
//Do stuff
} else {
holder.videoCardView.setVisibility(View.GONE);
holder.imageCardView.setVisibility(View.VISIBLE);
Glide.with(activity).load(currentFile.getAbsolutePath())
.skipMemoryCache(false)
.crossFade().into(holder.picturesThumbnail);
holder.imageDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(currentFile.getAbsolutePath());
boolean deleted = file.delete();
holder.picturesThumbnail.setOnClickListener(new View.OnClickListener() {
// Do stuff
holder.imageShare.setOnClickListener(new View.OnClickListener() {
}
}
@Override
public int getItemCount() {
return filesList.size();
}
}
这是我的片段代码
public class SavedStoriesFragment extends Fragment {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
private static final String FILE_PATH = "/storage/emulated/0/Folder/";
public SavedStoriesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_saved_stories, container, false);
recyclerView=(RecyclerView)v.findViewById(R.id.savedRecyclerView);
layoutManager = new GridLayoutManager(getContext(),2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new ItemDecorationAlbumColumns(
getResources().getDimensionPixelSize(R.dimen.image_list_space),2));
getListFiles(new File(FILE_PATH));
SavedRecycleAdapter ra = new SavedRecycleAdapter(this.getListFiles(new File(FILE_PATH)), getActivity());
recyclerView.setAdapter(ra);
return v;
}
private ArrayList<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files;
files = parentDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith(".jpg") ||
file.getName().endsWith(".png") ||
file.getName().endsWith(".jpeg")||
(file.getName().endsWith(".mp4") ||
file.getName().endsWith(".gif"))) {
if (!inFiles.contains(file))
inFiles.add(file);
}
}
}
return inFiles;
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> mFragment = new ArrayList<>();
ArrayList<String> mTitles = new ArrayList<>();
public void addFragments (Fragment fragment,String tabTitles)
{
this.mFragment.add(fragment);
this.mTitles.add(tabTitles);
}
public ViewPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragment.get(position);
}
@Override
public int getCount() {
return mFragment.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}
}
答案 0 :(得分:1)
您需要使用notifyDataSetChanged()
或notifyItemRemoved(position)
来通知您的适配器基础数据已更改或特定项已删除。您可以从here中了解有关方法的更多信息。
答案 1 :(得分:1)
我有同样的问题,但如果您使用 ViewPager
中的 androidx
,我有解决方案。您可以更改:
public class ViewPagerAdapter extends FragmentPagerAdapter
到:
public class ViewPagerAdapter extends FragmentStatePagerAdapter
答案 2 :(得分:0)
在删除某些东西之后,调用片段方法以获取新的ListFiles和notifyDataSetChanged()
例如
内部片段
public void refreshRecyclerView() {
ra.setFilesList(this.getListFiles(new File(FILE_PATH)))
}
内部适配器
public void setFilesList(ArrayList <File> filesList) {
this.filesList = filesList;
notifyDataSetChanged();
}
或者只是您可以从filesList中删除项目并在适配器内调用notifyDataSetChanged()
答案 3 :(得分:0)
在删除文件时,请确保更新包含该参考的列表。对于您而言,很可能是从存储中删除文件后,其引用仍然存在于列表中,并且RecyclerView仍显示该文件的列表项视图。
例如:如果将所有文件引用保存在名为mFileList的列表中,则需要使用mFileList.remove()删除已删除文件的引用,然后使用notifyDataSetChanged()更新回收器视图。