Philipp Schumann
Philipp Schumann

Reputation: 1780

Android refresh Recyclerview delayed

I have a simple layout with a recyclerview. In the onCreateView method I set up the Recyclerview:

  @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_overview, container, false);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_overview);
    list = new ArrayList<>();
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);

    list = getOverviewList(getActivity());
    adapter = new OverviewRecyclerAdapter(getActivity(), list);
    recyclerView.setAdapter(adapter);
    final Context context = getActivity();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            list =getOverviewList(context);
            adapter.notifyDataSetChanged();
        }
    }, 1000);
    return rootView;
}

As you can see the Recyclerview needs to be refreshed a second after the view is creted. I tried to achieve thsi with a Handler. But the code within the Handler does not refresh the Recyclerview. But obviously it should.

Upvotes: 2

Views: 1557

Answers (2)

rahul.taicho
rahul.taicho

Reputation: 1379

Code looks good should definitely work are you sure you're modifying the list you get on the second call to getOverViewList(Context ctx) otherwise it'll be hard to tell if it refreshed because it loads the same data again

Upvotes: 0

orium
orium

Reputation: 3813

But obviously it should.

no it shouldn't, cause you not setting new data to adapter

list =getOverviewList(context);

you just setting to variable new list, but it doesn't change old list in adapter

edit:

try some like this

list.clear();
list.addAll(getOverviewList(context));

Upvotes: 2

Related Questions