Passing data from Adapter to Item's ViewModel in Android

Viewed 22

I have a view pager with an adapter that has a number of items fetched from the cloud. I add the pages to the adapter through the fragment. I am trying to pass a variable to the Item's view model every time a new page is instantiated from the adapter so as to notify one of the views that depend on this view model. I have tried a few workarounds but nothing has been successful so far. Would anyone please advice how I may pass this data.

Below is my adapter

public class HelpPagerAdapter extends PagerAdapter {

private final List<HelpPage> mHelpPages;

private int currentPage = 0;

public HelpPagerAdapter(Context context) {
    mPages = new ArrayList<>();
}

@Override
public int getCount() {
    return mHelpPages.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    HelpPage helpPage = mHelpPages.get(position);
    currentPage = position;

    LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ViewDataBinding binding = DataBindingUtil.inflate(inflater, helpPage.getLayout(), container, false);
    binding.setVariable(helpPage.getViewModelBindingId(), helpPage.getViewModel());

    container.addView(binding.getRoot());

    return binding.getRoot();
}

public void addPage(@LayoutRes int layout, Object viewModel, int viewModelBindingId) {
    mHelpPages.add(new HelpPage(layout, viewModel, viewModelBindingId));
}

private class HelpPage {
    private final @LayoutRes int mLayout;
    private final Object mViewModel;
    private final int mViewModelBindingId;

    HelpPage(@LayoutRes int layout, Object viewModel, int viewModelBindingId) {
        mLayout = layout;
        mViewModel = viewModel;
        mViewModelBindingId = viewModelBindingId;
    }

    @LayoutRes int getLayout() {
        return mLayout;
    }

    Object getViewModel() {
        return mViewModel;
    }

    int getViewModelBindingId() {
        return mViewModelBindingId;
    }
}
}

Part of my fragment code

 @Override
public void onAttach(Context context) {
    super.onAttach(context);

    Bundle bundle = getArguments();
    if (bundle == null || (!bundle.containsKey(ARG_STEPS) && !bundle.containsKey(ARG_VIEWS))) {
        throw new IllegalStateException("HelpFragment cannot be initialised without views");
    }

    HelpPagerAdapter adapter = new HelpPagerAdapter(getContext());
    if (bundle.containsKey(ARG_STEPS)) {
        List<Step> steps = Parcels.unwrap(bundle.getParcelable(ARG_STEPS));
        adapter = addHelpPages(steps, adapter);
    }

    mHelpViewModel = new HelpViewModel(adapter);
}


private HelpPagerAdapter addHelpPages(List<Step> steps, HelpPagerAdapter adapter) {
    for (Step step : steps) {
        adapter.addPage(R.layout.layout_help, new HelpItemViewModel(step), BR.viewModel);
    }

    return adapter;
}

Part of my ViewModel

public class HelpItemViewModel {

private final String mText;
private final String mVideoId;
private final String mImageUrl;

public HelpItemViewModel(Step step) {

    mText = step.getText();
    mHelpId = step.getHelpId();
    mImageUrl = step.getImage();
   
}
}
0 Answers
Related