Android编程之仿iPhone滚轮控件

本文介绍了一款滚动视图控件,并通过实例演示了如何使用该控件来实现城市选择的功能。文中提供了完整的XML布局文件及Java代码,展示了如何设置适配器和监听器以实现联动效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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








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

<?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);
    }
}


滚动视图

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值