foreword
Recently, a form-like project has been developed, in which a large number of spinners are required, and default values need to be set (the spinner that comes with Android displays the first piece of data by default, which does not meet the requirements). We all know that using a spinner requires setting an adapter and some common parameters. If it is used in large quantities, it will be very troublesome and there will be many identical codes. The background introduction is over.
Next, let me talk about what problems my custom spinner solves.
1. The adapter is encapsulated, and you only need to pass in the String array when using it
2. Added the function of setting the default value inside the spinner
Above code:
Take a look at using:
1. Reference in xml
<com.gaoql.view.SimpleSpinner
android:id="@+id/spinner"
android:layout_width="150dp"
android:layout_height="@dimen/dp_40"
/>
2. Use in the code Note : the default value will only take effect after setting the listening event
ArrayList<String> lists = new ArrayList<>();
lists.add("A型");
lists.add("B型");
lists.add("O型");
//设置数据集
spinner.setContent(lists);
//设置默认值
spinner.setDefaultContent("请选择血型");
//设置监听事件
spinner.setOnItemSelectedListener(new SimpleSpinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id, String content) {
Toast.makeText(getContext(),"您选择的是:"+content+" 第"+position+"个 id:"+id,Toast.LENGTH_SHORT).show();
}
});
Isn't it cool.
Here is the code SimpleSpinner.java:
package com.hylink.wholemachine.ui.view;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.hylink.wholemachine.R;
import java.lang.reflect.Field;
import java.util.ArrayList;
/**
* @author: gaoql
* @date: 2022/3/1 11:04
* @function:封装下拉选框
*/
public class SimpleSpinner extends androidx.appcompat.widget.AppCompatSpinner {
private ArrayAdapter adapter;
private ArrayList<String> contents;
private String defaultContent;
private Context context;
public SimpleSpinner(Context context) {
super(context);
onCreate(context);
}
public SimpleSpinner(Context context, int mode) {
super(context, mode);
onCreate(context);
}
public SimpleSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
onCreate(context);
}
public SimpleSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
onCreate(context);
}
/**
* @author gaoql
* @time 2022/3/1 11:05
* @description 初始化
*/
private void onCreate(Context context){
this.context = context;
}
/**
* @author gaoql
* @time 2022/3/1 11:44
* @description 设置spinner数据
*/
public void setContent(ArrayList<String> contents){
this.contents = contents;
if(adapter!=null){
adapter = null;
}
adapter = new ArrayAdapter(getContext(), R.layout.sp_item_select,contents.toArray(new String[]{}));
adapter.setDropDownViewResource(R.layout.sp_item_dropdown);
this.setAdapter(adapter);
}
/**
* @author gaoql
* @time 2022/3/3 9:12
* @description 设置默认显示内容
*/
public void setDefaultContent(String defaultContent){
this.defaultContent = defaultContent;
}
/**
* @author gaoql
* @time 2022/3/1 13:26
* @description 选择事件
*/
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener){
setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//首次刚加载时 判断是否设置默认显示内容,如果有则显示默认内容,逻辑是把第一条的view内容修改掉
if(!TextUtils.isEmpty(defaultContent)){
if(view instanceof TextView){
TextView textView = (TextView) view;
textView.setText(defaultContent);
textView.setTextColor(Color.parseColor("#999999"));
defaultContent = "";
}
}else{
//把刚刚修改的内容和颜色都修改回来
if(position == 0){
if(view instanceof TextView){
TextView textView = (TextView) view;
textView.setText(contents.get(0));
textView.setTextColor(Color.parseColor("#ffffff"));
}
}
onItemSelectedListener.onItemSelected(parent,view,position,id,contents.get(position));
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public interface OnItemSelectedListener{
void onItemSelected(AdapterView<?> adapterView, View view, int position, long id,String content);
}
/**
* @author gaoql
* @time 2022/3/3 10:22
* @description 解决 选择相同选项时 点击不触发问题,逻辑是 如果点击的是相同选项则 强制调用一次条码选择事件
*/
@Override
public void setSelection(int position) {
super.setSelection(position);
boolean sameSelected = position == getSelectedItemPosition();
if (sameSelected) {
getOnItemSelectedListener().onItemSelected(this, getSelectedView(),
position, getSelectedItemId());
}
}
}
sp_item_dropdown.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="#0000ff"
android:textSize="20sp"
android:paddingLeft="20dp"
android:background="#559999"
android:gravity="center_vertical"/>
sp_item_select.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#999999"
android:textSize="22sp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="@android:color/white"/>
Code words are not easy, I hope brothers can give some encouragement, likes and so on.