java Android recyclerview 滚动到顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41655486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:01:35  来源:igfitidea点击:

Android recyclerview scroll to top

javaandroid

提问by nnn

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. I even tried to create my own custom LinearLayoutManager.

这是我的 onCreate 处理适配器、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;
}

回答by Florescu C?t?lin

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)更具体,它会根据实现的布局转到特定的索引,在您的情况下是线性的。