android 微调器下拉复选框
android spinner dropdown checkbox
我有 Spinner
这样的 :
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:background="@drawable/spinner_bg"
android:popupBackground="@drawable/spinner_bg"/>
这是spinner_bg.xml:
<item>
<layer-list>
<item>
<shape>
<gradient
android:startColor="#ffffff"
android:centerColor="#111111"
android:endColor="#000000"
android:angle="-90" />
<stroke
android:width="2dp"
android:color="#ffffff" />
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:right="10dp"/>
</shape>
</item>
<item >
<bitmap
android:gravity="right"
android:src="@android:drawable/arrow_down_float" />
</item>
</layer-list>
</item>
这是我的自定义微调器代码:
ArrayAdapter<ClassId> adapter = new ArrayAdapter<ClassId>(getActivity(),
R.layout.list_id, idList);
adapter.setDropDownViewResource(R.layout.list_id_select);
这是list_id.xml的布局:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#ff0004"
android:textSize="14sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
这是list_id_select.xml的布局:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#0004ff"
android:textSize="14sp"
android:checked="true"
android:checkMark="@drawable/custom_checkbox"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
这是custom_checkbox.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@android:drawable/checkbox_on_background" />
<item android:state_pressed="true"
android:drawable="@android:drawable/checkbox_on_background" />
<item android:drawable="@android:drawable/checkbox_off_background" />
这是我在微调器下拉列表中显示的结果:
_________________________________________________
__________________________checkbox_______________
____text_________________________________________
这意味着文本和复选框不在一条线上(复选框高于文本)。
如何解决?
为此,您必须创建 Custom Adapter
并在下面的方法中设置 TextView
和 CheckBox
。
在xml中定义Spinner
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
在 layout
文件夹中创建 spinner_item.xml
文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="text"
android:textAlignment="gravity" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
现在创建可以包含 TextView
和 CheckBox
值的 StateVO.java
class。
public class StateVO {
private String title;
private boolean selected;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
现在在您的 Activity
中初始化 Spinner
并在下方设置 CustomAdapter
。
final String[] select_qualification = {
"Select Qualification", "10th / Below", "12th", "Diploma", "UG",
"PG", "Phd"};
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList<StateVO> listVOs = new ArrayList<>();
for (int i = 0; i < select_qualification.length; i++) {
StateVO stateVO = new StateVO();
stateVO.setTitle(select_qualification[i]);
stateVO.setSelected(false);
listVOs.add(stateVO);
}
MyAdapter myAdapter = new MyAdapter(Main2Activity.this, 0,
listVOs);
spinner.setAdapter(myAdapter);
最后按下面的方式创建 CustomAdapter
class。
MyAdapter.java
public class MyAdapter extends ArrayAdapter<StateVO> {
private Context mContext;
private ArrayList<StateVO> listState;
private MyAdapter myAdapter;
private boolean isFromView = false;
public MyAdapter(Context context, int resource, List<StateVO> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = (ArrayList<StateVO>) objects;
this.myAdapter = this;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item, null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView
.findViewById(R.id.text);
holder.mCheckBox = (CheckBox) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
// To check weather checked event fire from getview() or user input
isFromView = true;
holder.mCheckBox.setChecked(listState.get(position).isSelected());
isFromView = false;
if ((position == 0)) {
holder.mCheckBox.setVisibility(View.INVISIBLE);
} else {
holder.mCheckBox.setVisibility(View.VISIBLE);
}
holder.mCheckBox.setTag(position);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if (!isFromView) {
listState.get(position).setSelected(isChecked);
}
}
});
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private CheckBox mCheckBox;
}
}
输出:
这个 post 是一个很好的资源。对于任何感兴趣的人,我根据另一个答案制作了一个通用适配器,该适配器适用于多种数据类型。还可以让您单击文本以切换复选框。
public class CheckableSpinnerAdapter<T> extends BaseAdapter {
static class SpinnerItem<T> {
private String txt;
private T item;
SpinnerItem(T t, String s) {
item = t;
txt = s;
}
}
private Context context;
private Set<T> selected_items;
private List<SpinnerItem<T>> all_items;
private String headerText;
CheckableSpinnerAdapter(Context context,
String headerText,
List<SpinnerItem<T>> all_items,
Set<T> selected_items) {
this.context = context;
this.headerText = headerText;
this.all_items = all_items;
this.selected_items = selected_items;
}
@Override
public int getCount() {
return all_items.size() + 1;
}
@Override
public Object getItem(int position) {
if( position < 1 ) {
return null;
}
else {
return all_items.get(position-1);
}
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
@SuppressWarnings({"unchecked"})
@NonNull
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
final ViewHolder holder;
if (convertView == null ) {
LayoutInflater layoutInflator = LayoutInflater.from(context);
convertView = layoutInflator.inflate(R.layout.checkable_spinner_item, parent, false);
holder = new ViewHolder();
holder.mTextView = convertView.findViewById(R.id.text);
holder.mCheckBox = convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
if( position < 1 ) {
holder.mCheckBox.setVisibility(View.GONE);
holder.mTextView.setText(headerText);
}
else {
final int listPos = position - 1;
holder.mCheckBox.setVisibility(View.VISIBLE);
holder.mTextView.setText(all_items.get(listPos).txt);
final T item = all_items.get(listPos).item;
boolean isSel = selected_items.contains(item);
holder.mCheckBox.setOnCheckedChangeListener(null);
holder.mCheckBox.setChecked(isSel);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if( isChecked ) {
selected_items.add(item);
}
else {
selected_items.remove(item);
}
}
});
holder.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.mCheckBox.toggle();
}
});
}
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private CheckBox mCheckBox;
}
}
和布局checkable_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/text"
app:layout_constraintBottom_toBottomOf="@id/text"/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textSize="16sp"
android:textColor="#000000"
app:layout_constraintStart_toEndOf="@id/checkbox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textAlignment="gravity" />
</android.support.constraint.ConstraintLayout>
您可以通过创建要显示的项目列表、包含所选项目的集合和 header 行文本来使用它。例如:
private final List<CheckableSpinnerAdapter.SpinnerItem<MyObject>> spinner_items = new ArrayList<>();
private final Set<MyObject> selected_items = new HashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// fill the 'spinner_items' array with all items to show
List<MyObject> all_objects = getMyObjects(); // from wherever
for(MyObject o : all_objects) {
spinner_items.add(new CheckableSpinnerAdapter.SpinnerItem<>(o, o.getName()));
}
// to start with any pre-selected, add them to the `selected_items` set
String headerText = "Click an Item";
Spinner spinner = findViewById(R.id.my_spinner);
CheckableSpinnerAdapter adapter = new CheckableSpinnerAdapter<>(this, headerText, spinner_items, selected_items);
spinner.setAdapter(adapter);
// when you want to see what the user has selected, look in the `selected_items`
// set anywhere in your activity
}
使用示例:
编辑:
MyObject
可以是任何 class 或枚举,无论您想要与微调项相关联。如果您直接复制此示例,则必须实现 String getName()
方法。这是一个简单的例子:
class MyObject {
private String mName;
private String mAddr;
MyObject(String name, String address) {
mName = name;
mAddr = address;
}
String getName() { return mName; }
String getAddress() { return mAddr; }
}
我有 Spinner
这样的 :
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:background="@drawable/spinner_bg"
android:popupBackground="@drawable/spinner_bg"/>
这是spinner_bg.xml:
<item>
<layer-list>
<item>
<shape>
<gradient
android:startColor="#ffffff"
android:centerColor="#111111"
android:endColor="#000000"
android:angle="-90" />
<stroke
android:width="2dp"
android:color="#ffffff" />
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:right="10dp"/>
</shape>
</item>
<item >
<bitmap
android:gravity="right"
android:src="@android:drawable/arrow_down_float" />
</item>
</layer-list>
</item>
这是我的自定义微调器代码:
ArrayAdapter<ClassId> adapter = new ArrayAdapter<ClassId>(getActivity(),
R.layout.list_id, idList);
adapter.setDropDownViewResource(R.layout.list_id_select);
这是list_id.xml的布局:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#ff0004"
android:textSize="14sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
这是list_id_select.xml的布局:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#0004ff"
android:textSize="14sp"
android:checked="true"
android:checkMark="@drawable/custom_checkbox"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
这是custom_checkbox.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@android:drawable/checkbox_on_background" />
<item android:state_pressed="true"
android:drawable="@android:drawable/checkbox_on_background" />
<item android:drawable="@android:drawable/checkbox_off_background" />
这是我在微调器下拉列表中显示的结果:
_________________________________________________
__________________________checkbox_______________
____text_________________________________________
这意味着文本和复选框不在一条线上(复选框高于文本)。
如何解决?
为此,您必须创建 Custom Adapter
并在下面的方法中设置 TextView
和 CheckBox
。
在xml中定义Spinner
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
在 layout
文件夹中创建 spinner_item.xml
文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="text"
android:textAlignment="gravity" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
现在创建可以包含 TextView
和 CheckBox
值的 StateVO.java
class。
public class StateVO {
private String title;
private boolean selected;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
现在在您的 Activity
中初始化 Spinner
并在下方设置 CustomAdapter
。
final String[] select_qualification = {
"Select Qualification", "10th / Below", "12th", "Diploma", "UG",
"PG", "Phd"};
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList<StateVO> listVOs = new ArrayList<>();
for (int i = 0; i < select_qualification.length; i++) {
StateVO stateVO = new StateVO();
stateVO.setTitle(select_qualification[i]);
stateVO.setSelected(false);
listVOs.add(stateVO);
}
MyAdapter myAdapter = new MyAdapter(Main2Activity.this, 0,
listVOs);
spinner.setAdapter(myAdapter);
最后按下面的方式创建 CustomAdapter
class。
MyAdapter.java
public class MyAdapter extends ArrayAdapter<StateVO> {
private Context mContext;
private ArrayList<StateVO> listState;
private MyAdapter myAdapter;
private boolean isFromView = false;
public MyAdapter(Context context, int resource, List<StateVO> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = (ArrayList<StateVO>) objects;
this.myAdapter = this;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item, null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView
.findViewById(R.id.text);
holder.mCheckBox = (CheckBox) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
// To check weather checked event fire from getview() or user input
isFromView = true;
holder.mCheckBox.setChecked(listState.get(position).isSelected());
isFromView = false;
if ((position == 0)) {
holder.mCheckBox.setVisibility(View.INVISIBLE);
} else {
holder.mCheckBox.setVisibility(View.VISIBLE);
}
holder.mCheckBox.setTag(position);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if (!isFromView) {
listState.get(position).setSelected(isChecked);
}
}
});
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private CheckBox mCheckBox;
}
}
输出:
这个 post 是一个很好的资源。对于任何感兴趣的人,我根据另一个答案制作了一个通用适配器,该适配器适用于多种数据类型。还可以让您单击文本以切换复选框。
public class CheckableSpinnerAdapter<T> extends BaseAdapter {
static class SpinnerItem<T> {
private String txt;
private T item;
SpinnerItem(T t, String s) {
item = t;
txt = s;
}
}
private Context context;
private Set<T> selected_items;
private List<SpinnerItem<T>> all_items;
private String headerText;
CheckableSpinnerAdapter(Context context,
String headerText,
List<SpinnerItem<T>> all_items,
Set<T> selected_items) {
this.context = context;
this.headerText = headerText;
this.all_items = all_items;
this.selected_items = selected_items;
}
@Override
public int getCount() {
return all_items.size() + 1;
}
@Override
public Object getItem(int position) {
if( position < 1 ) {
return null;
}
else {
return all_items.get(position-1);
}
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
@SuppressWarnings({"unchecked"})
@NonNull
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
final ViewHolder holder;
if (convertView == null ) {
LayoutInflater layoutInflator = LayoutInflater.from(context);
convertView = layoutInflator.inflate(R.layout.checkable_spinner_item, parent, false);
holder = new ViewHolder();
holder.mTextView = convertView.findViewById(R.id.text);
holder.mCheckBox = convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
if( position < 1 ) {
holder.mCheckBox.setVisibility(View.GONE);
holder.mTextView.setText(headerText);
}
else {
final int listPos = position - 1;
holder.mCheckBox.setVisibility(View.VISIBLE);
holder.mTextView.setText(all_items.get(listPos).txt);
final T item = all_items.get(listPos).item;
boolean isSel = selected_items.contains(item);
holder.mCheckBox.setOnCheckedChangeListener(null);
holder.mCheckBox.setChecked(isSel);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if( isChecked ) {
selected_items.add(item);
}
else {
selected_items.remove(item);
}
}
});
holder.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.mCheckBox.toggle();
}
});
}
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private CheckBox mCheckBox;
}
}
和布局checkable_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/text"
app:layout_constraintBottom_toBottomOf="@id/text"/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textSize="16sp"
android:textColor="#000000"
app:layout_constraintStart_toEndOf="@id/checkbox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textAlignment="gravity" />
</android.support.constraint.ConstraintLayout>
您可以通过创建要显示的项目列表、包含所选项目的集合和 header 行文本来使用它。例如:
private final List<CheckableSpinnerAdapter.SpinnerItem<MyObject>> spinner_items = new ArrayList<>();
private final Set<MyObject> selected_items = new HashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// fill the 'spinner_items' array with all items to show
List<MyObject> all_objects = getMyObjects(); // from wherever
for(MyObject o : all_objects) {
spinner_items.add(new CheckableSpinnerAdapter.SpinnerItem<>(o, o.getName()));
}
// to start with any pre-selected, add them to the `selected_items` set
String headerText = "Click an Item";
Spinner spinner = findViewById(R.id.my_spinner);
CheckableSpinnerAdapter adapter = new CheckableSpinnerAdapter<>(this, headerText, spinner_items, selected_items);
spinner.setAdapter(adapter);
// when you want to see what the user has selected, look in the `selected_items`
// set anywhere in your activity
}
使用示例:
编辑:
MyObject
可以是任何 class 或枚举,无论您想要与微调项相关联。如果您直接复制此示例,则必须实现 String getName()
方法。这是一个简单的例子:
class MyObject {
private String mName;
private String mAddr;
MyObject(String name, String address) {
mName = name;
mAddr = address;
}
String getName() { return mName; }
String getAddress() { return mAddr; }
}