将SwipetoreFresh添加到回收Android中

发布于 2025-02-12 16:00:16 字数 3535 浏览 1 评论 0原文

具有以下代码的活动页面:

<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>

在我的主要活动上

package com.example.what2do_v2

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.what2do_v2.Adapter.TaskAdapter
import com.example.what2do_v2.Model.TaskResponse
import com.example.what2do_v2.Service.ApiClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class MainActivity : AppCompatActivity() {
    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 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

    }
}

。我真的很挣扎在哪里添加其他“ SwipereFreshlayout”代码。我试图在StackFlow上遵循每个示例,但似乎不了解合适的位置添加位置。

Have activity page with following code:

<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>

On my main activity.kt file, I have the following code but I am unsure where i need to put listenings etc. as I didn't write this part of the code;

package com.example.what2do_v2

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.what2do_v2.Adapter.TaskAdapter
import com.example.what2do_v2.Model.TaskResponse
import com.example.what2do_v2.Service.ApiClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class MainActivity : AppCompatActivity() {
    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 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

    }
}

I'm really struggling where to add additional "SwipeRefreshLayout" code into the above. I have tried to follow each example on stackflow, but can't seem to understand where the appropriate places to add.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野の 2025-02-19 16:00:16

初始化swipereFreshlayout&amp;在您的ongreate()中分配刷新听众:

val swipeRefreshLayout: SwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout)

// load data on swipe refresh.
swipeRefreshLayout.setOnRefreshListener { loadData() }

// Disable the refreshing after data load
swipeRefreshLayout.isRefreshing = false

Initialize the SwipeRefreshLayout & assign a refresh listener in your onCreate() like this:

val swipeRefreshLayout: SwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout)

// load data on swipe refresh.
swipeRefreshLayout.setOnRefreshListener { loadData() }

// Disable the refreshing after data load
swipeRefreshLayout.isRefreshing = false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文