当前位置:  开发笔记 > 编程语言 > 正文

Android recyclerview滚动到顶部

如何解决《Androidrecyclerview滚动到顶部》经验,为你挑选了1个好方法。

这是我的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;
}

Florescu Căt.. 13

你有没有尝试taskRecyclerView.getLayoutManager().scrollToPosition(0)还是taskRecyclerView.scrollToPosition(your_position)

RecyclerView没有实现任何滚动逻辑,taskRecyclerView.scrollToPosition(your_position)并且taskRecyclerView.getLayoutManager().scrollToPosition(0)更具体,它根据实现的布局进入特定索引,在您的情况下是线性的.



1> Florescu Căt..:

你有没有尝试taskRecyclerView.getLayoutManager().scrollToPosition(0)还是taskRecyclerView.scrollToPosition(your_position)

RecyclerView没有实现任何滚动逻辑,taskRecyclerView.scrollToPosition(your_position)并且taskRecyclerView.getLayoutManager().scrollToPosition(0)更具体,它根据实现的布局进入特定索引,在您的情况下是线性的.

推荐阅读
低调pasta_730
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有