依赖
//wheelView
implementation 'com.contrarywind:wheelview:4.0.9'
//ArrayWheelAdapter
implementation 'com.contrarywind:Android-PickerView:4.1.9'
1、一级demo
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.contrarywind.view.WheelView
android:id="@+id/id_wheel_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Activity
public class WheelViewActivity extends AppCompatActivity {
public static final String TAG = "TAG_ZLZ";
@BindView(R.id.id_wheel_view)
WheelView wheelView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_wheel_view);
ButterKnife.bind(this);
initWheelView();
}
/**
* 示例 1
*/
private void initWheelView() {
//初始化数据
final List<String> countryList = new ArrayList<>();
countryList.add("中国");
countryList.add("美国");
countryList.add("俄罗斯");
countryList.add("日本");
countryList.add("印度");
countryList.add("英国");
countryList.add("法国");
countryList.add("德国");
//配置wheelView属性
ArrayWheelAdapter adapter = new ArrayWheelAdapter(countryList);
wheelView.setAdapter(adapter);
wheelView.setCyclic(false); //取消循环显示数据
wheelView.setCurrentItem(0); //当前显示第一条
wheelView.setItemsVisibleCount(4); //可见范围为4个
wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(int index) {
Log.i(TAG, countryList.get(index));
}
});
}
}
2、二级demo
layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context=".wheelview.WheelViewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="@android:color/white"
android:orientation="horizontal"
android:paddingTop="30dp">
<com.contrarywind.view.WheelView
android:id="@+id/id_wheel_view_country"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.contrarywind.view.WheelView
android:id="@+id/id_wheel_view_city"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="@+id/id_tv_res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:text="res"
android:textColor="@android:color/white"
android:textSize="30dp" />
</RelativeLayout>
Activity
public class WheelViewActivity extends AppCompatActivity {
@BindView(R.id.id_wheel_view_country)
WheelView wheelViewCountry;
@BindView(R.id.id_wheel_view_city)
WheelView wheelViewCity;
@BindView(R.id.id_tv_res)
TextView tvResult;
private List<String> countryList = null;
private List<List<String>> cityList = null;
private ArrayWheelAdapter countryAdapter = null;
private ArrayWheelAdapter cityAdapter = null;
private int countryNum = 0;
private int cityNum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_wheel_views);
ButterKnife.bind(this);
//国家
initCountryWheelView();
//城市
initCityWheelView();
}
/**
* 国家
*/
private void initCountryWheelView() {
countryList = new ArrayList<>();
countryList.add("中国");
countryList.add("美国");
countryList.add("日本");
countryAdapter = new ArrayWheelAdapter(countryList);
wheelViewCountry.setAdapter(countryAdapter);
wheelViewCountry.setCyclic(false); //取消循环显示数据
wheelViewCountry.setCurrentItem(0); //当前显示第一条
wheelViewCountry.setItemsVisibleCount(2); //可见范围为2个
wheelViewCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
@SuppressLint("SetTextI18n")
@Override
public void onItemSelected(int index) {
countryNum = index;
if (wheelViewCity != null) {
cityAdapter = new ArrayWheelAdapter(cityList.get(index));
wheelViewCity.setAdapter(cityAdapter);
wheelViewCity.setCurrentItem(0);
cityNum = 0;
tvResult.setText("country : " + countryNum + ", " + "city : " + cityNum);
}
}
});
}
/**
* 城市
*/
private void initCityWheelView() {
List<String> cnCityList = new ArrayList<>();
cnCityList.add("北京");
cnCityList.add("上海");
cnCityList.add("广州");
cnCityList.add("深圳");
List<String> usCityList = new ArrayList<>();
usCityList.add("华盛顿");
usCityList.add("纽约");
usCityList.add("底特律");
List<String> jpCityList = new ArrayList<>();
jpCityList.add("东京");
jpCityList.add("秋叶原");
jpCityList.add("横滨");
jpCityList.add("大版");
cityList = new ArrayList<>();
cityList.add(cnCityList);
cityList.add(usCityList);
cityList.add(jpCityList);
cityAdapter = new ArrayWheelAdapter(cityList.get(0));
wheelViewCity.setAdapter(cityAdapter);
wheelViewCity.setCyclic(false); //取消循环显示数据
wheelViewCity.setCurrentItem(0); //当前显示第一条
wheelViewCity.setItemsVisibleCount(2); //可见范围为2个
wheelViewCity.setOnItemSelectedListener(new OnItemSelectedListener() {
@SuppressLint("SetTextI18n")
@Override
public void onItemSelected(int index) {
cityNum = index;
tvResult.setText("country : " + countryNum + ", " + "city : " + cityNum);
}
});
}
}