sootak
sootak

Reputation: 31

PositionalDataSource : how to jump to specific position in recycler view?

I'm using PositionalDataSource and LiveData/PagedListAdapter

I want to make my recyclerview scroll to specific position when given a corresponding key value. How to do this?


added my trial code : what i hope is when button clicked recyclerview method scrolltoposition works well.. but I know it's an incorrect way. I believe i need to use key instead of this direct access.

itemViewModel.getAllItems().observe(this, new Observer<List<Item>>() {
        @Override
        public void onChanged(@Nullable List<Item> itemList) {
            itemViewModel.getPagedListLiveData(itemList).observe(MainActivity.this, new Observer<PagedList<Item>>() {
                @Override
                public void onChanged(@Nullable PagedList<Item> items) {
                    adapter.submitList(items);
                }
            });
        }
    });
    binding.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ItemDataSource dataSource = itemViewModel.getDataSource();
            dataSource.invalidate();
            binding.rv.getLayoutManager().scrollToPosition(20000);
        }
    });

datasource class

public class ItemDataSource extends PositionalDataSource<Item> {
private List<Item> list;

public ItemDataSource(List<Item> list) {
    this.list = list;
}

private int computeCount() {
    return list.size();
}

private List<Item> loadRangeInternal(int startPosition, int loadCount) {
    List<Item> modelList = new ArrayList<>();
    int endPosition = Math.min(computeCount(), startPosition + loadCount);
    for (int i = startPosition; i < endPosition; i++) {
        modelList.add(list.get(i));
    }
    return modelList;
}

@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<Item> callback) {
    int totalCount = computeCount();
    int position = computeInitialLoadPosition(params, totalCount);
    int loadSize = computeInitialLoadSize(params, position, totalCount);
    callback.onResult(loadRangeInternal(position, loadSize), position, totalCount);
}

@Override
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<Item> callback) {
    callback.onResult(loadRangeInternal(params.startPosition, params.loadSize));
}

repository class

public class ItemRepository {
private ItemDao itemDao;
private LiveData<List<Item>> allItems;
private MutableLiveData<PagedList<Item>> pagedListLiveData = new MutableLiveData<>();

private ItemDataSource dataSource;

public ItemRepository(Application application) {
    ItemDatabase database = ItemDatabase.getInstance(application.getApplicationContext());
    itemDao = database.itemDao();
    allItems = itemDao.getAllItems();
}

public LiveData<PagedList<Item>> getPagedListLiveData(List<Item> itemList) {
    dataSource = new ItemDataSource(itemList);
    PagedList.Config config = new PagedList.Config.Builder()
            .setPageSize(200)
            .setEnablePlaceholders(false)
            .build();
    PagedList<Item> itemPagedList = new PagedList.Builder<>(dataSource, config)
            .setInitialKey(10000)
            .setNotifyExecutor(new MainThreadExecutor())
            .setFetchExecutor(Executors.newSingleThreadExecutor())
            .build();
    pagedListLiveData.setValue(itemPagedList);
    return pagedListLiveData;
}

public ItemDataSource getDataSource() {...}

public LiveData<List<Item>> getAllItems() {...}

Upvotes: 3

Views: 569

Answers (1)

Pavel K
Pavel K

Reputation: 11

If you have a recyclerView object then you can use method scrollToPosition(position: Int)

Upvotes: 1

Related Questions