0

currently implementing a recyclerview inside a fragment. previously was done using activity -recyclerview, without any problem. but with fragment i found weird problem.

Problem - when i load home for the first time, my recyclerview would load normally. But after changing to different fragment and back to home, my recyclerview would't load but stuck to the fragment i load previously.

could someone clarify what i did wrong?

public class HomeFragment extends Fragment {
private List<FeedData> feedList = new ArrayList<>();
private RecyclerView recyclerView;
private FeedAdapter fAdapter;

private OnFragmentInteractionListener mListener;

public HomeFragment () {
}

public static HomeFragment newInstance(String param1, String param2) {
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    fAdapter = new FeedAdapter(feedList);
    RecyclerView.LayoutManager fLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(fLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(fAdapter);

   prepareMovieData();

    return rootView;
}

preparemovie()

private void prepareMovieData() {
    FeedData feedData = new FeedData("Mad Max: Fury Road", "Action & Adventure", "2015");
    feedList.add(feedData);

    feedData = new FeedData("Inside Out", "Animation, Kids & Family", "2015");
    feedList.add(feedData);

    feedData = new FeedData("Star Wars: Episode VII - The Force Awakens", "Action", "2015");
    feedList.add(feedData);

    feedData = new FeedData("Shaun the Sheep", "Animation", "2015");
    feedList.add(feedData);

    fAdapter.notifyDataSetChanged();
}

HomeFragment

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.HomeFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

sorry for the noob question, just learning. most code are follow closely with stackoverflow and androidhive. *Attached are gif that replicate what the problem look like.

via GIPHY

InsaneCat
  • 2,115
  • 5
  • 21
  • 40

1 Answers1

0

I think you are replacing one fragment with other using:fragmentmanager.replace() you also need to add fragment to backstack. for better understanding see: How to resume Fragment from BackStack if exists