安卓使用GridView实现网格视图

效果图

安卓使用GridView实现网格视图_第1张图片

主要代码

class FlowerAdapter(private val context: Context, flowerData: ArrayList<Flower>) : BaseAdapter() {
    private var mFlowerData = ArrayList<Flower>()
    private val mInflaterCatalogListItems: LayoutInflater

    init {
        mFlowerData = flowerData
        mInflaterCatalogListItems =
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    }

    override fun getCount(): Int {
        return mFlowerData.size
    }

    override fun getItem(position: Int): Any {
        return mFlowerData[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, _convertView: View?, parent: ViewGroup): View {
        var convertView: View
        val holder: ViewHolder
        if (_convertView == null) {
            holder = ViewHolder()
            convertView = mInflaterCatalogListItems.inflate(
                R.layout.adapter_flower,
                null
            )
            holder.nameTv =
                convertView.findViewById<View>(R.id.textView) as TextView
            holder.imageView =
                convertView.findViewById<View>(R.id.photoView) as ImageView
            convertView.tag = holder
        } else {
            convertView = _convertView
            holder = convertView.tag as ViewHolder
        }

        if (mFlowerData[position] != null) {
            holder.nameTv?.text = mFlowerData[position].flowerName
            holder.imageView?.setImageResource(mFlowerData[position].imagePath)
        }
        return convertView
    }

    private class ViewHolder {
        var nameTv: TextView? = null
        var imageView: ImageView? = null
    }
}
class Flower {
    var flowerName: String? = null
    var imagePath = 0
}
class FlowerActivity : AppCompatActivity() {
    var dataList = ArrayList<Flower>()
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flower)

        dataList = prepareDataList()

        val gridView = findViewById<View>(R.id.gridView) as GridView
        val flowerAdapter = FlowerAdapter(this, dataList)
        gridView.adapter = flowerAdapter
        gridView.setOnItemClickListener { _, _, position, _ -> toast(dataList[position].flowerName!!) }
    }

    private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()

    private fun prepareDataList(): ArrayList<Flower> {
        val flowerData = ArrayList<Flower>()

        //1st Item
        var flower = Flower()
        flower.flowerName = "Alyssum"
        flower.imagePath = R.drawable.image1
        flowerData.add(flower)

        //2nd Item
        flower = Flower()
        flower.flowerName = "Daisy"
        flower.imagePath = R.drawable.image2
        flowerData.add(flower)

        //3rd Item
        flower = Flower()
        flower.flowerName = "Jasmine"
        flower.imagePath = R.drawable.image3
        flowerData.add(flower)

        //4th Item
        flower = Flower()
        flower.flowerName = "Lily"
        flower.imagePath = R.drawable.image1
        flowerData.add(flower)

        //5th Item
        flower = Flower()
        flower.flowerName = "Poppy"
        flower.imagePath = R.drawable.image2
        flowerData.add(flower)

        //6th Item
        flower = Flower()
        flower.flowerName = "Rose"
        flower.imagePath = R.drawable.image3
        flowerData.add(flower)
        return flowerData
    }
}

完整源代码

https://gitee.com/cxyzy1/GridViewDemo

你可能感兴趣的:(安卓)