简体   繁体   中英

RecyclerView scroll position goes to top after update

I'm trying to update the data within the recyclerview every 5 seconds but every time the data is updated the scroll position is not maintained and in automatic it goes up.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_units_impl, container, false);
        initUnitsViewImpl(view);
        return view;
    }

    private void initUnitsViewImpl(View view) {
        initViewID(view);
        initPresenter();
        initOnClickListeners();
    }

private void initPresenter() {
        final UnitsPresenter presenter = new UnitsPresenterImpl(getContext());
        presenter.setView(this);
        presenter.getFullVehicles();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                presenter.getFullVehicles();
                presenter.hideProgressDialog();
                handler.postDelayed(this,5000);

            }
        },5000);

    }

@Override
    public void setUnitList(final List<Unit> unitList) {
        this.vehicles = unitList;
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rvUnits.setLayoutManager(linearLayoutManager);
        unitAdapter = new UnitAdapter(vehicles, getContext());
        rvUnits.setAdapter(unitAdapter);
    }

What am i doing wrong?

Thanks for your help.

I'm assuming the issue here is that you're trying to update your recycler view by updating the adapter. You actually do a lot of things element by element, no need to change the adapter.

Check out this answer and let me know if this is what you're looking for.

https://stackoverflow.com/a/48959184/13150066

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM