Android编程之仿iPhone滚轮控件

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

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








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



	
	
		
		
	
	

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(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(cities[newValue]));
				city.setCurrentItem(cities[newValue].length / 2);
			}
		});
        
        country.setCurrentItem(2);
    }
}


滚动视图

你可能感兴趣的:(Android,Android编程)