提问人:Asikur Rahman 提问时间:8/21/2023 更新时间:8/21/2023 访问量:26
无法从 RecyclerView 获取点击的项目列表
Cant get clicked items list from RecyclerView
问:
我正在开发一个注册功能,用户可以根据点击的星星选择他们的技能和技能水平。后端 JSON 文件为:
"candidate_skill": [
{
"skill_id": 1,
"skill_level": 4
}
]
我设计的设计: [![在此输入图片描述][1]][1]
我需要这项工作,用户将选择他的技能水平,然后单击下一步,然后在下一个片段中传递一个列表。但目前我得到 0 尺寸列表。
适配器类:
class RegSkillAdapter(private val skills: List<String>) :
RecyclerView.Adapter<RegSkillAdapter.RegSkillViewholder>() {
private val selectedSkills = MutableList(skills.size) { false }
private val selectedSkillsList: ArrayList<SkillItem> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RegSkillViewholder {
val binding =
SkillSingleitemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return RegSkillViewholder(binding)
}
override fun getItemCount(): Int {
return skills.size
}
override fun onBindViewHolder(holder: RegSkillViewholder, position: Int) {
holder.bind(position)
}
fun getSelectedSkillsList(): List<SkillItem> {
return selectedSkillsList
}
inner class RegSkillViewholder(val binding: SkillSingleitemBinding) :
RecyclerView.ViewHolder(binding.root) {
init {
binding.rbSkill.setOnClickListener {
val adapterPosition = adapterPosition
if (adapterPosition != RecyclerView.NO_POSITION) {
selectedSkills[adapterPosition] = !selectedSkills[adapterPosition]
binding.skillstars.visibility =
if (selectedSkills[adapterPosition]) View.VISIBLE else View.GONE
binding.rbSkill.isChecked = selectedSkills[adapterPosition]
}
}
val context = itemView.context
val starsLayout = binding.skillstars
val star1 = binding.star1
val star2 = binding.star2
val star3 = binding.star3
val star4 = binding.star4
val star5 = binding.star5
star1.setOnClickListener {
updateStars(starsLayout, 1, context)
updateSelectedSkills(adapterPosition, 1)
}
star2.setOnClickListener {
updateStars(starsLayout, 2,context)
}
star3.setOnClickListener {
updateStars(starsLayout, 3,context)
}
star4.setOnClickListener {
updateStars(starsLayout, 4,context)
}
star5.setOnClickListener {
updateStars(starsLayout, 5,context)
}
}
fun bind(position: Int) {
binding.rbSkill.text = skills[position]
binding.skillstars.visibility =
if (selectedSkills[position]) View.VISIBLE else View.GONE
binding.rbSkill.isChecked = selectedSkills[position]
}
private fun updateStars(starsLayout: LinearLayout, selectedStars: Int, context: Context) {
for (i in 1..5) {
val grayStar = starsLayout.findViewById<ImageView>(
context.resources.getIdentifier(
"grystar$i",
"id",
context.packageName
)
)
val yellowStar = starsLayout.findViewById<ImageView>(
context.resources.getIdentifier(
"ylwstar$i",
"id",
context.packageName
)
)
if (i <= selectedStars) {
yellowStar.visibility = View.VISIBLE
grayStar.visibility = View.GONE
} else {
yellowStar.visibility = View.GONE
grayStar.visibility = View.VISIBLE
}
}
}
private fun updateSelectedSkills(adapterPosition: Int, selectedStars: Int) {
val skillId = adapterPosition + 1
val skillItem = SkillItem(skillId, selectedStars)
val existingSkillIndex = selectedSkillsList.indexOfFirst { it.skillId == skillId }
if (existingSkillIndex != -1) {
selectedSkillsList[existingSkillIndex] = skillItem
Log.d("EWNARRAYRegSkillAdapter", "Updated skill: $skillItem")
} else {
selectedSkillsList.add(skillItem)
Log.d("EWNARRAYRegSkillAdapter", "Added skill: $skillItem")
}
}}}
列表的数据类:
import android.os.Parcelable
导入 kotlinx.parcelize.Parcelize
@Parcelize 数据类 SkillItem(val skillId: Int, val skillLevel: Int):P arcelable
RegSkillFragment:
class RegisterSkillFragment : Fragment(), View.OnClickListener {
private lateinit var binding: FragmentRegisterSkillBinding
private lateinit var regSkillAdapter: RegSkillAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentRegisterSkillBinding.inflate(inflater, container, false)
val skillList = ArrayList<String>()
skillList.add("Blockchain")
skillList.add("Cloud Computing")
skillList.add("English")
skillList.add("Art & Culture")
skillList.add("Blockchain")
skillList.add("Blockchain")
skillList.add("Blockchain")
skillList.add("Blockchain")
skillList.add("Blockchain")
skillList.add("Blockchain")
skillList.add("Blockchain")
regSkillAdapter = RegSkillAdapter(skillList)
binding.rvSkill.apply {
layoutManager = LinearLayoutManager(context)
adapter = regSkillAdapter
}
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setOnclicklistener()
}
private fun setOnclicklistener() {
binding.btnBack.setOnClickListener(this)
binding.btnNext.setOnClickListener(this)
}
override fun onClick(v: View) {
when (v.id) {
R.id.btnBack -> {
Navigation.findNavController(requireView())
.navigate(R.id.action_registerSkillFragment_to_loginFragment)
}
R.id.btnNext -> {
val selectedSkillsList = regSkillAdapter.getSelectedSkillsList()
val parcelableList: ArrayList<SkillItem?> = ArrayList(selectedSkillsList)
val bundle = Bundle()
bundle.putParcelableArrayList("selectedSkillsList", parcelableList)
Log.d("EWNARRAY", "skill list : ${parcelableList.size}")
Log.d("EWNARRAYRegisterSkillFragment", "Selected skills list size: ${selectedSkillsList.size}")
// Log each item individually
for (skill in selectedSkillsList) {
Log.d("EWNARRAY", "Skill Id: ${skill.skillId}, Skill Level: ${skill.skillLevel}")
}
Navigation.findNavController(requireView())
.navigate(R.id.action_registerSkillFragment_to_registerFragment, bundle)
/* val registerFragment = RegisterFragment()
registerFragment.arguments = bundle
Navigation.findNavController(requireView())
.navigate(R.id.action_registerSkillFragment_to_loginFragment)*/
}
}
}
}
房车单品:
<?xml version="1.0" encoding="utf-8"?>
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_padding20"
android:layout_marginTop="@dimen/margin_padding10"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:button="@null"
android:checked="false"
android:drawableStart="@drawable/custom_radiobutton"
android:drawablePadding="8dp"
android:fontFamily="@font/outfit_regular"
android:gravity="center"
style="@style/RadioButtonNoBackground"
android:textColor="@color/shark"
android:textSize="@dimen/font16" />
</LinearLayout>
<LinearLayout
android:id="@+id/skillstars"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:visibility="visible">
<RelativeLayout
android:id="@+id/star1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_padding10"
android:layout_marginTop="@dimen/margin_padding5"
android:layout_marginEnd="@dimen/margin_padding10"
android:layout_marginBottom="@dimen/margin_padding5"
android:background="@drawable/lauout_bg_white"
android:elevation="3dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:paddingEnd="10dp"
android:paddingBottom="5dp">
<ImageView
android:id="@+id/grystar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/grystar" />
<ImageView
android:id="@+id/ylwstar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ylwstar"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/star2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_padding10"
android:layout_marginTop="@dimen/margin_padding5"
android:layout_marginEnd="@dimen/margin_padding10"
android:layout_marginBottom="@dimen/margin_padding5"
android:background="@drawable/lauout_bg_white"
android:elevation="3dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:paddingEnd="10dp"
android:paddingBottom="5dp">
<ImageView
android:id="@+id/grystar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/grystar" />
<ImageView
android:id="@+id/ylwstar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ylwstar"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/star3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_padding10"
android:layout_marginTop="@dimen/margin_padding5"
android:layout_marginEnd="@dimen/margin_padding10"
android:layout_marginBottom="@dimen/margin_padding5"
android:background="@drawable/lauout_bg_white"
android:elevation="3dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:paddingEnd="10dp"
android:paddingBottom="5dp">
<ImageView
android:id="@+id/grystar3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/grystar" />
<ImageView
android:id="@+id/ylwstar3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ylwstar"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/star4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_padding10"
android:layout_marginTop="@dimen/margin_padding5"
android:layout_marginEnd="@dimen/margin_padding10"
android:layout_marginBottom="@dimen/margin_padding5"
android:background="@drawable/lauout_bg_white"
android:elevation="3dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:paddingEnd="10dp"
android:paddingBottom="5dp">
<ImageView
android:id="@+id/grystar4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/grystar" />
<ImageView
android:id="@+id/ylwstar4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ylwstar"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/star5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_padding10"
android:layout_marginTop="@dimen/margin_padding5"
android:layout_marginEnd="@dimen/margin_padding10"
android:layout_marginBottom="@dimen/margin_padding5"
android:background="@drawable/lauout_bg_white"
android:elevation="3dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:paddingEnd="10dp"
android:paddingBottom="5dp">
<ImageView
android:id="@+id/grystar5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/grystar" />
<ImageView
android:id="@+id/ylwstar5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ylwstar"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
你能看出问题出在哪里吗? [1]:https://i.stack.imgur.com/7LzIE.jpg
答: 暂无答案
评论
Log.d("EWNARRAYRegSkillAdapter", "Added skill: $skillItem")