如何在列表视图 (Kotlin) 中每行添加多个图像,例如 3 张图像
How to add multiple for example 3 images per line in a listview (Kotlin)
在 R.layout.maskot_list(listview 的自定义行)中,有三个 imageView,我试图显示如下内容:
mascot1.jpg . mascot2.jpg . mascot3.jpg
mascot4.jpg . mascot5.jpg . mascot6.jpg
etc
这意味着每行 3(多个)图像,我已经尝试了下面提到的代码。但它显示的图像如下:
mascot1.jpg
mascot2.jpg
etc
这意味着每行只有一张图片。那么,我该如何解决这个问题。
package jp.co.lumber_mill.toyotago.fragment
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import jp.co.lumber_mill.toyotago.R
class FragmentMascot : Fragment() {
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view= inflater!!.inflate(R.layout.fragment_mascot, container, false)
val listview = view.findViewById<ListView>(R.id.maskot_list)
listview.adapter = MaskotAdapter(context,activity)
return view
}
private class MaskotAdapter(val context: Context, val activity: FragmentActivity): BaseAdapter() {
// private val mContext: Context
private val maskot_images_list = arrayListOf<Int>(R.drawable.maskot1,R.drawable.maskot2,R.drawable.maskot3,R.drawable.maskot4,R.drawable.maskot5,R.drawable.maskot6)
override fun getCount(): Int {
return maskot_images_list.size //To change body of created functions use File | Settings | File Templates.
}
override fun getItem(position: Int): Any {
return "test String" //To change body of created functions use File | Settings | File Templates.
}
override fun getItemId(position: Int): Long {
return position.toLong()//To change body of created functions use File | Settings | File Templates.
}
//renders each row
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val layoutInflater = LayoutInflater.from(context)
val maskot_row=layoutInflater.inflate(R.layout.maskot_list, parent, false)
val maskot_image = maskot_row.findViewById<ImageView>(R.id.maskot_image)
maskot_image.setImageResource(maskot_images_list.get(position))
return maskot_row
}
}
}
好的,我终于找到解决方法了。为了添加 3 张图片,我们可以像下面这样改变位置添加图片 3 次。
val layoutInflater = LayoutInflater.from(context)
val badgeRow=layoutInflater.inflate(R.layout.badge_list, parent, false)
var pos = position
for (i in 0..(maskot_images_list.size / 3)-2) {
val maskot_image1 = badgeRow.findViewById<ImageView>(R.id.left_badge)
maskot_image1.setImageResource(maskot_images_list.get(pos))
pos++
val maskot_image2 = badgeRow.findViewById<ImageView>(R.id.center_badge)
maskot_image2.setImageResource(maskot_images_list.get(pos))
pos++
val maskot_image3 = badgeRow.findViewById<ImageView>(R.id.right_badge)
maskot_image3.setImageResource(maskot_images_list.get(pos))
pos++
}
并且可能会像下面这样更改 getCount 部分:
override fun getCount(): Int {
//for 3 images decrease the number of row count 3 times
return maskot_images_list.size/3 //To change body of created functions use File | Settings | File Templates.
}
尝试使用 recycler view 它是最新的...或者其他明智的你使用 GridLayout 。我提供recycler view code 很容易维护..
将以下依赖项添加到应用级别 gradle 文件..
compile 'com.android.support:recyclerview-v7:25.1.1'
使适配器像..
class CommentAdapter (var mList:List<Comment>) : RecyclerView.Adapter<CommentAdapter.ItemViewHolder> (){
override fun getItemCount(): Int {
return mList.size
}
override fun onBindViewHolder(holder: ItemViewHolder?, position: Int) {
var data=mList[position]
holder?.mEtMessage?.setText(data.message)
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ItemViewHolder {
var view=LayoutInflater.from(parent?.context).inflate(R.layout.comment_row_layout,parent,false)
return ItemViewHolder(view)
}
class ItemViewHolder : RecyclerView.ViewHolder{
var mEtMessage:EditText?=null
constructor(itemView: View?) : super(itemView){
mEtMessage=itemView?.findViewById(R.id.crlEtMessage)
}
}
}
并像下面这样定义回收器视图布局..
recyclerView?.layoutManager=GridLayoutManager(activity,3,LinearLayoutManager.VERTICAL,false)
在 R.layout.maskot_list(listview 的自定义行)中,有三个 imageView,我试图显示如下内容:
mascot1.jpg . mascot2.jpg . mascot3.jpg
mascot4.jpg . mascot5.jpg . mascot6.jpg
etc
这意味着每行 3(多个)图像,我已经尝试了下面提到的代码。但它显示的图像如下:
mascot1.jpg
mascot2.jpg
etc
这意味着每行只有一张图片。那么,我该如何解决这个问题。
package jp.co.lumber_mill.toyotago.fragment
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import jp.co.lumber_mill.toyotago.R
class FragmentMascot : Fragment() {
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view= inflater!!.inflate(R.layout.fragment_mascot, container, false)
val listview = view.findViewById<ListView>(R.id.maskot_list)
listview.adapter = MaskotAdapter(context,activity)
return view
}
private class MaskotAdapter(val context: Context, val activity: FragmentActivity): BaseAdapter() {
// private val mContext: Context
private val maskot_images_list = arrayListOf<Int>(R.drawable.maskot1,R.drawable.maskot2,R.drawable.maskot3,R.drawable.maskot4,R.drawable.maskot5,R.drawable.maskot6)
override fun getCount(): Int {
return maskot_images_list.size //To change body of created functions use File | Settings | File Templates.
}
override fun getItem(position: Int): Any {
return "test String" //To change body of created functions use File | Settings | File Templates.
}
override fun getItemId(position: Int): Long {
return position.toLong()//To change body of created functions use File | Settings | File Templates.
}
//renders each row
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val layoutInflater = LayoutInflater.from(context)
val maskot_row=layoutInflater.inflate(R.layout.maskot_list, parent, false)
val maskot_image = maskot_row.findViewById<ImageView>(R.id.maskot_image)
maskot_image.setImageResource(maskot_images_list.get(position))
return maskot_row
}
}
}
好的,我终于找到解决方法了。为了添加 3 张图片,我们可以像下面这样改变位置添加图片 3 次。
val layoutInflater = LayoutInflater.from(context)
val badgeRow=layoutInflater.inflate(R.layout.badge_list, parent, false)
var pos = position
for (i in 0..(maskot_images_list.size / 3)-2) {
val maskot_image1 = badgeRow.findViewById<ImageView>(R.id.left_badge)
maskot_image1.setImageResource(maskot_images_list.get(pos))
pos++
val maskot_image2 = badgeRow.findViewById<ImageView>(R.id.center_badge)
maskot_image2.setImageResource(maskot_images_list.get(pos))
pos++
val maskot_image3 = badgeRow.findViewById<ImageView>(R.id.right_badge)
maskot_image3.setImageResource(maskot_images_list.get(pos))
pos++
}
并且可能会像下面这样更改 getCount 部分:
override fun getCount(): Int {
//for 3 images decrease the number of row count 3 times
return maskot_images_list.size/3 //To change body of created functions use File | Settings | File Templates.
}
尝试使用 recycler view 它是最新的...或者其他明智的你使用 GridLayout 。我提供recycler view code 很容易维护..
将以下依赖项添加到应用级别 gradle 文件..
compile 'com.android.support:recyclerview-v7:25.1.1'
使适配器像..
class CommentAdapter (var mList:List<Comment>) : RecyclerView.Adapter<CommentAdapter.ItemViewHolder> (){
override fun getItemCount(): Int {
return mList.size
}
override fun onBindViewHolder(holder: ItemViewHolder?, position: Int) {
var data=mList[position]
holder?.mEtMessage?.setText(data.message)
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ItemViewHolder {
var view=LayoutInflater.from(parent?.context).inflate(R.layout.comment_row_layout,parent,false)
return ItemViewHolder(view)
}
class ItemViewHolder : RecyclerView.ViewHolder{
var mEtMessage:EditText?=null
constructor(itemView: View?) : super(itemView){
mEtMessage=itemView?.findViewById(R.id.crlEtMessage)
}
}
}
并像下面这样定义回收器视图布局..
recyclerView?.layoutManager=GridLayoutManager(activity,3,LinearLayoutManager.VERTICAL,false)