I have the following layout code in my xlm, to call my recycler view in my status fragment
<?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>
I tried to call this swipe to refresh using this in my main activity, but no sucessfull so now I am attempting to use in my fragment file, the one I used the xml to get the recyclerView,and here are the errors are showen:
Unresolved reference: mySwipeRefreshLayout—
PackageManagerCompat.LOG_TAG can only be accessed from within the same library (androidx.core:core)—
Unresolved reference: myUpdateOperation—
Unresolved reference: mySwipeRefreshLayout—
Variable expected
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()
}
}
2
Answers
I think you only forgot to declare the mySwipeToRefresh element.
This is the corrected code, I implemented it inside an Activity and it triggers the myUpdateOperation() function fine.
Maybe you can write that