我坚持使用groupie适配器,无法弄清楚我在这里出了什么问题。
问题1 适配器onclick侦听器仅在第二次单击时有效,
问题-2 当我尝试更新部分时,它会更新整个部分(包括其中没有任何更改的某些部分),从而更改其中的项目ID。以下是代码。
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.View.INVISIBLE
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.squareup.picasso.Picasso
import com.sstech.eventmanager.R
import com.sstech.eventmanager.databinding.MainHomeFragmentBinding
import com.sstech.eventmanager.datamodel.EventModel
import com.sstech.eventmanager.datamodel.UserModel
import com.sstech.eventmanager.mainUI.AppMainActivityViewModel
import com.sstech.eventmanager.mainUI.CreateEvent
import com.sstech.eventmanager.methods.TimeFunctions
import com.xwray.groupie.*
import kotlinx.android.synthetic.main.home_info.view.*
import kotlinx.android.synthetic.main.item_header.view.*
import kotlinx.android.synthetic.main.event_info.view.*
class MainHome : Fragment() {
companion object {
fun newInstance() = MainHome()
}
lateinit var binding: MainHomeFragmentBinding
private lateinit var viewModel: MainHomeViewModel
private lateinit var viewModelMain: AppMainActivityViewModel
private lateinit var viewModelFactory: MainHomeViewModelFactory
private lateinit var updatingGroupInfo: Section
private lateinit var updatingGroupItem: Section
var adaptor = GroupAdapter<GroupieViewHolder>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// TODO : Navigation path, Add Adaptor
Log.i("mn", "Home Oncreate")
binding =
DataBindingUtil.inflate(layoutInflater, R.layout.main_home_fragment, container, false)
val userdata = UserModel()
viewModelFactory = MainHomeViewModelFactory(userdata)
viewModel = ViewModelProviders.of(this, viewModelFactory).get(MainHomeViewModel::class.java)
activity?.let {
viewModelMain = ViewModelProviders.of(it).get(AppMainActivityViewModel::class.java)
}
if (viewModel.userdata.value?.uid.isNullOrEmpty()) {
viewModel.updateDashboard()
viewModel.getActiveGroups()
}
// recycleView Setup
adaptor = GroupAdapter()
adaptor.setOnItemClickListener(onItemClickListener)
updatingGroupInfo = Section()
updatingGroupInfo.addAll(ArrayList<infoItem>())
adaptor.add(updatingGroupInfo)
val updatingSection = Section()
updatingSection.setHeader(HeaderItem("Active Event"))
updatingGroupItem = Section()
updatingGroupItem.addAll(ArrayList<listItem>())
updatingSection.add(updatingGroupItem)
// updatingGroupItem.
adaptor.add(updatingSection)
//
viewModel.userdata.observe(this, Observer {
Log.i("mn", "uid is ${it.uid}")
if (it.uid.isNotEmpty()) {
viewModelMain.userdata = it
Log.i("mn", "adaptor added")
val temp_array = ArrayList<infoItem>()
temp_array.add(infoItem(it))
updatingGroupInfo.update(temp_array)
}
})
viewModel.eventData.observe(this, Observer {
if (it.isEmpty()) return@Observer //TODO: Add empty to fill screen
val tempArray = ArrayList<listItem>()
for (eventModel in it) {
tempArray.add(listItem(eventModel, viewModel.userdata.value!!.uid))
}
updatingGroupItem.update(tempArray)
})
binding.mainHomeRecycle.adapter = adaptor
return binding.root
}
override fun onDestroy() {
super.onDestroy()
Log.i("mn", "Home Destroyed")
}
override fun onResume() {
super.onResume()
viewModelMain.currentlyDisplayed(R.id.mainHome)
}
class infoItem(val data: UserModel) : Item<GroupieViewHolder>() {
override fun getLayout(): Int {
return R.layout.home_info
}
override fun getItem(position: Int): Item<*> {
return super.getItem(position)
}
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
Log.i("mn", "Adaptor Position : $position")
viewHolder.itemView.display_name.text = data.username
viewHolder.itemView.display_phno.text = data.phno
viewHolder.itemView.medal1.text = data.status.toString()
if (data.status) {
viewHolder.itemView.user_status.setImageResource(R.drawable.ic_user_verified)
} else {
viewHolder.itemView.user_status.setImageResource(R.drawable.ic_user_not_verified)
}
Picasso.get().load(data.imageurl).into(viewHolder.itemView.display_profilePic)
viewHolder.itemView.create_event.setOnClickListener {
launchCreateEvent(viewHolder)
}
}
fun launchCreateEvent(viewHolder: GroupieViewHolder) {
val context = viewHolder.itemView.context
val intent = Intent(context, CreateEvent::class.java)
intent.putExtra("uid", data.uid)
intent.putExtra("username", data.username)
context.startActivity(intent)
}
}
class listItem(val data: EventModel, val uid: String) : Item<GroupieViewHolder>() {
override fun getLayout(): Int {
return R.layout.event_info
}
@SuppressLint("ResourceAsColor")
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.itemView.event_name.text = data.name
if (data.admin == uid) {
viewHolder.itemView.admin.text = "ADMIN"
viewHolder.itemView.admin.setTextColor(Color.RED)
viewHolder.itemView.admin.setCompoundDrawablesRelativeWithIntrinsicBounds(
R.drawable.ic_security,
0,
0,
0
)
} else {
viewHolder.itemView.admin.text = "RACER"
viewHolder.itemView.admin.setTextColor(Color.GREEN)
viewHolder.itemView.admin.setCompoundDrawablesRelativeWithIntrinsicBounds(
R.drawable.ic_eventr,
0,
0,
0
)
}
viewHolder.itemView.date.text = TimeFunctions().getdate(data.eventdate, "dd")
viewHolder.itemView.day.text = TimeFunctions().getdate(data.eventdate, "EEEE")
viewHolder.itemView.month.text = TimeFunctions().getdate(data.eventdate, "MMM,yyyy")
viewHolder.itemView.club_name.text = data.club
viewHolder.itemView.event_location.text = data.displaylocation
viewHolder.itemView.event_id.text = data.id
if (!data.eventstarted) {
viewHolder.itemView.live.visibility = INVISIBLE
} else {
viewHolder.itemView.live.visibility = VISIBLE
}
// viewHolder.itemView.setOnClickListener { onItemClickListener }
}
// private val onItemClickListener = OnItemClickListener { item, _ ->
// if (item is listItem) {
//// Toast.makeText(activity, "clicked ${item.id}", Toast.LENGTH_SHORT).show()
// }
// }
}
class HeaderItem(val data: String) : Item<GroupieViewHolder>() {
override fun getLayout(): Int {
return R.layout.item_header
}
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.itemView.text1.text = data
}
}
private val onItemClickListener = OnItemClickListener { item, _ ->
if (item is listItem) {
Toast.makeText(activity, "clicked ${item.id}", Toast.LENGTH_SHORT).show()
}
}
}
如果我第一次单击某个项目,则“ onItemCLickListener”不起作用,如果我第二次单击,则它仅起作用。 每次更新一个项目时,整个部分都会刷新(我认为分组查看器使用diffUtil)