網上看到有人寫了一個滾動組件,這個不錯,你們能夠看看
可是,我我的以爲這裏有一處不是很好,你們能夠試試:不循環的狀況下,若是就是最後一個選項,你把它移到最上或者最下的位置,它回滾回到選擇條時,是直接跳過去的,而不是天然滑動過去的,這個須要改進一下!
html
這三張圖分別是使用滾動控件實現城市,隨機數和時間三個簡單的例子,固然,界面有點簡陋,下面咱們就以時間這個爲例,開始解析一下。
java
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:background="@drawable/layout_bg"
- android:layout_width="fill_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="24dp"
- android:layout_gravity="center_horizontal"
- android:textSize="20sp"
- android:text="Please select the city" />
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:layout_width="fill_parent"
- android:paddingLeft="12dp"
- android:paddingRight="12dp"
- android:paddingTop="4dp"
- android:layout_marginTop="8dp"
- android:orientation="horizontal">
- <kankan.wheel.widget.WheelView
- android:id="@+id/country"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_weight="1" />
- <kankan.wheel.widget.WheelView
- android:id="@+id/city"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_weight="1.5" />
- </LinearLayout>
- <!-- Button android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal" android:layout_marginTop="18dp"
- android:textSize="18sp" android:text=" Next > "/ -->
- </LinearLayout>
- package kankan.wheel.demo;
-
- import kankan.wheel.R;
- import kankan.wheel.widget.ArrayWheelAdapter;
- import kankan.wheel.widget.OnWheelChangedListener;
- import kankan.wheel.widget.WheelView;
-
- import android.app.Activity;
- import android.os.Bundle;
-
- public class CitiesActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.cities_layout);
-
- WheelView country = (WheelView) findViewById(R.id.country);
- String countries[] = new String[] {"USA", "Canada", "Ukraine", "France"};
- country.setVisibleItems(3);
- country.setAdapter(new ArrayWheelAdapter<String>(countries));
-
- final String cities[][] = new String[][] {
- new String[] {"New York", "Washington", "Chicago", "Atlanta", "Orlando"},
- new String[] {"Ottawa", "Vancouver", "Toronto", "Windsor", "Montreal"},
- new String[] {"Kiev", "Dnipro", "Lviv", "Kharkiv"},
- new String[] {"Paris", "Bordeaux"},
- };
-
- final WheelView city = (WheelView) findViewById(R.id.city);
- city.setVisibleItems(5);
-
- country.addChangingListener(new OnWheelChangedListener() {
- public void onChanged(WheelView wheel, int oldValue, int newValue) {
- city.setAdapter(new ArrayWheelAdapter<String>(cities[newValue]));
- city.setCurrentItem(cities[newValue].length / 2);
- }
- });
-
- country.setCurrentItem(2);
- }
- }