我正在使用FirestorePagingAdapter在我的应用程序中显示数据,但当我使用滑动刷新布局时,当我不滚动时,无法使用LOAD_MORE中的FirestorePaging Adapter、swipe refresh layout.isVisible = true
如何在FirestorePagingAdapter中使用滑动刷新布局?
ViewHolde.class:
class ViewHolde(item: View) : RecyclerView.ViewHolder(item){
val username = item.findViewById(R.id.iduser) as TextView
val title = item.findViewById(R.id.idtitle) as TextView
val decription = item.findViewById(R.id.iddes) as TextView
}
用户类别:
class User {
lateinit var userphoto: StorageReference
lateinit var username:String
lateinit var title:String
lateinit var notes: String
lateinit var id: String
var isSelected:Boolean = false
constructor(){
}
constructor( id: String?,username:String?,title: String?, notes: String?) {
this.title= title!!
this.id= id!!
this.username= username!!
this.notes= notes!!
}
}
RecyclerActivity.class:
class RecyclerActivity : AppCompatActivity(),SwipeRefreshLayout.OnRefreshListener {
var mylist=ArrayList<User>()
var rv:RecyclerView? = null
var currentuser = FirebaseAuth.getInstance().currentUser!!.uid
val str= FirebaseStorage.getInstance().reference
private var mAuthStateListener: FirebaseAuth.AuthStateListener? = null
var db = FirebaseFirestore.getInstance();
private val notebookRef2 = db.collection("ProfileData")
private val mQuery = notebookRef2.orderBy("title", Query.Direction.ASCENDING)
val config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(2)
.setPageSize(6)
.build()
val options = FirestorePagingOptions.Builder<User>()
.setLifecycleOwner(this)
.setQuery(mQuery, config, User::class.java)
.build()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycler)
rec()
var swipeContainer = findViewById(R.id.swipeContainer)as SwipeRefreshLayout
swipeContainer.setOnRefreshListener(this);
swipeContainer = findViewById(R.id.swipeContainer)as SwipeRefreshLayout
swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
swipeContainer.setOnRefreshListener {
rv?.invalidate();
swipeContainer.setRefreshing(false);
}
}
override fun onRefresh() {
Handler().postDelayed( Runnable() {
swipeContainer.setRefreshing(false);
}, 3000);
}
fun rec(){
rv=findViewById(R.id.idrecycler) as? RecyclerView
rv!!.setHasFixedSize(true)
rv!!.layoutManager = LinearLayoutManager(this)
db.collection("ProfileData").orderBy("title", Query.Direction.DESCENDING)
.addSnapshotListener { querySnapshot, firebaseFirestoreException ->
if (querySnapshot != null) {
for(da in querySnapshot) {
val note = da.toObject(Note::class.java)
var title = note.title
var description = note.notes
var username = note!!.username
var id = note!!.id
mylist.add(User(id!!,username!!, title!!, description!!))
}
}
}
setupadapter()
}
fun setupadapter(){
rv=findViewById(R.id.idrecycler) as? RecyclerView
var mAdapter = object : FirestorePagingAdapter<User, ViewHolde>(options) {
override fun onLoadingStateChanged(state: LoadingState) {
swipeRefreshLayout2.isVisible = false
when (state) {
LoadingState.LOADING_INITIAL -> {
swipeRefreshLayout2.isVisible = true
}
LoadingState.LOADING_MORE -> {
swipeRefreshLayout.isVisible = true
}
LoadingState.LOADED -> {
swipeRefreshLayout.isVisible = false
}
LoadingState.ERROR -> {
swipeRefreshLayout.isVisible = false
}
LoadingState.FINISHED -> {
swipeRefreshLayout.isVisible = false
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolde {
var v = LayoutInflater.from(parent.context)
.inflate(R.layout.iktem_list, parent, false)
return ViewHolde(v)
}
override fun onBindViewHolder(holder: ViewHolde, position: Int, p2: User) {
holder.username.text = mylist.get(position).username
holder.title.text = mylist.get(position).title
holder.decription.text = mylist.get(position).notes
}
}
rv!!.adapter = mAdapter
}
}
ActivityCycler.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iddrawer2"
tools:context=".RecyclerActivity">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_height="match_parent"
android:layout_width="match_parent" >
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4C00CF"
android:id="@+id/idtoolbar2"
tools:ignore="MissingConstraints"/>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_height="460dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/idrecycler"
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"
/>
</ScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<ProgressBar
android:id="@+id/swipeRefreshLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="340dp"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/swipeRefreshLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="450dp"
android:visibility="gone"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:headerLayout="@layout/header"
android:id="@+id/idnavigatuonv2"
android:layout_gravity="start"
app:menu="@menu/draw_menu"/>
</androidx.drawerlayout.widget.DrawerLayout >
所以我使用适配器来显示事务历史,并且我有一个按钮来显示过滤后的事务历史,状态为==成功。
点击按钮,我以以下方式更新适配器:
baseQuery = firestore.collection("transactions")
.whereEqualTo("status", "success")
.orderBy("__name__", Query.Direction.DESCENDING);
options = new FirestorePagingOptions.Builder<ItemTxn>()
.setLifecycleOwner(this)
.setQuery(baseQuery, config, ItemTxn.class)
.build();
adapter.updateOptions(options);
https://github.com/firebase/FirebaseUI-Android/issues/1726#issuecomment-573033035