要实现Spinner选项的分组折叠与展开,可以通过自定义Spinner的适配器(Adapter)来实现。以下是一个简单的示例:
public class GroupItem {
private String groupName;
private List<String> childItems;
public GroupItem(String groupName, List<String> childItems) {
this.groupName = groupName;
this.childItems = childItems;
}
public String getGroupName() {
return groupName;
}
public List<String> getChildItems() {
return childItems;
}
}
public class CustomSpinnerAdapter extends BaseAdapter {
private List<GroupItem> groupItems;
private Context context;
public CustomSpinnerAdapter(Context context, List<GroupItem> groupItems) {
this.context = context;
this.groupItems = groupItems;
}
@Override
public int getCount() {
int count = 0;
for (GroupItem groupItem : groupItems) {
count += groupItem.getChildItems().size() + 1; // 计算每个分组下的选项数量,并加上分组头部
}
return count;
}
@Override
public Object getItem(int position) {
int count = 0;
for (GroupItem groupItem : groupItems) {
int groupSize = groupItem.getChildItems().size() + 1;
if (position < count + groupSize) {
if (position == count) {
return groupItem.getGroupName(); // 返回分组头部
} else {
return groupItem.getChildItems().get(position - count - 1); // 返回选项内容
}
}
count += groupSize;
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setPadding(20, 10, 20, 10);
Object item = getItem(position);
if (item instanceof String) {
// 显示选项内容
textView.setText((String) item);
} else if (item instanceof String) {
// 显示分组头部
textView.setText((String) item);
textView.setBackgroundColor(Color.GRAY);
}
return textView;
}
}
public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private List<GroupItem> groupItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
initData();
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this, groupItems);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = adapter.getItem(position);
if (item instanceof String) {
// 处理选项点击事件
String selectedItem = (String) item;
Toast.makeText(MainActivity.this, selectedItem, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void initData() {
groupItems = new ArrayList<>();
List<String> group1Items = new ArrayList<>();
group1Items.add("Option 1");
group1Items.add("Option 2");
groupItems.add(new GroupItem("Group 1", group1Items));
List<String> group2Items = new ArrayList<>();
group2Items.add("Option 3");
group2Items.add("Option 4");
groupItems.add(new GroupItem("Group 2", group2Items));
}
}
通过以上步骤,可以实现Spinner选项的分组折叠与展开功能。在自定义的适配器中,根据位置判断是分组头部还是选项内容,并在getView方法中设置不同的UI样式。在Activity中根据选中的位置处理相应的逻辑。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。