安卓回收者查看滚动到顶部

2022-09-04 20:35:46

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

答案 1

你试过还是?taskRecyclerView.getLayoutManager().scrollToPosition(0)taskRecyclerView.scrollToPosition(your_position)

RecyclerView没有实现任何滚动逻辑,并且更具体,它根据实现的布局转到特定索引,在您的例子中是线性的。taskRecyclerView.scrollToPosition(your_position)taskRecyclerView.getLayoutManager().scrollToPosition(0)


答案 2

滚动到特定项目是RecyclerView的一个棘手部分,但下面的代码有助于滚动

平滑滚动的方法

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

将平滑滚动应用到回收器查看

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

将不平滑的滚动应用于回收器查看(快速)

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

要使用的代码

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

额外信息:您可以替换 ..(0)用你的位置需要得到滚动。

完整代码:链接


推荐