【问题标题】:PositionalDataSource : how to jump to specific position in recycler view?PositionalDataSource:如何跳转到回收站视图中的特定位置?
【发布时间】:2019-11-10 16:36:51
【问题描述】:

我正在使用 PositionalDataSource 和 LiveData/PagedListAdapter

当给定相应的键值时,我想让我的 recyclerview 滚动到特定位置。 如何做到这一点?


添加了我的试用代码:我希望当按钮单击时 recyclerview 方法 scrolltoposition 运行良好.. 但我知道这是一种不正确的方式。我相信我需要使用密钥而不是这种直接访问。

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);
        }
    });

数据源类

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));
}

存储库类

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() {...}

【问题讨论】:

  • 你试过什么?请分享您的相关代码的 sn-p。
  • 感谢您的评论,我刚刚添加了一些相关代码
  • 您需要.setEnablePlaceholders(true) 才能做到这一点。虽然不确定它是否会起作用。我隐约记得必须通过告诉 LinearLayoutManager 来滚动,例如 .scrollToPositionWithOffset(0, distanceTo20000thElementInPixels)
  • 嗨@sootak 你找到解决问题的方法了吗?目前我有同样的情况,不知道如何解决它

标签: android android-recyclerview android-architecture-components android-paging


【解决方案1】:

如果你有一个 recyclerView 对象,那么你可以使用方法scrollToPosition(position: Int)

【讨论】:

    猜你喜欢
    • 2017-10-05
    • 2018-11-17
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2018-03-16
    • 1970-01-01
    相关资源
    最近更新 更多