Android 如何在listview(Kotlin)中每行添加多个图像,例如3个图像

Android 如何在listview(Kotlin)中每行添加多个图像,例如3个图像,android,listview,kotlin,imageview,Android,Listview,Kotlin,Imageview,在R.layout.maskot_列表(listview的自定义行)中,有三个imageView,我尝试显示如下内容: mascot1.jpg . mascot2.jpg . mascot3.jpg mascot4.jpg . mascot5.jpg . mascot6.jpg etc 这意味着每行3个(多个)图像,我已经尝试了下面提到的代码。但它显示的图像如下: mascot1.jpg mascot2.jpg etc 这意味着每行只有一个图像。那么,我该如何解决这个问题呢 package

在R.layout.maskot_列表(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
        }
      }
    }
包装jp.co.lumber_mill.toyotago.fragment
导入android.content.Context
导入android.os.Bundle
导入android.support.v4.app.Fragment
导入android.support.v4.app.FragmentActivity
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入android.widget.BaseAdapter
导入android.widget.ImageView
导入android.widget.ListView
导入android.widget.TextView
进口日本公司木材厂
类FragmentMascot:Fragment(){
覆盖创建视图(充气机:布局充气机?、容器:视图组?、savedInstanceState:捆绑?):视图{
val view=充气机!!.充气(R.layout.fragment\u吉祥物,容器,假)
val listview=view.findViewById(R.id.maskot_列表)
listview.adapter=MaskotAdapter(上下文、活动)
返回视图
}
私有类MaskotAdapter(val-context:context,val-activity:FragmentActivity):BaseAdapter(){
//private val mContext:上下文
private val maskot_images_list=arrayListOf(R.drawable.maskot1,R.drawable.maskot2,R.drawable.maskot3,R.drawable.maskot4,R.drawable.maskot5,R.drawable.maskot6)
重写fun getCount():Int{
返回maskot_images_list.size//若要更改已创建函数的主体,请使用文件|设置|文件模板。
}
覆盖趣味getItem(位置:Int):任意{
返回“测试字符串”//以更改创建的函数体使用文件|设置|文件模板。
}
覆盖getItemId(位置:Int):长{
return position.toLong()//要更改创建的函数体,请使用文件|设置|文件模板。
}
//渲染每一行
覆盖视图(位置:Int,转换视图:View?,父视图:ViewGroup?):视图{
val layoutInflater=layoutInflater.from(上下文)
val maskot_行=LayoutFlater.充气(R.layout.maskot_列表,父项,false)
val maskot_image=maskot_row.findViewById(R.id.maskot_image)
maskot_image.setImageResource(maskot_images_list.get(位置))
返回马斯克图街
}
}
}

好的,我终于找到了解决方法。为了添加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++
                }

好的,我终于找到了解决方法。为了添加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++
                }

尝试使用回收查看它是最新的。。。或者你使用了GridLayout。我提供了便于维护的回收器视图代码

将以下依赖项添加到应用程序级渐变文件中

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)
    }
}

尝试使用回收查看它是最新的。。。或者你使用了GridLayout。我提供了便于维护的回收器视图代码

将以下依赖项添加到应用程序级渐变文件中

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)
    }
}

您可以根据需要对一行中的多个项目使用GridView或Recyclerview确定,您可以使用recycle view。这是最新的和更容易的,可以按照这个链接来学习它。好的,谢谢你的链接。我只是看了一下,它看起来很有用。继续编码。您可以根据需要对一行中的多个项目使用GridView或Recyclerview确定,您可以使用recycle view。这是最新的和更容易的,可以按照这个链接来学习它。好的,谢谢你的链接。我只是看了一下,它看起来很有用。继续编码。