学习Android自定义Spinner适配器
作者:不正经的小二哥 时间:2022-12-15 01:28:31
本文为大家分享Android自定义Spinner适配器的相关知识点,供大家参考,具体内容如下
一、大致效果
二.关键代码
在注释中讲重点吧。
(1)Spinner的布局: car_brand_spinner.xml
即为弹出来的下拉列表的布局啦,后面的那个布局就不拿出来丢人现眼了,反正知道有一个Spinner的id为carBrandSpinner就可以了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/car_brand_name"
android:layout_gravity="left"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/car_brand_flag"
android:layout_gravity="right"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
(2)适配器
ArrayAdapter carBrandAdapter=new ArrayAdapter<String>
(
AddCarActivity.this,
android.R.layout.simple_spinner_dropdown_item,
carBrandNameList//是String[],就是所有要显示的brandName
){
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(AddCarActivity.this,R.layout.car_brand_spinner,null);//获得Spinner布局View
if(convertView!=null)
{
TextView carBrandNameView = (TextView)convertView.findViewById(R.id.car_brand_name);
ImageView carBrandFlagView = (ImageView)convertView.findViewById(R.id.car_brand_flag);
try
{
JSONObject json = new JSONObject(carBrandList.get(position).get("carBrand").toString());
carBrandNameView.setText(json.getString("carBrandName"));//设置数据,我这里的数据是从服务器读出来的,所以前面有一个转化取值的过程
}catch (Exception e){}
Bitmap bitmap =Common.String2Bitmap(carBrandList.get(position).get("carBrandFlagContent").toString());//这里也一样,图片数据来自于服务器,同时有一个将数据从String转Bitmap的过程
if(bitmap!=null)
carBrandFlagView.setImageBitmap(bitmap);//显示图片
}
return convertView;
}
};
//给Spinner set适配器
Spinner carBrandSpinner=(Spinner)findViewById(R.id.carBrandSpinner);
carBrandSpinner.setAdapter(carBrandAdapter);
carBrandSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override//重写Item被选择的事件
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
到此结束!
标签:Android,Spinner,适配器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android基于ViewPager实现的应用欢迎界面完整实例
2021-10-02 23:01:52
Spring Cloud Ribbon的踩坑记录与原理详析
2023-02-06 04:06:55
![](https://img.aspxhome.com/file/2023/7/126357_0s.png)
Android实现小米相机底部滑动指示器
2023-03-12 04:57:00
![](https://img.aspxhome.com/file/2023/1/120071_0s.jpg)
c# 钩子学习笔记
2023-03-31 20:54:22
Android Scroller实现弹性滑动效果
2022-12-08 11:01:02
![](https://img.aspxhome.com/file/2023/0/138740_0s.gif)
浅谈Android onTouchEvent 与 onInterceptTouchEvent的区别详解
2021-05-30 02:56:55
![](https://img.aspxhome.com/file/2023/1/114331_0s.jpg)
浅谈SpringBoot中的@Conditional注解的使用
2021-12-07 16:47:04
![](https://img.aspxhome.com/file/2023/8/61798_0s.png)
Android的消息机制
2023-08-05 10:19:28
Springboot+ElementUi实现评论、回复、点赞功能
2022-06-16 04:34:00
![](https://img.aspxhome.com/file/2023/3/72913_0s.jpg)
ImportBeanDefinitionRegistrar手动控制BeanDefinition创建注册详解
2021-11-11 18:49:18
Spring实现动态切换多数据源的解决方案
2023-05-21 13:07:15
Java Hutool 包工具类推荐 ExcelUtil详解
2023-01-22 23:47:12
Android 图片处理缩放功能
2023-09-29 13:59:54
![](https://img.aspxhome.com/file/2023/9/126699_0s.gif)
Android实现完整游戏循环的方法
2023-12-26 16:40:06
Java中的 stop the world是什么呢
2022-09-11 21:32:42
![](https://img.aspxhome.com/file/2023/5/78925_0s.png)
SpringMVC源码解读之HandlerMapping - AbstractUrlHandlerMapping系列request分发
2022-07-26 20:39:48
C#实现Excel导入sqlite的方法
2021-12-02 23:38:17
Java使用Jedis操作Redis服务器的实例代码
2023-09-08 07:38:42
C#实现的图片、string相互转换类分享
2022-04-17 04:50:34
解析spring boot与ireport 整合问题
2021-12-15 17:49:25