如何将数据从片段传递到其视图持有者

时间:2019-07-12 13:07:34

标签: android string android-intent fragment

我正在尝试将数据从片段传递到其viewHolder。我的数据是一个字符串值。单击某个项目时,我将在其他活动中使用此数据。我搜索了很多遍,但是找不到任何解决方案。但是我不知道该怎么做。这是我的片段和viewHolder 如果您想知道其他内容,请告诉我。

 public class CreateStickerFragment extends Fragment {

    FragmentCreateStickerBinding binding;
    CreateStickerFragmentArgs args;
    CreateStickerAdapter adapter;
    List<Pack> mList;
    GridLayoutManager layoutManager;
    public CreateStickerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentCreateStickerBinding.inflate(inflater, container, false);
        adapter = new CreateStickerAdapter(getActivity());
        layoutManager = new GridLayoutManager(getActivity(), adapter.getGridSize());
        layoutManager.setSpanSizeLookup(adapter.getSpanSizeLookup());
        binding.recyclerList.setLayoutManager(layoutManager);
        binding.recyclerList.setAdapter(adapter);
        args = CreateStickerFragmentArgs.fromBundle(getArguments());
        String fName = args.getFolderName(); // I want to send this data to my view holder

        if (!AdmUtils.isContextInvalid(getActivity())) {
            mList = new ArrayList<>();
            for (int i = 0; i <= 29; i++) {
                mList.add(new Pack());
            }
            adapter.setData(mList);
        }

        return binding.getRoot();
    }
}





//viewholder
    public class CreateStickerViewHolder extends BindableViewHolder<Pack> {

        private TextView numberOfRow;
        private ImageView sticker;

        public CreateStickerViewHolder(@NonNull View itemView) {
            super(itemView);
            numberOfRow = itemView.findViewById(R.id.numberOfRow);
            sticker = itemView.findViewById(R.id.sticker);
        }

        @Override
        public void bindTo(final Activity activity, final Pack model, final int position) {
            numberOfRow.setText(String.valueOf(position + 1));

            sticker.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(activity, StickersCreatorActivity.class);
                    activity.startActivity(intent);
                }
            });
        }
    }

这是我的适配器;

public class CreateStickerAdapter extends BaseAdapter<Pack, CreateStickerViewHolder> {


    public CreateStickerAdapter(Activity activity) {
        super(activity, R.layout.row_create_sticker);

    }

    @Override
    protected CreateStickerViewHolder createViewHolder(View view) {
        return new CreateStickerViewHolder(view);
    }

}

1 个答案:

答案 0 :(得分:0)

您是否创建了CreateStickerAdapter?如果是这样,您可以向其中添加文件夹名称属性,以传递到ViewHolder实例。