我的主页片段中有这个 Searchview,我想通过在“帖子”上搜索标题值来过滤我的 recyclerview。我已经搜索了一些教程,但大多数使用“FirebaseRecyclerOptions”,但就我而言,我使用列表的默认 RecyclerView。我的尝试都没有达到我的预期。
这是数据库结构。
"Posts": {
"-NNsor5WiX9wxTDf7qo5": {
"category": "Art",
"description": "tes 123",
"duration": "-",
"paymentMethods": "e-wallet",
"postId": "-NNsor5WiX9wxTDf7qo5",
"post_image": "https://firebasestorage.googleapis.com/v0/b/final-project-idn.appspot.com/o/Posts%20Pictures%2F1675990621049.jpg?alt=media&token=f7b747c0-1efe-4f43-9ff1-729c7d99c7c1",
"priceRange": "Rp 0 - 10.000",
"publisher": "6FzpJ2sQC9gewbUIGHB8hiNWK1z2",
"title": "tes"
}
},
"User": {
"6FzpJ2sQC9gewbUIGHB8hiNWK1z2": {
"background_image": "https://firebasestorage.googleapis.com/v0/b/final-project-idn.appspot.com/o/Default%20Images%2Fimg_background.png?alt=media&token=b51e7cf5-2015-4cc4-9583-159373d081e2",
"bio": "Member of NCT",
"email": "mark@gmail.com",
"link": "",
"profile_image": "https://firebasestorage.googleapis.com/v0/b/final-project-idn.appspot.com/o/Profile%20Pictures%2F6FzpJ2sQC9gewbUIGHB8hiNWK1z2.jpg?alt=media&token=d174fad9-2f8c-4964-83ec-549f18876587",
"uid": "6FzpJ2sQC9gewbUIGHB8hiNWK1z2",
"userName": "marklee"
}
}
}
这里是家庭片段。
class HomeFragment : Fragment(){
private lateinit var refUsers : DatabaseReference
private var firebaseUser: FirebaseUser? = null
private var postAdapter: PostAdapter? = null
private var postList: MutableList<Post>? = null
private var dataList = ArrayList<Post>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val bind = FragmentHomeBinding.inflate(layoutInflater)
bind.cvCategoryArt.setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.mainContainer, CategoryArtFragment()).addToBackStack(null).commit()
}
bind.cvCategoryWriting.setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.mainContainer, CategoryWritingFragment()).addToBackStack(null).commit()
}
bind.cvCategoryApplication.setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.mainContainer, CategoryApplicationFragment()).addToBackStack(null).commit()
}
bind.cvCategoryDesign.setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.mainContainer, CategoryDesignFragment()).addToBackStack(null).commit()
}
//RecyclerView
var recyclerView: RecyclerView? = null
recyclerView = bind.rvHome
val linearLayoutManager = LinearLayoutManager(context)
linearLayoutManager.reverseLayout = true
linearLayoutManager.stackFromEnd = true
recyclerView.layoutManager = linearLayoutManager
refUsers = FirebaseDatabase.getInstance().reference.child("Posts")
postList = ArrayList()
dataList = ArrayList()
val fragmentManager = parentFragmentManager
postAdapter = context?.let { PostAdapter(it, postList as ArrayList<Post>,fragmentManager) }
recyclerView.adapter = postAdapter
bind.pbHomeRecycler.isVisible = true
retrievePostHome(bind)
return bind.root
}
private fun retrievePostHome(bind: FragmentHomeBinding) {
firebaseUser = FirebaseAuth.getInstance().currentUser
val postsRef = FirebaseDatabase.getInstance().reference.child("Posts")
postsRef.addValueEventListener(object : ValueEventListener {
@SuppressLint("NotifyDataSetChanged")
override fun onDataChange(p0: DataSnapshot) {
postList?.clear()
bind.pbHomeRecycler.isVisible = false
for (snapshot in p0.children){
val post = snapshot.getValue(Post::class.java)
if (post!!.getPublisher() != firebaseUser!!.uid){
postList!!.add(post)
}
postAdapter!!.notifyDataSetChanged()
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
}
这是主页片段 xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.HomeFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clHome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
<ImageView
android:id="@+id/ivHomeLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="@dimen/_20dp"
android:layout_marginTop="@dimen/_30dp"
android:src="@drawable/app_logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_bold"
android:text="@string/app_name"
android:textColor="@color/blue"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="@id/ivHomeLogo"
app:layout_constraintStart_toEndOf="@id/ivHomeLogo"
app:layout_constraintTop_toTopOf="@id/ivHomeLogo" />
<androidx.appcompat.widget.SearchView
android:id="@+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/bg_et_search"
android:drawablePadding="12dp"
android:ems="10"
app:queryBackground="@android:color/transparent"
android:focusable="false"
app:iconifiedByDefault="false"
app:queryHint="Search Post.."
app:searchIcon="@drawable/ic_search"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ivHomeLogo"/>
<HorizontalScrollView
android:id="@+id/hsvHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/etSearch"
app:layout_constraintTop_toBottomOf="@+id/etSearch">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_10dp"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/cvCategoryArt"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="@color/blue"
app:cardCornerRadius="@dimen/_10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="40dp"
android:layout_marginVertical="8dp"
android:fontFamily="@font/poppins_semibold"
android:text="Art"
android:textColor="@color/white"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_10dp"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/cvCategoryWriting"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="@color/blue_light"
app:cardCornerRadius="@dimen/_10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="40dp"
android:layout_marginVertical="8dp"
android:fontFamily="@font/poppins_semibold"
android:text="Writing"
android:textColor="@color/blue"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_10dp"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/cvCategoryApplication"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="@color/blue"
app:cardCornerRadius="@dimen/_10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="40dp"
android:layout_marginVertical="8dp"
android:fontFamily="@font/poppins_semibold"
android:text="Application"
android:textColor="@color/white"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingEnd="@dimen/_20dp">
<androidx.cardview.widget.CardView
android:id="@+id/cvCategoryDesign"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="@dimen/_20dp"
app:cardBackgroundColor="@color/blue_light"
app:cardCornerRadius="@dimen/_10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="40dp"
android:layout_marginVertical="8dp"
android:fontFamily="@font/poppins_semibold"
android:text="Design"
android:textColor="@color/blue"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvHome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/clHome"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="@dimen/_10dp"
tools:listitem="@layout/item_timeline" />
</RelativeLayout>
<ProgressBar
android:id="@+id/pbHomeRecycler"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content" />
</FrameLayout>
这是帖子的适配器。
class PostAdapter(
private val mContext: Context,
private var mPost: List<Post>,
private val mParentFragmentManager: FragmentManager,
) : RecyclerView.Adapter<PostAdapter.ViewHolder>() {
private var firebaseUser: FirebaseUser? = null
inner class ViewHolder(@NonNull itemView: View) : RecyclerView.ViewHolder(itemView) {
var profileImage: CircleImageView
var tvUsername: TextView
var likeButton: ImageView
var tvTitle: TextView
var tvPrice: TextView
var tvDescription: TextView
var tvSeeMore: TextView
var postImage: ImageView
var postCard : CardView
init {
profileImage = itemView.findViewById(R.id.cvProfile)
tvUsername = itemView.findViewById(R.id.tvCvUsername)
likeButton = itemView.findViewById(R.id.cvFav)
tvTitle = itemView.findViewById(R.id.tvCvTitle)
tvPrice = itemView.findViewById(R.id.tvCvPrice)
tvDescription = itemView.findViewById(R.id.tvCvDesc)
tvSeeMore = itemView.findViewById(R.id.tvCvSeeMore)
postImage = itemView.findViewById(R.id.ivPostImage)
postCard = itemView.findViewById(R.id.cvImagePost)
tvSeeMore.setOnClickListener{
val publisherId = mPost[adapterPosition].getPublisher()
val postId = mPost[adapterPosition].getPostId()
mParentFragmentManager.beginTransaction()
.replace(R.id.mainContainer, DetailPostFragment(publisherId, postId)).addToBackStack(null).commit()
}
profileImage.setOnClickListener {
firebaseUser = FirebaseAuth.getInstance().currentUser
val publisherId = mPost[adapterPosition].getPublisher()
val postId = mPost[adapterPosition].getPostId()
if (publisherId != firebaseUser!!.uid){
mParentFragmentManager.beginTransaction()
.replace(R.id.mainContainer, ProfileFragment(publisherId, postId)).addToBackStack(null).commit()
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(mContext).inflate(R.layout.item_timeline, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
firebaseUser = FirebaseAuth.getInstance().currentUser
val post = mPost[position]
if (post.getPost_image() == ""){
holder.postCard.isVisible = false
}else{
Glide.with(mContext.applicationContext).load(post.getPost_image()).into(holder.postImage)
}
holder.tvTitle.text = post.getTitle()
holder.tvDescription.text = post.getDescription()
holder.tvPrice.text = post.getPriceRange()
publisherInfo(holder.profileImage, holder.tvUsername, post.getPublisher())
}
private fun publisherInfo(
profileImage: CircleImageView,
tvUsername: TextView,
publisher: String
) {
val usersRef = FirebaseDatabase.getInstance().reference.child("User").child(publisher)
usersRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
val user = snapshot.getValue(Users::class.java)
Glide.with(mContext.applicationContext).load(user!!.getProfile_image())
.into(profileImage)
tvUsername.text = user!!.getUserName()
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
override fun getItemCount(): Int {
return mPost.size
}
}
任何建议或解决方案?我真的很感激。谢谢