我的 xlm 中有以下布局代碼,用于在我的狀態片段中呼叫我的回收站視圖
<?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=".fragments.Status">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@ id/swipeToRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/statusRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
我試圖在我的主要活動中使用它來呼叫此滑動以重繪 ,但沒有成功,所以現在我嘗試在我的片段檔案中使用,我使用 xml 來獲取 recyclerView,這里顯示了錯誤: 未解決參考:mySwipeRefreshLayout--- PackageManagerCompat.LOG_TAG 只能從同一個庫中訪問(androidx.core:core)--- 未解決的參考:myUpdateOperation--- 未解決的參考:mySwipeRefreshLayout--- 預期變數
package com.example.whatsappstatussaver.fragments
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.content.PackageManagerCompat.LOG_TAG
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.whatsappstatussaver.R
import com.example.whatsappstatussaver.adapters.StatusAdapter
import com.example.whatsappstatussaver.dao.Dao
import com.example.whatsappstatussaver.utils.Constants
class Status() : Fragment(R.layout.fragment_status) {
private lateinit var path:String
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val sharedPref = requireContext().getSharedPreferences("dwa", Context.MODE_PRIVATE)
val bool = sharedPref.getBoolean("dwaBool",false)
path = if (bool){
Constants.STATUS_LOCATION_DW
}else{
Constants.STATUS_LOCATION
}
setupLayout(view)
mySwipeRefreshLayout.setOnRefreshListener(object : SwipeRefreshLayout.OnRefreshListener{
override fun onRefresh() {
Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout")
// This method performs the actual data-refresh operation.
// The method calls setRefreshing(false) when it's finished.
myUpdateOperation()
mySwipeRefreshLayout.isRefreshing = false
}
})
}
private fun setupLayout(view: View){
val filesList = Dao().getData(path, requireContext())
println(path)
val recyclerView = view.findViewById<RecyclerView>(R.id.statusRecyclerView)
recyclerView.layoutManager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
val adapter = StatusAdapter(requireContext(), filesList)
recyclerView.adapter = adapter
adapter.notifyDataSetChanged()
}
}
uj5u.com熱心網友回復:
我認為您只是忘記宣告 mySwipeToRefresh 元素。這是更正的代碼,我在 Activity 中實作了它,它很好地觸發了 myUpdateOperation() 函式。
lateinit var mySwipeRefreshLayout: SwipeRefreshLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mySwipeRefreshLayout = this.findViewById(R.id.swipeToRefresh)
mySwipeRefreshLayout.setOnRefreshListener { // Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout")
// This method performs the actual data-refresh operation.
// The method calls setRefreshing(false) when it's finished.
this.updateLayout()
}
}
private fun updateLayout() {
println("Updated")
mySwipeRefreshLayout.isRefreshing = false
}
uj5u.com熱心網友回復:
也許你可以這樣寫
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@ id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/418198.html
標籤:
