第十一天的PickerView和AutoSize
PickerView是什么
仿iOS的PickerView控件,带有3D圆弧效果,并封装了时间选择和选项选择这两种选择器。
导包
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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wheel"></com.contrarywind.view.WheelView>
</LinearLayout>
在代码中使用
package com.fenghongzhang.day011;
import android.os.Bundle;
import android.util.Log;
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 wheel;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wheel = (WheelView) findViewById(R.id.wheel);
//数据源
final List<String> list = new ArrayList<>();
list.add("我的中国心");
list.add("北国之春");
list.add("鹿港小镇");
wheel.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(int index) {
Log.i(TAG, "onItemSelected: "+list.get(index));
}
});
//数组适配器
ArrayWheelAdapter arrayWheelAdapter = new ArrayWheelAdapter(list);
//设置适配器
wheel.setAdapter(arrayWheelAdapter);
}
}