I'm having a recyclerview (verticalRV) which scrolls vertically. Each item in this recyclerview(horizontalRV) is a Horizontal recyclerview.
Inside the verticalRV itemViewHodler im trying to fetch data from the viewmodel and observe for any chages and update the horizontalRV adapter accordingly.
But the observers is onChanged
method is not getting called.
I have implemented the LifecycleOwner interface to manage the the lifecyle of the view holder with livedata and setting the state accordingly form the adapter of verticalRV
public class VeritcalRVHolderItem implements LifecycleOwner {
private static final String TAG = LDFeedListAdapterHolder.class.getSimpleName();
private final FragmentActivity activity;
private final RvHorizontalListAdapter adapter;
private RecyclerView rvHorizontalList;
public VeritcalRVHolderItem(Context context, View itemView, FragmentActivity activity) {
super(context, itemView);
this.activity = activity;
rvHorizontalList = itemView.findViewById(R.id.rvHorizontalList);
LinearLayoutManager layout = new LinearLayoutManager(getContext(), LinearLayout.HORIZONTAL, false);
rvHorizontalList.setLayoutManager(layout);
adapter = new RvHorizontalListAdapter(this.activity);
rvHorizontalList.setAdapter(adapter);
LDViewModel LDViewModel = ViewModelProviders.of(activity).get(LDViewModel.class);
LDViewModel.getTopicsForFeed().observe(this, new Observer<List<Topic>>() {
@Override
public void onChanged(List<Topic> topics) {
//adding live discussion model at first position
adapter.updateLiveList(topics);
adapter.notifyItemChanged(0);
Log.d(TAG, "discussion model calls");
}
});
}
private LifecycleRegistry lifecycleRegistry;
public void onAppear() {
lifecycleRegistry.markState(Lifecycle.State.CREATED);
}
public void onDisappear() {
lifecycleRegistry.markState(Lifecycle.State.DESTROYED);
}
@NonNull
@Override
public Lifecycle getLifecycle() {
return lifecycleRegistry;
}
}
Please let me know what am I missing here.
lifecycleOwner
from Fragments butviewLifecycleOwner
instead. Therefore you'd need to check if thecontext
is a fragment and then get theviewLifecycleOwner
. – Nuggar