自定义右侧滑动栏 仿联系人

一、控件效果


二、思路

一个布局文件添加若干textview来实现展示每个key值,放入到一个layout中,通过onTouchEvent来实现滑动点击其中某一个key的值,直接上代码:


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by liqiyin on 2015/12/3.
 */
public class SortView extends RelativeLayout{
    private final String[] sortKeys = {"当前","历史","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    /**
     * 边距
     */
    private static final int PADDING = 10;

    boolean isLoaded = false;

    /**
     * 字体大小
     */
    private static final int TEXT_SIZE = 14;

    /**
     * 动画绑定view
     */
    View animView;
    /**
     * 主要布局
     */
    LinearLayout mainLayout;

    /**
     * 每个textView的高度
     */
    int itemTvHeight;

    Context context;

    OnKeyChangeListener onKeyChangeListener;

    int curIndex = -1;
    int oldIndex = -1;

    /**
     * 点击key的回调
     * @author lqy
     * @date 2016-2-23
     */
    public interface OnKeyChangeListener
    {
        /**
         * key变化
         * @param key
         */
        void change(String key);

        /**
         * 手指抬起之后调用
         */
        void changeAfter();
    }

    public SortView(Context context) {
        super(context);
        init(context);
    }

    public SortView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    
    public SortView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    	int measureMode = MeasureSpec.getMode(widthMeasureSpec);
    	if(measureMode == MeasureSpec.AT_MOST) {
    		setMeasuredDimension(50, heightMeasureSpec);
    	}
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(!isLoaded)
        {
            setTextViews();
            isLoaded = true;
        }
    }
    
    /**
     * 初始化
     * @param context
     */
    private void init(Context context){
    	this.context = context;
    	 mainLayout = new LinearLayout(context);
         mainLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
         mainLayout.setOrientation(LinearLayout.VERTICAL);
         mainLayout.setGravity(Gravity.CENTER_HORIZONTAL);
         mainLayout.setPadding(PADDING, PADDING, PADDING, PADDING);
         setBackgroundColor(Color.BLACK);
         addView(mainLayout);
    }

    /**
     * 放置textview
     */
    private void setTextViews()
    {
        itemTvHeight = (getHeight() - PADDING * 2) / sortKeys.length;

        Paint paint = new Paint();

        for(int i = 0; i < sortKeys.length; i++)
        {
            TextView textView = new TextView(context);
            textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, itemTvHeight));
            textView.setTextSize(TEXT_SIZE);
            textView.setText(sortKeys[i]);
            textView.setTextColor(Color.GRAY);
            textView.setSingleLine(true);
            textView.setGravity(Gravity.CENTER_VERTICAL);
            mainLayout.addView(textView);
            paint.setTextSize(TEXT_SIZE);
        }

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
        params.width = getChildAt(0).getLayoutParams().width * PADDING;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                curIndex = (int) ((event.getY() - PADDING) / itemTvHeight);
                oldIndex = curIndex;
                if(onKeyChangeListener != null && curIndex < sortKeys.length)
                {
                    onKeyChangeListener.change(sortKeys[curIndex]);
                    startAnim();
                }
                break;

            case MotionEvent.ACTION_MOVE:
                curIndex = (int) ((event.getY() - PADDING) / itemTvHeight);
                if(curIndex < 0){
                    curIndex = 0;//避免往上滑动的速度过快。index==-1的情况
                }
                if(onKeyChangeListener != null && curIndex != oldIndex && curIndex < sortKeys.length)
                {
                    onKeyChangeListener.change(sortKeys[curIndex]);
                    startAnim();
                    oldIndex = curIndex;
                }
                break;

            case MotionEvent.ACTION_UP:
                if(onKeyChangeListener != null && curIndex < sortKeys.length)
                {
                    onKeyChangeListener.changeAfter();
                }
                break;
        }
        return true;
    }

    public void setOnKeyChangeListener(OnKeyChangeListener onKeyChangeListener) {
        this.onKeyChangeListener = onKeyChangeListener;
    }
   
    /**
     * 放置一个动画播放效果的view
     * @param view
     */
    public void setAnimView(View view)
    {
        animView = view;
    }

    /**
     * 开启展示key的dialog动画效果
     */
    private void startAnim()
    {
        if(animView != null)
        {
            Animation anim = new AlphaAnimation(1.0f, 0.0f);
            anim.setDuration(500);
            anim.setInterpolator(new AccelerateInterpolator());
            anim.setStartOffset(1000);
            anim.setFillAfter(true);
            animView.startAnimation(anim);
        }
    }

    /**
     * 获取key值列表
     * @return
     */
    public String[] getSortKeys()
    {
        return sortKeys;
    }

}

mainactivity中的使用方法:

        sortView = (SortView) findViewById(R.id.sort);
        tvDialog = (TextView) findViewById(R.id.tv_show);
        sortView.setAnimView(tvDialog);
        sortView.setOnKeyChangeListener(new OnKeyChangeListener() {
			
			@Override
			public void changeAfter() {
			}
			
			@Override
			public void change(String key) {
				tvDialog.setText(key);
			}
		});



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值