有以下代碼的活動頁面:
<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>
在我的主要 activity.kt 檔案中,我有以下代碼,但我不確定我需要在哪里放置監聽等,因為我沒有撰寫這部分代碼;
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 上的每個示例進行操作,但似乎無法理解要添加的適當位置。
uj5u.com熱心網友回復:
像這樣初始化SwipeRefreshLayout& 分配一個重繪 偵聽器onCreate():
val swipeRefreshLayout: SwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout)
// load data on swipe refresh.
swipeRefreshLayout.setOnRefreshListener { loadData() }
// Disable the refreshing after data load
swipeRefreshLayout.isRefreshing = false
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497986.html
標籤:安卓 科特林 android-recyclerview
