我有一个微调器,可以同时选择多个项目。我跟随this和this并成功创建了一个。
以下是指向MultiSpinner
的链接我不明白的是如何自定义(例如:更改颜色)列表布局中显示的复选框。我知道这可以通过使用自定义适配器来完成,但上面共享的代码使用默认的" android.R.layout.simple_spinner_item"为了扩大列表的布局,我似乎无法弄清楚复选框的位置以及如何自定义它们?
谢谢!
答案 0 :(得分:0)
看起来带有复选框的选择器是通过此代码与Spinner
的正常工作分开创建的:
@Override
public boolean performClick() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMultiChoiceItems(
items.toArray(new CharSequence[items.size()]), selected, this);
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setOnCancelListener(this);
builder.show();
return true;
}
因此,要进行自定义,您必须自定义AlertDialog
多选择布局。
答案 1 :(得分:0)
您只需要替换
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item,
new String[] { spinnerText });
到
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
R.layout.your_row_item,
new String[] { spinnerText });
我还使用了multispinner类,并根据需要进行了自定义。这是班级 MultiSpinner.java
public class MultiSpinner extends Spinner implements DialogInterface.OnMultiChoiceClickListener, DialogInterface.OnCancelListener {
private List<String> items;
private List<GradeBean> mGradeBeanList = new ArrayList<GradeBean>();
private boolean[] selected;
private MultiSpinnerListener listener;
private String mGradeIds="";
private String mGrades="";
private MultiChoiceAdapter multiChoiceAdapter;
private AlertDialog alertDialog;
public MultiSpinner(Context context) {
super(context);
}
public MultiSpinner(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public MultiSpinner(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked)
selected[which] = true;
else
selected[which] = false;
}
@Override
public boolean performClick() {
multiChoiceAdapter = new MultiChoiceAdapter(mGradeBeanList,selected);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setAdapter(multiChoiceAdapter,null);
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog = builder.create();
ListView listView = alertDialog.getListView();
listView.setAdapter(multiChoiceAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selected[position] = !selected[position];
multiChoiceAdapter.notifyDataSetChanged();
}
});
alertDialog.setOnCancelListener(this);
alertDialog.show();
return true;
}
public void setItems(List<GradeBean> gradeBeanList, int defaultSelected,MultiSpinnerListener listener) {
mGradeBeanList.clear();
Log.e("list",""+gradeBeanList);
mGradeBeanList.addAll(gradeBeanList);
this.listener = listener;
items = new ArrayList<String>();
// all selected by default
selected = new boolean[mGradeBeanList.size()];
for (int i = 0; i < selected.length; i++) {
if (defaultSelected == i) {
selected[i] = true;
} else {
selected[i] = false;
}
}
for(int i= 0;i<mGradeBeanList.size();i++)
{
items.add(mGradeBeanList.get(i).getGradeName());
}
// all text on the spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.row_spinner, new String[] { mGradeBeanList.get(defaultSelected).getGradeName()});
setAdapter(adapter);
performClick();
alertDialog.cancel();
}
@Override
public void onCancel(DialogInterface dialog) {
StringBuilder spinnerBuilder = new StringBuilder();
StringBuilder gradeBuilder = new StringBuilder();
for (int i = 0; i < items.size(); i++) {
if (selected[i] == true) {
spinnerBuilder.append(items.get(i));
spinnerBuilder.append(", ");
gradeBuilder.append(mGradeBeanList.get(i).getGradeId());
gradeBuilder.append(", ");
}
}
String spinnerText = "";
if (spinnerBuilder.length() > 2) {
spinnerText = spinnerBuilder.substring(0, spinnerBuilder.length() - 2).toString();
mGrades = spinnerText;
}
if (gradeBuilder.length() > 2) {
mGradeIds = gradeBuilder.substring(0, gradeBuilder.length() - 2).toString();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),R.layout.row_spinner,new String[] { spinnerText });
setAdapter(adapter);
listener.onItemsSelected(selected);
}
public interface MultiSpinnerListener {
public void onItemsSelected(boolean[] selected);
}
public String getGradeIds() {
return mGradeIds;
}
public String getGrades() {
return mGrades;
}
public void setItems(List<GradeBean> gradeBeanList,List<String> itemValues,String selectedList,MultiSpinnerListener listener) {
mGradeBeanList.clear();
Log.e("list",""+gradeBeanList);
mGradeBeanList.addAll(gradeBeanList);
this.listener = listener;
items = new ArrayList<String>();
for(int i= 0;i<mGradeBeanList.size();i++)
{
items.add(mGradeBeanList.get(i).getGradeName());
}
// Set false by default
selected = new boolean[mGradeBeanList.size()];
for (int j = 0; j < itemValues.size(); j++)
selected[j] = false;
String spinnerText = "";
StringBuilder gradeBuilder= new StringBuilder();
if (selectedList != null) {
String[] selectedItems = selectedList.trim().split(",");
for (int i = 0; i < selectedItems.length; i++)
for (int j = 0; j < mGradeBeanList.size(); j++)
if (selectedItems[i].trim().equals(mGradeBeanList.get(j).getGradeName())) {
selected[j] = true;
gradeBuilder.append(mGradeBeanList.get(j).getGradeId());
gradeBuilder.append(", ");
spinnerText += (spinnerText.equals("")?"":", ") + items.get(j);
break;
}
}
mGrades = spinnerText;
if (gradeBuilder.length() > 2) {
mGradeIds = gradeBuilder.substring(0, gradeBuilder.length() - 2).toString();
}
// Text for the spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),R.layout.row_spinner, new String[] { spinnerText });
setAdapter(adapter);
}
}
和row_spinner.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_name"
style="@style/style_tv_normal_fill"
android:singleLine="true"
android:text="TEXT1"
android:padding="@dimen/d_tv_row_spinner_padding"/>
您可以根据需要更改代码。