您好,登錄后才能下訂單哦!
要實現帶復選框的列表項,可以使用Spinner控件配合自定義的適配器來實現。以下是一個簡單的示例代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp" />
</LinearLayout>
public class CheckboxAdapter extends BaseAdapter {
private Context context;
private List<String> itemList;
private LayoutInflater inflater;
public CheckboxAdapter(Context context, List<String> itemList) {
this.context = context;
this.itemList = itemList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return itemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
view = inflater.inflate(R.layout.list_item_checkbox, null);
holder = new ViewHolder();
holder.checkBox = view.findViewById(R.id.checkBox);
holder.textView = view.findViewById(R.id.textView);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(itemList.get(position));
return view;
}
static class ViewHolder {
CheckBox checkBox;
TextView textView;
}
}
public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private CheckboxAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
List<String> itemList = new ArrayList<>();
itemList.add("Item 1");
itemList.add("Item 2");
itemList.add("Item 3");
adapter = new CheckboxAdapter(this, itemList);
spinner.setAdapter(adapter);
}
}
通過以上步驟,就可以實現帶復選框的列表項在Spinner中顯示了。當用戶點擊Spinner后,會彈出帶有復選框的列表供用戶選擇。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。