PickerView和WheelView
- PickerView是什么
- WheelView的使用
- 效果图
- 多级联动
1、PickerView是什么
仿iOS的PickerView控件,带有3D圆弧效果,并封装了时间选择和选项选择这两种选择器。
2、WheelView的使用
导入依赖:
implementation 'com.contrarywind:Android-PickerView:4.1.9'
xml配置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.contrarywind.view.WheelView
android:id="@+id/wheel_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
代码
package com.bw.day11;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private WheelView wheelView;
private ArrayWheelAdapter<String> adapter;
private List<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
list.add("MESA");
list.add("HARROW");
list.add("VOLT");
list.add("GAUSS");
adapter = new ArrayWheelAdapter<>(list);
wheelView.setAdapter(adapter);
wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(int index) {
Toast.makeText(MainActivity.this, list.get(index), Toast.LENGTH_SHORT).show();
}
});
}
private void initView() {
wheelView = findViewById(R.id.wheel_view);
}
}
3.效果图:

4.级联选择
package com.bw.day11;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private WheelView wheelView;
private ArrayWheelAdapter<String> adapter;
private List<String> list1 = new ArrayList<>();
private List<String> list = new ArrayList<>();
private List<String> list2 = new ArrayList<>();
private List<String> list3 = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
list.add("MESA");
list.add("HARROW");
list.add("VOLT");
adapter = new ArrayWheelAdapter<>(list);
wheelView.setAdapter(adapter);
wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(int index) {
Toast.makeText(MainActivity.this, list.get(index), Toast.LENGTH_SHORT).show();
}
});
}
private void initView() {
wheelView = findViewById(R.id.wheel_view);
}
public void onClick(View view) {
//集合一
list1.add("郭靖");
list1.add("黄蓉");
list1.add("杨铁心");
list1.add("欧阳克");
//集合二
list2.add("小龙女");
list2.add("杨过");
list2.add("大雕");
list2.add("warframe");
//集合三
list3.add("小柜子");
list3.add("康熙");
list3.add("康亲王");
list3.add("鳌拜");
//多个集合
final List<List<String>> lists = new ArrayList<>();
lists.add(list1);
lists.add(list2);
lists.add(list3);
OptionsPickerView<String> build = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
String tx = lists.get(options1) + lists.get(options1).get(options2);
Toast.makeText(MainActivity.this, tx, Toast.LENGTH_SHORT).show();
}
}).build();
build.setPicker(list,lists);
build.show();
}
}
本文介绍了Android中的PickerView控件,它模仿了iOS的PickerView,具有3D圆弧效果,支持时间选择和选项选择。同时,还详细讲解了WheelView的使用,包括导入依赖、XML配置和代码实现,最后展示了多级联动的效果。

6735

被折叠的 条评论
为什么被折叠?



