Android编程之仿iPhone滚轮控件

网上看到有人写了一个滚动组件,这个不错,大家可以看看

但是,我个人觉得这里有一处不是很好,大家可以试试:不循环的情况下,如果就是最后一个选项,你把它移到最上或者最下的位置,它回滚回到选择条时,是直接跳过去的,而不是自然滑动过去的,这个需要改进一下!







  这三张图分别是使用滚动控件实现城市,随机数和时间三个简单的例子,当然,界面有点简陋,下面我们就以时间这个为例,开始解析一下。

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical"  
  6.     android:background="@drawable/layout_bg"  
  7.     android:layout_width="fill_parent">  
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginTop="24dp"  
  12.         android:layout_gravity="center_horizontal"  
  13.         android:textSize="20sp"  
  14.         android:text="Please select the city" />  
  15.     <LinearLayout  
  16.         xmlns:android="http://schemas.android.com/apk/res/android"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_gravity="center_horizontal"  
  19.         android:layout_width="fill_parent"  
  20.         android:paddingLeft="12dp"  
  21.         android:paddingRight="12dp"  
  22.         android:paddingTop="4dp"  
  23.         android:layout_marginTop="8dp"  
  24.         android:orientation="horizontal">  
  25.         <kankan.wheel.widget.WheelView  
  26.             android:id="@+id/country"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_gravity="center_vertical"  
  30.             android:layout_weight="1" />  
  31.         <kankan.wheel.widget.WheelView  
  32.             android:id="@+id/city"  
  33.             android:layout_height="wrap_content"  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_marginLeft="10dp"  
  36.             android:layout_weight="1.5" />  
  37.     </LinearLayout>  
  38.     <!-- Button android:layout_width="wrap_content" android:layout_height="wrap_content"   
  39.         android:layout_gravity="center_horizontal" android:layout_marginTop="18dp"   
  40.         android:textSize="18sp" android:text=" Next > "/ -->  
  41. </LinearLayout>  

[java]  view plain copy
  1. package kankan.wheel.demo;  
  2.   
  3. import kankan.wheel.R;  
  4. import kankan.wheel.widget.ArrayWheelAdapter;  
  5. import kankan.wheel.widget.OnWheelChangedListener;  
  6. import kankan.wheel.widget.WheelView;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10.   
  11. public class CitiesActivity extends Activity {  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.   
  16.         setContentView(R.layout.cities_layout);  
  17.                   
  18.         WheelView country = (WheelView) findViewById(R.id.country);  
  19.         String countries[] = new String[] {"USA""Canada""Ukraine""France"};  
  20.         country.setVisibleItems(3);  
  21.         country.setAdapter(new ArrayWheelAdapter<String>(countries));  
  22.   
  23.         final String cities[][] = new String[][] {  
  24.                 new String[] {"New York""Washington""Chicago""Atlanta""Orlando"},  
  25.                 new String[] {"Ottawa""Vancouver""Toronto""Windsor""Montreal"},  
  26.                 new String[] {"Kiev""Dnipro""Lviv""Kharkiv"},  
  27.                 new String[] {"Paris""Bordeaux"},  
  28.                 };  
  29.           
  30.         final WheelView city = (WheelView) findViewById(R.id.city);  
  31.         city.setVisibleItems(5);  
  32.   
  33.         country.addChangingListener(new OnWheelChangedListener() {  
  34.             public void onChanged(WheelView wheel, int oldValue, int newValue) {  
  35.                 city.setAdapter(new ArrayWheelAdapter<String>(cities[newValue]));  
  36.                 city.setCurrentItem(cities[newValue].length / 2);  
  37.             }  
  38.         });  
  39.           
  40.         country.setCurrentItem(2);  
  41.     }  
  42. }  

转载于:https://my.oschina.net/dochong/blog/76876

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值