如何在Recycler View中平滑滚动到特定位置(有效/绑定收集适配器)

时间:2018-06-21 07:28:35

标签: android android-recyclerview recycler-adapter android-databinding recyclerview-layout

我正在使用evant / binding-collection-adapter呈现项目列表。呈现列表后,我想滚动到特定位置。以下是我的代码,但是没有用。任何帮助.. ??

XML。

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewFriends"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                android:nestedScrollingEnabled="true"
                android:visibility="@{viewModel.isProgressBarVisible ? View.GONE : View.VISIBLE}"
                app:itemBinding="@{viewModel.singleItemFriendsList}"
                app:items="@{viewModel.friendDataItems}" />

活动:

   @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        invokeTheService();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        getBinding().recyclerViewFriends.setLayoutManager(linearLayoutManager);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getBinding().recyclerViewFriends.scrollToPosition(getViewModel().getFriendDataItems().size()-1);
            }
        }, 1000);

    }

ViewModel:

  public final OnItemBind<FriendItemViewModel> singleItemFriendsList = new OnItemBind<FriendItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, FriendItemViewModel item) {
            if (position == getSelectedTabListSize() - 1 && isLoadingAdded())
                itemBinding.set(BR.itemViewModel, R.layout.item_progress_friends);
            else
                itemBinding.set(BR.itemViewModel, R.layout.friends_listing_row);

            item.setItemPosition(position);
        }

    };

我用于RecyclerView的库: https://github.com/evant/binding-collection-adapter

1 个答案:

答案 0 :(得分:0)

Recyclerview放在nestedScroll中。然后设置您的recyclerview setNestedScrollEnabled(true)。我认为这会对您有所帮助:)