android——spinner下拉弹窗、popupwindow下拉弹窗列表

一、spinner下拉弹窗

效果图如下:

android——spinner下拉弹窗、popupwindow下拉弹窗列表_第1张图片

adapter的代码:

package com.yaona.spinner

import android.R
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView

class SpinnerAdapter(
    context: Context,
    textViewResourceId: Int, objects: Array
) : ArrayAdapter(context, textViewResourceId, objects) {
    private var context: Context
    private var items = arrayOf()

    init {
        items = objects
        this.context = context
    }

    /** 点击后弹窗显示 **/
    override fun getDropDownView(
        position: Int, convertView: View?,
        parent: ViewGroup
    ): View? {
        val tv: TextView = if (convertView == null) {
            val inflater = LayoutInflater.from(context)
            val view = inflater.inflate(R.layout.simple_spinner_item, parent, false)
            view.findViewById(R.id.text1)
        } else {
            convertView.findViewById(R.id.text1)
        }
        tv.text = items[position]
        tv.setTextColor(Color.BLUE)
        tv.textSize = 24f
        return tv
    }

    /** spinner控件显示 **/
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        if (convertView == null) {
            val inflater = LayoutInflater.from(context)
            convertView = inflater.inflate(
                R.layout.simple_spinner_item, parent, false
            )
        }
        val tv = convertView?.findViewById(R.id.text1) as TextView
        tv.text = items[position]
//        tv.setTextColor(Color.BLUE)
        tv.textSize = 24f
        return convertView!!
    }
}

android——spinner下拉弹窗、popupwindow下拉弹窗列表_第2张图片

xml代码:




    

    

    

        

    

android——spinner下拉弹窗、popupwindow下拉弹窗列表_第3张图片

MainActivity代码

import android.R
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.yaona.spinner.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)

        initSupplierSpinner(mutableListOf("张三", "李四","王五"))
        setContentView(binding.root)
    }

    private fun initSupplierSpinner(list: List) {
        // 创建一个适配器用于显示 Spinner 中的项目列表
        val adapter = SpinnerAdapter(this, R.layout.simple_spinner_item, list.toTypedArray())
        binding.spSupplyRefund.dropDownVerticalOffset = 180
        binding.spSupplyRefund.adapter = adapter
    }
}

android——spinner下拉弹窗、popupwindow下拉弹窗列表_第4张图片

具体的代码下载地址为:https://download.csdn.net/download/wy313622821/88274359

二、popupwindow下拉弹窗列表(推荐)

效果图为:

 android——spinner下拉弹窗、popupwindow下拉弹窗列表_第5张图片 

代码下载地址为:

https://download.csdn.net/download/wy313622821/88275750

你可能感兴趣的:(kotlin,-,java,android)