Home > OS >  Scroll to top of RecyclerView using setOnClickListener
Scroll to top of RecyclerView using setOnClickListener

Time:07-05

I have this home button in top bar:

val activity: ImageButton =findViewById(R.id.activity)
        activity.setOnClickListener{
            startActivity(Intent(this, ActivitySearch::class.java))
        }

Instead of going to a new page, I want to be able to just put a "to top" intent, I have tried to replace:

startActivity(Intent(this, ActivitySearch::class.java))

with

mRecyclerView.smoothScrollToPosition(0);

But this does not work, any ideas? I do have a RecyclerView in use so half way down, I want to be able to click the above button and it goes straight to the top.

Total code:

    private val apiService by lazy {
        ApiClient.create()
    }


    private var taskData : ArrayList<TaskResponse> = arrayListOf();

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val activity: ImageButton =findViewById(R.id.activity)
        activity.setOnClickListener{
            startActivity(Intent(this, ActivitySearch::class.java))
        }

        val searchbutton:ImageButton=findViewById(R.id.searchbutton)
        searchbutton.setOnClickListener{
            startActivity(Intent(this, ActivitySearch::class.java))
        }

        val saves:ImageButton=findViewById(R.id.saves)
        saves.setOnClickListener{
            startActivity(Intent(this, ActivitySaves::class.java))
        }

        val settings:ImageButton=findViewById(R.id.settings)
        settings.setOnClickListener{
            startActivity(Intent(this, ActivitySettings::class.java))
        }
        loadData();

    }

    private fun loadData(){
        var data = apiService.getMainData();

        data.enqueue(object : Callback<ArrayList<TaskResponse>> {

            override fun onResponse(
                call: Call<ArrayList<TaskResponse>>,
                response: Response<ArrayList<TaskResponse>>
            ) {

                if (response.code() == 200) {
                    taskData = response.body()!!;
                    displaydata();
                }

            }

            override fun onFailure(call: Call<ArrayList<TaskResponse>>, t: Throwable) {
                Log.d("-----------------", t.toString())
            }
        })


    }

    private fun displaydata(){
        var recyclerView = findViewById<RecyclerView>(R.id.mainList);
        var adapter = TaskAdapter(this, taskData);
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter



        val swipeRefreshLayout: SwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout)
// load data on swipe refresh.
        swipeRefreshLayout.setOnRefreshListener { loadData() }
// Disable the refreshing after data load
        swipeRefreshLayout.isRefreshing = false


    }

And what i'm using for activity page:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


        <View
            android:id="@ id/view"
            android:layout_width="match_parent"
            android:layout_height="0.1dp"
            android:background="#6A6A6A"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/searchbutton"
            app:layout_constraintVertical_bias="0.01" />

        <TextView
            android:id="@ id/textView"
            android:layout_width="wrap_content"
            android:layout_height="46dp"
            android:gravity="center"
            android:text="@string/w2d"
            android:textColor="@color/black"
            android:textSize="25sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.047"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.01" />

        <ImageButton
            android:id="@ id/activity"
            android:layout_width="45dp"
            android:layout_height="46dp"
            android:layout_gravity="center"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:background="#FFFFFF"
            android:contentDescription="@string/activities"
            android:src="@drawable/ic_home"
            android:tint="#00B0F0"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.107"
            app:layout_constraintStart_toEndOf="@ id/textView"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.01" />

        <ImageButton
            android:id="@ id/searchbutton"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_gravity="center"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:background="#FFFFFF"
            android:contentDescription="@string/activities"
            android:src="@drawable/ic_search"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.154"
            app:layout_constraintStart_toEndOf="@ id/activity"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.01" />

        <ImageButton
            android:id="@ id/saves"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_gravity="center"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:background="#FFFFFF"
            android:contentDescription="@string/activities"
            android:src="@drawable/ic_saves"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.242"
            app:layout_constraintStart_toEndOf="@ id/searchbutton"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.01" />

        <ImageButton
            android:id="@ id/settings"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_gravity="center"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:background="#FFFFFF"
            android:contentDescription="@string/activities"
            android:src="@drawable/ic_setting"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.673"
            app:layout_constraintStart_toEndOf="@ id/saves"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.01" />




    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@ id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00FFFFFF"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/view"
        app:layout_constraintHorizontal_bias="0.0">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@ id/mainList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F0F2F5"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/view"
            app:layout_constraintVertical_bias="0"
            tools:layout_editor_absoluteX="0dp" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

This should work for going to the top of the recyclerView.

mRecyclerView.smoothScrollToPosition(0);

or

mRecyclerView.scrollToPosition(0);

When your list changes in recyclerView adapter, you should use that code. Make sure other parts of the code. This should work.

CodePudding user response:

Using a comments answer from @cactustictacs, I edited my code to this and it worked:

val activity: ImageButton =findViewById(R.id.activity)
        val mRecyclerView = findViewById<RecyclerView>(R.id.mainList)
        activity.setOnClickListener{
            mRecyclerView.smoothScrollToPosition(0)
        }
  • Related