我正在工作的程序,我需要切换之间的片段。 我创建了recyclerview,当我点击整个recyclerview项目时,我需要切换到特定的片段。 我如何使用我的适配器来做这件事?
myMainFragment代码:
class MastersFragment : Fragment(R.layout.fragment_masters) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val arrayList = ArrayList<MasterModel>()
arrayList.add(MasterModel("Sakurami", "Lorem Ipsum is simply dummy text\\nof the printing and
typesetting industry.", "Aliya Alieva", R.drawable.image_1__2_,
R.drawable.ic_building_place_of_work,
R.drawable.ic_btn_not_filled_favourite,R.drawable.ic_circle, "4,7", "Косметолог"))
arrayList.add(MasterModel("Sakurami", "Lorem Ipsum is simply dummy text\\nof the printing and
typesetting industry.", "Aliya Alieva", R.drawable.image_1__2_,
R.drawable.ic_building_place_of_work,
R.drawable.ic_btn_not_filled_favourite,R.drawable.ic_circle,"4,7", "Косметолог"))
arrayList.add(MasterModel("Sakurami", "Lorem Ipsum is simply dummy text\\nof the printing and
typesetting industry.", "Aliya Alieva", R.drawable.image_1__2_,
R.drawable.ic_building_place_of_work,
R.drawable.ic_btn_not_filled_favourite,R.drawable.ic_circle, "4,7", "Косметолог"))
arrayList.add(MasterModel("Sakurami", "Lorem Ipsum is simply dummy text\\nof the printing and
typesetting industry.", "Aliya Alieva", R.drawable.image_1__2_,
R.drawable.ic_building_place_of_work,
R.drawable.ic_btn_not_filled_favourite,R.drawable.ic_circle,"4,7","Косметолог"))
arrayList.add(MasterModel("Sakurami", "Lorem Ipsum is simply dummy text\\nof the printing and
typesetting industry.", "Aliya Alieva", R.drawable.image_1__2_,
R.drawable.ic_building_place_of_work,
R.drawable.ic_btn_not_filled_favourite,R.drawable.ic_circle, "4,7", "Косметолог"))
arrayList.add(MasterModel("Sakurami", "Lorem Ipsum is simply dummy text\\nof the printing and
typesetting industry.", "Aliya Alieva", R.drawable.image_1__2_,
R.drawable.ic_building_place_of_work,
R.drawable.ic_btn_not_filled_favourite,R.drawable.ic_circle,"4,7", "Косметолог"))
arrayList.add(MasterModel("Sakurami", "Lorem Ipsum is simply dummy text\\nof the printing and
typesetting industry.", "Aliya Alieva", R.drawable.image_1__2_,
R.drawable.ic_building_place_of_work,
R.drawable.ic_btn_not_filled_favourite,R.drawable.ic_circle, "4,7", "Косметолог"))
val masterAdapter = MasterAdapter(arrayList, requireContext())
master_recyclerview.layoutManager = LinearLayoutManager(requireContext())
master_recyclerview.adapter = masterAdapter
}
}
myadapter代码:
class MasterAdapter(val arrayList: ArrayList<MasterModel>, val context: Context) :
RecyclerView.Adapter<MasterAdapter.ViewHolder>() {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(model: MasterModel) {
itemView.locationTextSalonContainer.text = model.title
itemView.descriptionSalon.text = model.des
itemView.titleTextSalonContainer.text = model.name
itemView.descriptionIconSalonContainer.setImageResource(model.icon_circle)
itemView.titleIconSalonContainer.setImageResource(model.icon_building)
itemView.salonContainerImage.setImageResource(model.image)
itemView.textRating.text = model.rating
itemView.textProfession.text = model.profession
itemView.like.setImageResource(model.icon_heart)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.masters_recyclerview, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return arrayList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindItems(arrayList[position])
holder.itemView.like.setOnClickListener {
Toast.makeText(context, "You clicked like", Toast.LENGTH_SHORT).show()
}
holder.itemView.setOnClickListener {
if(position == 0) {
Toast.makeText(context, "You clicked 1", Toast.LENGTH_SHORT).show()
}
if(position == 1) {
Toast.makeText(context, "You clicked 2", Toast.LENGTH_SHORT).show()
}
if(position == 2) {
Toast.makeText(context, "You clicked 3", Toast.LENGTH_SHORT).show()
}
if(position == 3) {
Toast.makeText(context, "You clicked 4", Toast.LENGTH_SHORT).show()
}
if(position == 4) {
Toast.makeText(context, "You clicked 5", Toast.LENGTH_SHORT).show()
}
if(position == 5) {
Toast.makeText(context, "You clicked 6", Toast.LENGTH_SHORT).show()
}
if(position == 6) {
Toast.makeText(context, "You clicked 7", Toast.LENGTH_SHORT).show()
}
if(position == 7) {
Toast.makeText(context, "You clicked 8", Toast.LENGTH_SHORT).show()
}
}
}
}
我需要切换到的配置文件配置代码:
class MasterProfileFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_master_profile, container, false)
val tabLayout = view.findViewById(R.id.tabMaster) as TabLayout
val viewPager = view.findViewById(R.id.viewPager) as ViewPager
val fragmentAdapter = MyAdapter(childFragmentManager)
viewPager.adapter = fragmentAdapter
tabMaster.setupWithViewPager(viewPager)
return view
}
private inner class MyAdapter(fragmentManager: FragmentManager?) :
FragmentPagerAdapter(fragmentManager!!, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int): Fragment {
return when(position) {
0 -> CommentsFragment()
else -> ServiceFragment()
}
}
override fun getCount(): Int {
return 2
}
override fun getPageTitle(position: Int): CharSequence? {
return when(position) {
0 -> "Отзывы"
else -> "Услуги"
}
}
}
我需要通过使用RecycerView切换到另一个片段,特别是从MainFragment到ProfileFragment代码,这是在这一行上面给出的。 我怎么能做到呢? 有什么解决办法吗?
为此,如果您使用Kotlin,则可以使用lambda函数将片段与适配器通信,将适配器与查看器通信:
我找到的最好的方法是使用lambdas+密封类,你可以这样声明你的事件:
sealed class PostEvent(post: Post)
data class LikePostEvent(val post: Post) : PostEvent(post)
data class SharePostEvent(val post: Post) : PostEvent(post)
data class ReportPostEvent(val post: Post) : PostEvent(post)
在这个例子中,我们有一个Post项目,我们可以喜欢,分享或报告。
然后传递lambda函数,从查看器和适配器类返回事件:
class PostViewHolder(itemView: View, val onEvent:(PostEvent) -> Unit)
class PostsAdapter(val onEvent:(PostEvent) -> Unit)
在viewholder的bind函数中,您可以像下面这样发送事件:
fun bind(post: Post) {
btnLike.setOnClickListener { onEvent(LikePostEvent(post)) }
btnReport.setOnClickListener { onEvent(ReportPostEvent(post)) }
btnShare.setOnClickListener { onEvent(SharePostEvent(post)) }
}
然后在您的片段中,您可以传递lambda并像这样使用它:
// Pass the reference when creating the adapter
val adapter = PostsAdapter(::onEvent)
// Use the same signature
fun onEvent(event:PostEvent) {
when(event) {
is LikePostEvent -> {}
is SharePostEvent -> {}
is ReportPostEvent -> {}
}
}
我写过一篇关于这方面的文章,我还有一个提高性能的附加主题,看看吧:https://euryperez.dev/kotlin/betle-a-lambda-functions-jedi-kotlin/
编辑:要检测整个项目中的点击,只需在itemview中添加一个点击侦听器,如:
itemView.setOnClickListener { // send your event here }