android Picker选择器

定义了一个通用的base adapterpackage com.whuthm.picker;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.view.View;import android.view.ViewGroup;/** * 基类
摘要由CSDN通过智能技术生成

定义了一个通用的base adapter

package com.whuthm.picker;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

/**
 * 基类adapter
 * 
 * @author whuthm
 * @date 2015-6-25
 */
public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {
    protected Context context;
    protected List<T> list = null;
    List<BaseAdapter.ViewHolder<T>> holders = new ArrayList<BaseAdapter.ViewHolder<T>>();
    
    public BaseAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
    }
    
    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }
    
    @Override
    public Object getItem(int position) {
        return list != null && position >= 0 && position < list.size() ? list
                .get(position) : null;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder<T> holder;
        if (convertView == null) {
            convertView = createView(context, parent);
            holder = createViewHolder();
            convertView.setTag(holder);
            holder.init(context, convertView);
            holders.add(holder);
        }
        else {
            holder = (ViewHolder<T>) convertView.getTag();
        }
        T data = list.get(position);
        holder.position = position;
        holder.update(context, data);
        return convertView;
    }
    
    public abstract View createView(Context context, ViewGroup parent);
    
    public abstract ViewHolder<T> createViewHolder();
    
    /**
     * 保存当前Item的view
     */
    public static abstract class ViewHolder<T> {
        public int position;
        
        public abstract void init(Context context, View convertView);
        
        public abstract void update(Context context, T data);
    }
}


PickerView的实现

package com.whuthm.picker;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PorterDuff;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.Scroller;

public class PickerView extends FrameLayout {

	/**
	 * The coefficient by which to adjust (divide) the max fling velocity.
	 */
	private static final int SELECTOR_MAX_FLING_VELOCITY_ADJUSTMENT = 8;

	/**
	 * The the duration for adjusting the selector wheel.
	 */
	private static final int SELECTOR_ADJUSTMENT_DURATION_MILLIS = 500;

	private int mMinScrollY;
	private int mMaxScrollY;

	private int mSelectorIndex = 0;
	private int mSelectorCount = 0;
	private int mFixedWidth = 0;
	private int mFixedHeight = 0;

	private PickerInnerView mInnerView;

	private ListAdapter mAdapter;

	private int mVisibleStartIndex = -1;
	private int mVisibleEndIndex = -1;

	private int mPosition = -1;

	private SparseArray<View> mVisibleViews = new SparseArray<View>();
	private List<View> mScrapViews = new ArrayList<View>();

	private Drawable mSelectorForeground;

	/**
	 * Listener to be notified upon scroll state change.
	 */
	private OnScrollListener mOnScrollListener;

	private Scroller mScroller;

	/**
	 * The Y position of the last down event.
	 */
	private float mLastDownEventY;

	/**
	 * The Y position of the last down or move event.
	 */
	private float mLastDownOrMoveEventY;

	/**
	 * Determines speed during touch scrolling.
	 */
	private VelocityTracker mVelocityTracker;

	/**
	 * @see ViewConfiguration#getScaledTouchSlop()
	 */
	private int mTouchSlop;
	/**
	 * @see ViewConfiguration#getScaledMinimumFlingVelocity()
	 */
	private int mMinimumFlingVelocity;

	/**
	 * @see ViewConfiguration#getScaledMaximumFlingVelocity()
	 */
	private int mMaximumFlingVelocity;

	/**
	 * The current scroll state of the number picker.
	 */
	private int mScrollState = OnScrollListener.SCROLL_STATE_IDLE;

	/**
	 * Interface to listen for the picker scroll state.
	 */
	public interface OnScrollListener {
		/**
		 * The view is not scrolling.
		 */
		public static int SCROLL_STATE_IDLE = 0;

		/**
		 * The user is scrolling using the key, or adjust to right position
		 */
		public static int SCROLL_STATE_SCROLL = 2;

		/**
		 * The user is scrolling using touch, and his finger is still on the
		 * screen.
		 */
		public static int SCROLL_STATE_TOUCH_SCROLL = 2;

		/**
		 * The user had previously been scrolling using touch and performed a
		 * fling.
		 */
		public static int SCROLL_STATE_FLING_SCROLL = 3;

		public void onScrollStateChange(PickerView view, int scrollState);
	}

	public interface OnPickerSelectedListener {
		public void onPickerSelected(int position);
	}

	private OnPickerSelectedListener mPickerSelectedListener;

	private Shader mShader;
	private Paint mPaint;
	private Matrix mMatrix;

	p
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值