前端开发中的加载指示器(Loading Spinners)一种动态旋转的图形元素(如圆圈、齿轮状动画)
在 Android 中Spinner是一个下拉选择控件用于从预定义列表中选择一项。以下是标准、稳定、兼容性好的实现方式基于ViewBindingArrayAdapter适配 AndroidX 和 API 21✅ 一、绑定数据以字符串数组为例1. 布局文件activity_main.xmlSpinnerandroid:idid/spinnerandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:promptstring/spinner_prompt/2. Activity/Fragment 中绑定Kotlin 示例// 1. 准备数据源可为 ListString、String[] 或自定义对象列表valitemslistOf(北京,上海,广州,深圳)// 2. 创建 ArrayAdapter推荐使用带布局资源的构造函数确保可访问性valadapterArrayAdapter(this,android.R.layout.simple_spinner_item,// 下拉项显示样式单行文本items).apply{// 设置下拉菜单展开时的样式必须否则点击无反应或样式错乱setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)}// 3. 绑定到 Spinnerbinding.spinner.adapteradapter 提示simple_spinner_item用于默认显示项simple_spinner_dropdown_item用于下拉弹出项——二者必须分别设置否则可能无文字、文字重叠或点击无效。✅ 二、监听选中事件binding.spinner.onItemSelectedListenerobject:AdapterView.OnItemSelectedListener{overridefunonItemSelected(parent:AdapterView*,view:View?,position:Int,id:Long){valselecteditems[position]// 安全获取选中项position 有效Toast.makeText(this,选中$selected,Toast.LENGTH_SHORT).show()}overridefunonNothingSelected(parent:AdapterView*){// 用户清空选择仅当 Spinner 支持空选且设置了 prompt 时可能触发// 注意原生 Spinner 默认不支持“无选中”此回调极少触发}}⚠️ 关键点onItemSelected会在初始化设 adapter 后自动触发一次position0若需避免首次误触发可用标志位或post { }延迟监听注册。❗ 三、常见坑点与解决方案坑点原因解决方案下拉菜单点击无响应 / 文字不显示未调用setDropDownViewResource()✅ 必须显式设置adapter.setDropDownViewResource(...)首次加载自动触发onItemSelectedAdapter 设置后立即回调 position0✅ 使用布尔标记isInitialized false首次忽略或binding.spinner.post { ... }延迟注册监听器中文乱码 / 字体过小 / 无高亮使用了自定义 layout 但未正确设置TextView属性✅ 自定义 item layout 中TextView需设android:layout_widthmatch_parent、android:textAppearance?android:attr/textAppearanceMediumSpinner 在 RecyclerView 中滚动后状态丢失Spinner不是ViewHolder友好控件复用导致状态错乱✅ 避免在RecyclerView中直接嵌套Spinner改用PopupWindowButton模拟或保存/恢复selection状态Material Design 风格缺失如无下划线、无动画使用了原生Spinner而非MaterialSpinner✅ 替换为com.google.android.material.spinner.MaterialSpinner需引入material库 v1.10.0支持app:hint、app:boxBackgroundMode等✅ 补充绑定自定义对象如User(id: Int, name: String)dataclassUser(valid:Int,valname:String)valuserslistOf(User(1,张三),User(2,李四))valadapterArrayAdapterUser(this,android.R.layout.simple_spinner_item,users).apply{setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)// 自定义显示文本覆盖 toString()setViewBinder{view,user,_-if(viewisTextView){view.textuser.nametrue}elsefalse}}binding.spinner.adapteradapter// 监听时获取对象binding.spinner.onItemSelectedListenerobject:OnItemSelectedListener{overridefunonItemSelected(p0:AdapterView*?,p1:View?,pos:Int,p2:Long){valselectedUserusers[pos]// 或 adapter.getItem(pos)}// ...}需要我为你生成一个**完整可运行的 GitHub Gist 示例含 Java 版本、MaterialSpinner 用法、状态保存方案**吗欢迎继续提问“Spinners” 通常指以下几种常见含义具体需结合上下文判断前端开发中的加载指示器Loading Spinners一种动态旋转的图形元素如圆圈、齿轮状动画用于向用户反馈操作正在进行中例如页面加载、API 请求处理。常通过 CSS 动画keyframestransform: rotate()或 SVG/JavaScript 实现。Android 开发中的 Spinner 组件一个下拉选择控件类似 HTML 的select允许用户从预定义列表中选择一项。继承自AdapterView需配合ArrayAdapter或自定义Adapter使用。Python 的spinners库第三方包一个轻量级命令行工具库提供终端中可配置的旋转动画如dots,bouncingBar,weather等样式常用于 CLI 脚本中增强用户体验。安装方式pip install spinners。物理/教育场景中的陀螺类教具Spinning Tops / Spinners如 fidget spinner指尖陀螺曾流行于减压玩具领域也用于物理教学演示角动量、惯性等概念。如需进一步说明某一种用法如代码示例、CSS 实现、Android 配置或 Python 使用欢迎补充具体场景。SpinnersIndicate the loading state of a component or page with Bootstrap spinners, built entirely with HTML, CSS, and no JavaScript.ads via CarbonLimited time offer: Get 10 free Adobe Stock images.ads via CarbonAboutBootstrap “spinners” can be used to show the loading state in your projects. They’re built only with HTML and CSS, meaning you don’t need any JavaScript to create them. You will, however, need some custom JavaScript to toggle their visibility. Their appearance, alignment, and sizing can be easily customized with our amazing utility classes.For accessibility purposes, each loader here includes role“status” and a nestedLoading….Border spinnerUse the border spinners for a lightweight loading indicator.Loading…Loading...ColorsThe border spinner uses currentColor for its border-color, meaning you can customize the color with text color utilities. You can use any of our text color utilities on the standard spinner.Loading…Loading…Loading…Loading…Loading…Loading…Loading…Loading…Loading...Loading...Loading...Loading...Loading...Loading...Loading...Loading...Why not use border-color utilities? Each border spinner specifies a transparent border for at least one side, so .border-{color} utilities would override that.Growing spinnerIf you don’t fancy a border spinner, switch to the grow spinner. While it doesn’t technically spin, it does repeatedly grow!Loading…Loading...Once again, this spinner is built with currentColor, so you can easily change its appearance with text color utilities. Here it is in blue, along with the supported variants.Loading…Loading…Loading…Loading…Loading…Loading…Loading…Loading…Loading...Loading...Loading...Loading...Loading...Loading...Loading...Loading...AlignmentSpinners in Bootstrap are built with rems, currentColor, and display: inline-flex. This means they can easily be resized, recolored, and quickly aligned.MarginUse margin utilities like .m-5 for easy spacing.Loading…Loading...PlacementUse flexbox utilities, float utilities, or text alignment utilities to place spinners exactly where you need them in any situation.FlexLoading…Loading...Loading…Loading...FloatsLoading…Loading...Text alignLoading…Loading...SizeAdd .spinner-border-sm and .spinner-grow-sm to make a smaller spinner that can quickly be used within other components.Loading…Loading…Loading...Loading...Or, use custom CSS or inline styles to change the dimensions as needed.Loading…Loading…Loading...Loading...ButtonsUse spinners within buttons to indicate an action is currently processing or taking place. You may also swap the text out of the spinner element and utilize button text as needed.Loading...Loading...Loading...Loading... 
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2482615.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!