网上看到有人写了一个滚动组件,这个不错,大家可以看看
但是,我个人觉得这里有一处不是很好,大家可以试试:不循环的情况下,如果就是最后一个选项,你把它移到最上或者最下的位置,它回滚回到选择条时,是直接跳过去的,而不是自然滑动过去的,这个需要改进一下!
这三张图分别是使用滚动控件实现城市,随机数和时间三个简单的例子,当然,界面有点简陋,下面我们就以时间这个为例,开始解析一下。
- <?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);
- }
- }