REX
REX

Reputation: 149

Recycler View gets loaded again when switching between fragments

So, I have an activity which contains three fragments and I am switching between the fragments using ViewPager.

So in one of my Fragment I am using a recycler view to load my data. I am using onCreateView to initialize my view and populate recycler view also. But the problem here is when I switch back and forth between the fragments my recycler view items gets loaded again. I don't want that. I want the items to be loaded only once.

eg. when I switch back and forth between IMAGES and SAVED tab the recycler view gets reloaded in IMAGES tab.(ps. look at image to understand better) my fragments view

This is my fragment where recycler view is loaded.

public ImageFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    statusView = inflater.inflate(R.layout.fragment_image,container,false);
    initViews();
    return statusView;
}

private void initViews() {
    progressBar = statusView.findViewById(R.id.progressBar);
    recyclerView = statusView.findViewById(R.id.statusRecyclerView);
    recyclerView.addItemDecoration(new ListViewItemDecorator(getResources().getDimensionPixelSize(R.dimen.spacing)));
    refreshLayout = statusView.findViewById(R.id.statusSwipeRefreshLayout);
    recyclerView.setHasFixedSize(true);
    adapter = new StatusAdapter(getContext(),arrayList);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
    adapter.notifyDataSetChanged();
    recyclerView.scheduleLayoutAnimation();
}

So is there anyway to make this happen?

Upvotes: 0

Views: 248

Answers (1)

Ayush Sth
Ayush Sth

Reputation: 332

Use ViewPager2 to create a tab bar that will solve your problem.

Upvotes: 1

Related Questions