您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
要实现Spinner中选项的拖拽排序,可以通过自定义Adapter和拖拽功能来实现。以下是一个简单的实现示例:
public class CustomSpinnerAdapter extends BaseAdapter {
private Context mContext;
private List<String> mItems;
public CustomSpinnerAdapter(Context context, List<String> items) {
mContext = context;
mItems = items;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int position) {
return mItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_spinner_item, parent, false);
}
TextView textView = convertView.findViewById(android.R.id.text1);
textView.setText(mItems.get(position));
return convertView;
}
}
List<String> items = new ArrayList<>();
items.add("Option 1");
items.add("Option 2");
items.add("Option 3");
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this, items);
Spinner spinner = findViewById(R.id.spinner);
spinner.setAdapter(adapter);
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
// Swap the items in the list
Collections.swap(items, fromPosition, toPosition);
adapter.notifyDataSetChanged();
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
// Not needed for drag and drop sorting
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
通过以上步骤,就可以实现Spinner中选项的拖拽排序功能。需要注意的是,如果要在Spinner中使用拖拽排序,需要将Spinner替换为RecyclerView,并在RecyclerView中使用自定义Adapter和拖拽功能。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。