简体   繁体   English

Android recyclerview 滚动到顶部

[英]Android recyclerview scroll to top

This is my onCreate that handles the adapter, LinearLayoutManager and etc. I tried every single option to try to scroll it to the top but could not.这是我的 onCreate 处理适配器、LinearLayoutManager 等。我尝试了每个选项来尝试将其滚动到顶部,但不能。 I even tried to create my own custom LinearLayoutManager.我什至尝试创建自己的自定义 LinearLayoutManager。

//Reson for static to call from a static method to scroll it up
private static RecyclerView taskRecyclerView;
private FirebaseAdapter firebaseAdapter;
private static LinearLayoutManager linearLayoutManager;

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_tasklist, container, false);



    linearLayoutManager = new LinearLayoutManager(getActivity());

    taskRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_mainTask);
    databaseRef = FirebaseDatabase.getInstance().getReference().child(userAccount.getId());


    firebaseAdapter = new FirebaseAdapter(TaskObject.class, R.layout.one_task, TaskViewHolder.class, databaseRef.child("Task"), getContext());

    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    linearLayoutManager.setSmoothScrollbarEnabled(true);


    taskRecyclerView.setAdapter(firebaseAdapter);
    taskRecyclerView.setLayoutManager(linearLayoutManager);
    relativeLayoutAdd.setOnClickListener(this);
    //Listen for any data change
    databaseRef.child("Task").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //onDataChange called so remove progress bar

            //make a call to dataSnapshot.hasChildren() and based
            //on returned value show/hide empty view

            //use helper method to add an Observer to RecyclerView

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    databaseRef.child("Task").keepSynced(true);
    return view;
}

Did you try taskRecyclerView.getLayoutManager().scrollToPosition(0) or taskRecyclerView.scrollToPosition(your_position) ?您是否尝试过taskRecyclerView.getLayoutManager().scrollToPosition(0)taskRecyclerView.scrollToPosition(your_position)

RecyclerView does not implement any scrolling logic in taskRecyclerView.scrollToPosition(your_position) and taskRecyclerView.getLayoutManager().scrollToPosition(0) is more specific and it goes to specific index depending implemented layout, in your case is linear. RecyclerView 在taskRecyclerView.scrollToPosition(your_position)taskRecyclerView.getLayoutManager().scrollToPosition(0)中没有实现任何滚动逻辑更具体,它根据实现的布局转到特定的索引,在你的情况下是线性的。

Scrolling to specific items is a tricky part with RecyclerView but below code helps to scroll使用 RecyclerView 滚动到特定项目是一个棘手的部分,但下面的代码有助于滚动

Method for Smooth Scrolling平滑滚动的方法

fun Context.getSmoothScroll(): LinearSmoothScroller {
        return object : LinearSmoothScroller(this) {
            override fun getVerticalSnapPreference(): Int {
                return LinearSmoothScroller.SNAP_TO_START
            }
        }
    }

Apply Smooth scrolling to RecyclerView将平滑滚动应用于 RecyclerView

fun RecyclerView.scrollToPositionSmooth(int: Int) {
        this.layoutManager?.startSmoothScroll(this.context.getSmoothScroll().apply {
            targetPosition = int
        })
    }

Apply Scrolling without Smooth to RecyclerView (Fast)将不平滑的滚动应用到 RecyclerView(快速)

fun RecyclerView.scrollToPositionFast(position: Int) {
        this.layoutManager?.scrollToPosition(position)
    }

Code to use要使用的代码

itemFilteredRecyclerView.scrollToPositionFast(0) //Normal Scroll
itemFilteredRecyclerView.scrollToPositionSmooth(0) //Smooth Scroll

Extra Information: You can replace ..(0) with your position need to get scrolled.额外信息:您可以将 ..(0) 替换为您需要滚动的位置。

Complete code: Link完整代码: 链接

添加下面的代码也有效。

linearLayoutManager.setStackFromEnd(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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