問題陳述
每當在BottomSheetDialogFragment
應用程式描述
在我的應用程式中,我有 MainActivity,它在它的 ViewPager 中托管 2 個 Fragment。GalleryFragment
應用內容的第一個片段和顯示畫廊視圖的第二個片段(我們稱之為)。用戶可以點擊加載BottomSheet(讓我們稱之為GalleryBottomSheet
)的畫廊專案 - 托管RecyclerView以全屏顯示畫廊專案。現在最初應用程式提供了GalleryFragment
以ArrayList
顯示畫廊專案。當用戶單擊 Gallery 專案時,這ArrayList
將傳遞給GalleryBottomSheet
.
所以發生
了什么發生的事情是,每當我從ArrayList
my 中洗掉一個專案時,它也會自動洗掉ofGalleryBottomSheet
中的那個專案。簡而言之,從影響到的任何更新ArrayList
GalleryFragment
Arraylist
GalleryBottomSheet
ArrayList
GalleryFragment
你想要什么
我想要分離關注點。我不希望對 of 所做的更改ArrayList
影響GalleryBottomSheet
原來ArrayList
的GalleryFragment
給我看看該死的
為了使這個問題簡潔,我只添加了代碼的重要部分。
~ GalleryFragment.java
public class GalleryFragment extends Fragment {
private RecyclerView recyclerView;
private ArrayList<String> arrayList = new ArrayList<>(); //This is the one which will be passed to the GalleryBottomSheet
private GalleryAdapter galleryAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
//setting up all the UI work...
arrayList = FindFiles(); //FindFiles is a private function searching for all file in the dir and adding the path as a string to the arraylist
galleryAdapter = new GalleryAdapter(getActivity()); //init the adapter
recyclerView.setAdapter(galleryAdapter); //setting the adapter
return view;
}
}
private class GalleryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Activity activity;
GalleryAdapter(Activity context) {
this.activity = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_content_item, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if(holder instanceof ItemViewHolder){
//setting up stuff..
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
private class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView imageView;
ItemViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putStringArrayList("list", arrayList);
GalleryBottomSheet galleryBottomSheet= GalleryBottomSheet.newInstance();
galleryBottomSheet.setOnRefreshListener( new GalleryBottomSheet.GalleryInterface() {
@Override
public void onRefresh() {
//here the actual arrayList size reduced even though the arrayList that was modified exist in GalleryBottomSheet
System.out.println("CURRENT LIST SIZE: " arrayList.size());
}
});
galleryBottomSheet.setArguments(bundle);
galleryBottomSheet.show(getParentFragmentManager(), "galleryPager");
}
}
~ GalleryBottomSheet.java
public class GalleryBottomSheet extends BottomSheetDialogFragment {
static GalleryBottomSheet newInstance() {
return new GalleryBottomSheet();
}
public interface GalleryInterface {
void onRefresh();
}
private RecyclerView recyclerView;
private ViewAdapter viewAdapter;
private Button deleteBtn;
private GalleryInterface galleryInterface;
public void setOnRefreshListener( GalleryInterface galleryInterface){
this.galleryInterface = galleryInterface;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//.. setting up sheetDialog
return bottomSheetDialog;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(BottomSheetDialogFragment.STYLE_NO_FRAME, R.style.GalleryBottomStyle);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gallery_sheet, container, false);
Bundle bundle = getArguments();
ArrayList<String> filePathArr = bundle.getStringArrayList("list"); //here arrayList from GalleryFragment
//Setting up all UI views...
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int positionPager = recyclerView.computeVerticalScrollOffset(); //just for demo, in the actual app we are using addOnScrollListener for position
filePathArr.remove(positionPager);
viewAdapter.notifyItemRemoved(positionPager);
galleryInterface.onRefresh(); //this is where GalleryFragment shows that the arraylist is modified too.
Toast.makeText(getActivity(), "Deleted Successfully",Toast.LENGTH_SHORT).show();
}
});
return view;
}
//setting up viewAdapter and other stuff
}
uj5u.com熱心網友回復:
好吧,事實證明主要問題是物件通過參考存盤在記憶體中。通過從中修改 arrayListGalleryBottomSheet
也會更改原始 arrayList,因為從 中參考了確切的GalleryBottomSheet
arrayList GalleryFragment
。解決方案是簡單地在GalleryBottomSheet
變化自:
ArrayList<String> filePathArr = bundle.getStringArrayList("list");
至:
ArrayList<String> filePathArr = new ArrayList<>(bundle.getStringArrayList("list"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/507359.html
上一篇:修改后如何訪問變數