你好,我一直试图分离我的洗衣机列表,使他们在网格视图中一个接一个,但由于某种原因,他们一直像在图像中一样粘在一起。[![在这里输入图像描述][1]][1]
我的片段,它的大部分工作都在后台进行。
class HomeFragment : Fragment() {
private lateinit var binding: FragmentHomeBinding
private val viewModel by KoinJavaComponent.inject(HomeFragmentVM::class.java)
private val sp by inject<SPManager>()
private var profile : Data? = null
private val itemList: Array<String>
get() = arrayOf(viewModel.products.toString())
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.getWashingMachines()
setObservers()
setupGridView()
}
private fun setupGridView() {
println(viewModel.products)
}
private fun setObservers() {
val strings = arrayOf(viewModel.products)
viewModel.isCorrect.observe(viewLifecycleOwner) {
if (it.isNotEmpty()){
itemList.contentEquals(arrayOf(viewModel.products))
val adapter = ImageListAdapter(context!!, R.layout.list_item, itemList)
binding.gridview.adapter = adapter
binding.gridview.onItemClickListener =
AdapterView.OnItemClickListener { parent, v, position, id ->
println("you clicked")
}
println("Not Failed")
}
else
{
println("Failed")
}
}
}
class HomeFragment : Fragment() {
private lateinit var binding: FragmentHomeBinding
private val viewModel by KoinJavaComponent.inject(HomeFragmentVM::class.java)
private val sp by inject<SPManager>()
private var profile : Data? = null
private val itemList: Array<String>
get() = arrayOf(viewModel.products.toString())
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.getWashingMachines()
setObservers()
setupGridView()
}
private fun setupGridView() {
println(viewModel.products)
}
private fun setObservers() {
val strings = arrayOf(viewModel.products)
viewModel.isCorrect.observe(viewLifecycleOwner) {
if (it.isNotEmpty()){
itemList.contentEquals(arrayOf(viewModel.products))
val adapter = ImageListAdapter(context!!, R.layout.list_item, itemList)
binding.gridview.adapter = adapter
binding.gridview.onItemClickListener =
AdapterView.OnItemClickListener { parent, v, position, id ->
println("you clicked")
}
println("Not Failed")
}
else
{
println("Failed")
}
}
}
适配器
internal class ImageListAdapter internal constructor(
context: Context,
private val resource: Int,
private val itemList: Array<String>?
) : ArrayAdapter<ImageListAdapter.ItemViewHolder>(context, resource) {
private val inflater: LayoutInflater = LayoutInflater.from(context)
private lateinit var itemBinding: ListItemBinding
override fun getCount(): Int {
return if (this.itemList != null) this.itemList.size else 0
}
override fun getView(position: Int, view: View?, parent: ViewGroup): View {
var convertView = view
val holder: ItemViewHolder
if (convertView == null) {
itemBinding = ListItemBinding.inflate(inflater)
convertView = itemBinding.root
holder = ItemViewHolder()
holder.name = itemBinding.textView
holder.icon = itemBinding.icon
convertView.tag = holder
} else {
holder = convertView.tag as ItemViewHolder
}
holder.name!!.text = this.itemList!![position]
holder.icon!!.setImageResource(R.mipmap.ic_launcher)
return convertView
}
internal class ItemViewHolder {
var name: TextView? = null
var icon: ImageView? = null
}
}
Api调用的工作原理只是我如何将这一个一个分开,使每个网格布局视图非常感谢
2条答案
按热度按时间hgqdbh6s1#
看了你的代码后,我觉得问题出在
因为当您呼叫'toString()'时,会将产品清单转换成ONE字串,并将该字串插入清单中,然后将含有一个项目(字串)的清单传递给配接器
oxcyiej72#
您应该替换
与