Android Fragments Android BottomSheetDialogFragment数据更改父片段中的数据

amrnrhlw  于 2022-11-24  发布在  Android
关注(0)|答案(1)|浏览(201)

问题陈述

每当在BottomSheetDialogFragment中修改数据时,我都会遇到数据分段更改的问题

应用程序说明

在我的应用中,我有一个主活动,它在ViewPager中托管2个片段。第一个片段用于应用内容,第二个片段(我们称之为GalleryFragment),显示图库视图。用户可以点击图库项,加载BottomSheet(让我们称之为GalleryBottomSheet)-它托管RecyclerView以全屏显示图库项目。现在,应用程序最初将ArrayList输入GalleryFragment以显示图库项目。当用户点击图库项目时,此ArrayList将传递给GalleryBottomSheet
"发生了什么事"
发生的情况是,每当我从GalleryBottomSheet中的ArrayList中删除一个项目时,它也会自动删除GalleryFragmentArrayList中的该项目。简而言之,Arraylist中来自GalleryBottomSheet的任何更新都会影响GalleryFragment中的ArrayList
"你想要什么"
我希望分离关注点。我不希望在GalleryBottomSheetArrayList中所做的更改影响GalleryFragment的原始ArrayList
"告诉我密码"
为了使这个问题简洁,我只添加代码的重要部分。
~ * 图库片段.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");

                
            }


    }

~ * 图库底部工作表.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

}
tuwxkamq

tuwxkamq1#

最主要的问题是对象是通过引用存储在内存中的。通过修改GalleryBottomSheet中的arrayList,也会改变原始的arrayList,因为GalleryBottomSheet中的arrayList是从GalleryFragment引用的。解决方法是在GalleryBottomSheet中初始化一个新的ArrayList
变更自:

ArrayList<String> filePathArr = bundle.getStringArrayList("list");

至:

ArrayList<String> filePathArr = new ArrayList<>(bundle.getStringArrayList("list"));

相关问题