使用Recycler View将会议室数据库滚动到特定位置

时间:2019-06-18 12:55:57

标签: android android-recyclerview android-room

我正在使用Room数据库从数据库中获取数据。 Recyclerview我想滚动到recyclerview中的特定位置。

我已经尝试过list.setNestedScrollingEnabled(true)

StationListRepository stationListRepositoryOnlyActive =新的StationListRepository(getApplicationContext());         stationListRepositoryOnlyActive.getActiveStationList()。observe(this,new Observer>(){             @Override             public void onChanged(List OnlyActivestations){                 Log.d(“ OnlyActivestations”,“” + OnlyActivestations.size());                 断言OnlyActivestations!= null;                 如果(OnlyActivestations.size()> 0 || FinalList.size()> 0){                     如果(FinalList!= null && FinalList.size()> 0){                         OnlyActivestations = sortList(OnlyActivestations);                         FinalList.addAll(OnlyActivestations);

                } else {
                    OnlyActivestations = sortList(OnlyActivestations);
                    FinalList = OnlyActivestations;
                }
                Log.d("FinalListSizeActive",""+FinalList.size());
                detailsAdapter = new db_stationListAdapter(context, FinalList);
                //recyclerView.setNestedScrollingEnabled(true);
                StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
                staggeredGridLayoutManager.scrollToPosition(10);
                recyclerView.setLayoutManager(staggeredGridLayoutManager);
                recyclerView.setAdapter(detailsAdapter);
                detailsAdapter.notifyDataSetChanged();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                       // stopLoading();
                        stopAnimation();
                    }
                });
            }
        }
    });

除回收器视图外,我会自动滚动到位置10

1 个答案:

答案 0 :(得分:0)

在将数据设置为适配器后尝试使用以下代码

recyclerView.smoothScrollToPosition(10);

您可以将 OnLayoutChangeListener 添加到RecyclerView。

recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){

for path in dictionary.keys {
    print(type(of: path).rootType)
    print(type(of: path).valueType)
    if let writableKeyPath = path as? WritableKeyPath<Root, Value>, let value = value as? Value { //no idea how to cast this for all cases
        spots[keyPath: writableKeyPath] = value
    }
}