nnn
nnn

Reputation: 183

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

Upvotes: 14

Views: 23081

Answers (3)

Merlin Jeyakumar
Merlin Jeyakumar

Reputation: 281

Scrolling to specific items is a tricky part with RecyclerView but below code helps to scroll

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

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

Apply Scrolling without Smooth to RecyclerView (Fast)

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

Code to use

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

Extra Information: You can replace ..(0) with your position need to get scrolled.

Complete code: Link

Upvotes: 2

Fernandes Aluda
Fernandes Aluda

Reputation: 1

Adding the code below works too.

linearLayoutManager.setStackFromEnd(false);

Upvotes: -1

Cătălin Florescu
Cătălin Florescu

Reputation: 5158

Did you try taskRecyclerView.getLayoutManager().scrollToPosition(0) or 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.

Upvotes: 18

Related Questions