我正试图找出如何显示过滤(搜索)列表与正确的排名显示在回收视图在开始…
所以,我想说的是:(Issue Link)
https://mega.nz/file/cksjAQgD#ofyhLQOd3KW4nuw2NShrPLDncj4Fz6hSoWdN5jVbERc
当我从搜索框中搜索用户时,用户排名应显示为在循环视图中分配的排名(未搜索)
我的代码
class LeaderboardFragment : BaseFragment() {
private lateinit var list : ArrayList<User>
private lateinit var binding: FragmentLeaderboardBinding
private lateinit var filteredLeaderBoardList:ArrayList<User>
private lateinit var leaderBoardUserList:ArrayList<User>
companion object{
var list=ArrayList<User>()
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding=FragmentLeaderboardBinding.inflate(inflater,container,false)
binding.toolbarDashboard.inflateMenu(R.menu.leaderboard_top)
activity?.window!!.statusBarColor = requireActivity().getColor(R.color.dark_green)
leaderBoardUserList = kotlin.collections.ArrayList<User>()
//LeaderBoardSearchFunctionality
binding.searchBox.clearFocus()
binding.searchBox.setOnQueryTextListener(object:OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
filterLeaderBoardList(newText)
return true
}
})
// list=ArrayList<User>()
// list.add(User("1","Pratyush","aries.@gmail.com","8.4"))
// list.add(User("2","Ayush","aries.@gmail.com","7.2"))
// list.add(User("3","Abhiram","aries.@gmail.com","6.8"))
// list.add(User("4","Aditya","aries.@gmail.com","5.3"))
// list.add(User("5","Samyak","aries.@gmail.com","5.1"))
// list.add(User("6","Rahul","aries.@gmail.com","4.9"))
// list.add(User("7","Ankur","aries.@gmail.com","3.0"))
// list.add(User("8","Adiii","aries.@gmail.com","2.5"))
// list.add(User("9","Prince","aries.@gmail.com","1.7"))
// list.add(User("10","Ritik","aries.@gmail.com","0.8"))
return binding.root
}
private fun filterLeaderBoardList(newText: String?) {
filteredLeaderBoardList = ArrayList<User>()
for(item in leaderBoardUserList){
if(item.name.lowercase().contains(newText!!.lowercase()))
{
filteredLeaderBoardList.add(item)
}
}
if(filteredLeaderBoardList.isEmpty()){
showErrorSnackBar("No User Found",true)
}else{
binding.rvLeaderboard.adapter=LeaderboardAdapter(requireContext(),filteredLeaderBoardList)
}
}
fun getLeaderBoard(userList: ArrayList<User>) {
userList.sortByDescending { it.distance.toDouble() }
hideProgressDialog()
binding.rvLeaderboard.adapter=LeaderboardAdapter(requireContext(),userList)
binding.rvLeaderboard.layoutManager=LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false)
}
fun getUserList(userList:ArrayList<User>){
//filter list issue pratyush singh is at bottom with 7.9km should at pos 2
leaderBoardUserList = userList
getTopUser(userList)
}
override fun onResume() {
super.onResume()
showProgressDialog()
FirestoreClass().getLeaderboardFragment(this)
}
private fun getTopUser(userList: ArrayList<User>) {
binding.nameOfUser.text = userList.first().name
binding.emailOfUser.text = userList.first().email
binding.distanceCovered.text = userList.first().distance+" km"
}
}
LeaderboardAdapter.kt
class LeaderboardAdapter(val context: Context, private var list: ArrayList<User>) :
RecyclerView.Adapter<LeaderboardAdapter.LeaderViewHolder>() {
inner class LeaderViewHolder(val binding: ItemLeaderboardBinding) :
RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LeaderViewHolder {
return LeaderViewHolder(
ItemLeaderboardBinding.inflate(
LayoutInflater.from(context),
parent,
false
)
)
}
override fun onBindViewHolder(holder: LeaderViewHolder, position: Int) {
val model = list[position]
Log.d("listelements","$model")
holder.binding.positionOfUser.text = (position+1).toString()+" . "
holder.binding.nameOfUser.text = model.name
holder.binding.emailOfUser.text = model.email
holder.binding.distanceCovered.text = model.distance
}
override fun getItemCount(): Int {
return list.size
}
// fun setLeaderBoardQuery(filteredList:ArrayList<User>){
// this.list = filteredList
// notifyDataSetChanged()
// }
}
有谁能帮我弄清楚吗?如您所见,您正在使用用户的位置作为他们的等级。你应该做的是用那个人在数据类中的排名(位置)来显示那个人。否则,LeaderboardAdapter
会产生错误的数据。在你的例子中:
class LeaderboardAdapter(val context: Context, private var list: ArrayList<User>) :
RecyclerView.Adapter<LeaderboardAdapter.LeaderViewHolder>() {
inner class LeaderViewHolder(val binding: ItemLeaderboardBinding) :
RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LeaderViewHolder {
return LeaderViewHolder(
ItemLeaderboardBinding.inflate(
LayoutInflater.from(context),
parent,
false
)
)
}
override fun onBindViewHolder(holder: LeaderViewHolder, position: Int) {
val model = list[position]
Log.d("listelements", "$model")
// As you can see, we use model's position variable instead of position argument
holder.binding.positionOfUser.text = model.position.toString() + " . "
holder.binding.nameOfUser.text = model.name
holder.binding.emailOfUser.text = model.email
holder.binding.distanceCovered.text = model.distance
}
override fun getItemCount(): Int {
return list.size
}
// fun setLeaderBoardQuery(filteredList:ArrayList<User>){
// this.list = filteredList
// notifyDataSetChanged()
// }
}
另一方面,我强烈建议不要在每次搜索时设置适配器,而是更改适配器内的对象。如果使用DiffUtil
就更好了编辑:
我将改变这个getLeaderBoard()函数在LeaderboardFragment:
fun getLeaderBoard(userList: ArrayList<User>) {
val sortedList = userList.sortByDescending { it.distance.toDouble() }
.mapIndexed { index, item -> item.copy(position = index + 1) }
hideProgressDialog()
binding.rvLeaderboard.adapter = LeaderboardAdapter(requireContext(), sortedList)
binding.rvLeaderboard.layoutManager =
LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
}