Chinaunix首页 | 论坛 | 博客
  • 博客访问: 83781
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 693
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-03 13:58
个人简介

淡泊明志,宁静致远

文章分类

全部博文(14)

文章存档

2013年(14)

我的朋友

分类: Android平台

2013-06-08 23:22:54

        在iOS里面有一种控件------滚筒控件(Wheel View),这通常用于设置时间/日期,非常方便,但Android SDK并没有提供类似的控件。这里说一下我的实现。
先看效果图
 
  • 1. checkout代码
            网上有开源的代码实现了改功能。svn地址:。checkout代码后加入项目中。
          2. 
实现WheelViewAdapter
            

点击(此处)折叠或打开

  1. package com.ccn.ui;

  2. import java.util.List;

  3. import android.content.Context;
  4. import android.database.DataSetObserver;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.TextView;

  9. import com.ccn.R;
  10. import com.ccn.widget.WheelViewAdapter;

  11. /**
  12.  * @todo 货币适配器
  13.  * @author ShuLiQun
  14.  * @date 2013-6-8
  15.  */
  16. public class CurrencyAdapter implements WheelViewAdapter {
  17.     private List<String> lists;
  18.     private Context context;

  19.     public void setLists(List<String> lists, Context context) {
  20.         this.lists = lists;
  21.         this.context = context;
  22.     }

  23.     @Override
  24.     public int getItemsCount() {
  25.         return lists.size();
  26.     }

  27.     @Override
  28.     public View getItem(int index, View convertView, ViewGroup parent) {
  29.         Info info;
  30.         if (convertView == null) {
  31.             convertView = LayoutInflater.from(context).inflate(R.layout.cc_item, null);
  32.             info = new Info();
  33.             info.tv = (TextView) convertView.findViewById(R.id.text1);
  34.             convertView.setTag(info);
  35.         } else {
  36.             info = (Info) convertView.getTag();
  37.         }
  38.         
  39.         info.tv.setText(lists.get(index));
  40.         return convertView;
  41.     }

  42.     private static class Info {
  43.         TextView tv;
  44.     }

  45.     @Override
  46.     public View getEmptyItem(View convertView, ViewGroup parent) {
  47.         return null;
  48.     }

  49.     @Override
  50.     public void registerDataSetObserver(DataSetObserver observer) {

  51.     }

  52.     @Override
  53.     public void unregisterDataSetObserver(DataSetObserver observer) {

  54.     }

  55. }
        3. 布局文件

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:background="@color/transport"
  6.     android:orientation="vertical" >

  7.     <LinearLayout
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="0dp"
  10.         android:layout_weight="4"
  11.         android:background="@drawable/wheel_view_bg"
  12.         android:orientation="horizontal" >

  13.         <LinearLayout
  14.             android:layout_width="fill_parent"
  15.             android:layout_height="fill_parent"
  16.             android:layout_margin="10dp"
  17.             android:orientation="horizontal" >

  18.             <com.ccn.widget.WheelView
  19.                 android:id="@+id/wvCurrency1"
  20.                 android:layout_width="0dp"
  21.                 android:layout_height="fill_parent"
  22.                 android:layout_weight="1" />

  23.             <com.ccn.widget.WheelView
  24.                 android:id="@+id/wvCurrency2"
  25.                 android:layout_width="0dp"
  26.                 android:layout_height="fill_parent"
  27.                 android:layout_weight="1" />

  28.             <com.ccn.widget.WheelView
  29.                 android:id="@+id/wvCurrency3"
  30.                 android:layout_width="0dp"
  31.                 android:layout_height="fill_parent"
  32.                 android:layout_weight="1" />

  33.             <com.ccn.widget.WheelView
  34.                 android:id="@+id/wvCurrency4"
  35.                 android:layout_width="0dp"
  36.                 android:layout_height="fill_parent"
  37.                 android:layout_weight="1" />
  38.         </LinearLayout>

  39.         <View
  40.             android:layout_width="fill_parent"
  41.             android:layout_height="fill_parent"
  42.             android:visibility="gone" />
  43.     </LinearLayout>

  44. </LinearLayout>
        4. 在界面添加
     

点击(此处)折叠或打开

  1. package com.ccn.ui;

  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;

  5. import android.app.Activity;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.Bundle;
  9. import android.support.v4.view.ViewPager;
  10. import android.support.v4.view.ViewPager.OnPageChangeListener;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.view.animation.Animation;
  15. import android.view.animation.TranslateAnimation;
  16. import android.widget.ImageView;
  17. import android.widget.LinearLayout;

  18. import com.ccn.R;
  19. import com.ccn.util.ImageUtil;
  20. import com.ccn.widget.OnWheelScrollListener;
  21. import com.ccn.widget.WheelView;

  22. /**
  23.  * @todo Tab页面手势滑动切换以及动画效果
  24.  * @author ShuLiQun
  25.  * @date 2013-6-7
  26.  */
  27. public class ConversionUI extends Activity implements OnClickListener, OnPageChangeListener, OnWheelScrollListener {
  28.     private WheelView wv1;
  29.     private WheelView wv2;
  30.     private WheelView wv3;
  31.     private WheelView wv4;


  32.     @Override
  33.     public void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.conversion);

  36.         initWheelView();
  37.     }

  38.     /**
  39.      * @todo 初始化滚筒
  40.      * @date 2013-6-8
  41.      */
  42.     private void initWheelView() {
  43.         wv1 = (WheelView) llConverter.findViewById(R.id.wvCurrency1);
  44.         wv2 = (WheelView) llConverter.findViewById(R.id.wvCurrency2);
  45.         wv3 = (WheelView) llConverter.findViewById(R.id.wvCurrency3);
  46.         wv4 = (WheelView) llConverter.findViewById(R.id.wvCurrency4);
  47.         String[] items = getResources().getStringArray(R.array.money);
  48.         lists = Arrays.asList(items);
  49.         currencyAdapter.setLists(lists, this);
  50.         wv1.setViewAdapter(currencyAdapter);
  51.         wv2.setViewAdapter(currencyAdapter);
  52.         wv3.setViewAdapter(currencyAdapter);
  53.         wv4.setViewAdapter(currencyAdapter);

  54.         wv1.addScrollingListener(this);
  55.         wv2.addScrollingListener(this);
  56.         wv3.addScrollingListener(this);
  57.         wv4.addScrollingListener(this);
  58.     }

  59.     @Override
  60.     public void onScrollingStarted(WheelView wheel) {
  61.         
  62.         
  63.     }

  64.     @Override
  65.     public void onScrollingFinished(WheelView wheel) {
  66.         if (wheel == wv1) {
  67.             System.err.println("wv1============="+lists.get(wheel.getCurrentItem()));
  68.         }else if (wheel == wv2) {
  69.             System.err.println("wv2============="+lists.get(wheel.getCurrentItem()));
  70.         }else if (wheel == wv3) {
  71.             System.err.println("wv3============="+lists.get(wheel.getCurrentItem()));
  72.         }else if (wheel == wv4) {
  73.             System.err.println("wv4============="+lists.get(wheel.getCurrentItem()));
  74.         }
  75.         
  76.         
  77.     }
  78. }
        5. 总结
           
代码是在一个项目中,就只贴了部分代码,我也正在研究中。
阅读(7008) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:CentOS更改开机启动顺序

给主人留下些什么吧!~~