android kotlin 垂直滚动(消息轮播)

这是个仿淘宝 首页上下滚动消息的layout
+使用方式

/**
 * Created by mocaris on 2017/12/20.
 * 垂直跑马灯
 */

public class VerticalRollingLayout extends ViewFlipper {

    public static final int TOP_BOTTOM = 0;
    public static final int BOTTOM_TOP = 1;

    //滚动方向  上下  下上
    private int rollingType = 0;
    //是否重复滚动  否,只将所有滚动完毕停止
//    private boolean mRepeat;
    //滚动间隔 默认1000
    private int mRollingInterval;
    //动画时间
    private int mAnimDuration;
    private ListAdapter mListAdapter;

    public VerticalRollingLayout(Context context) {
        this(context, null);
    }

    public VerticalRollingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.VerticalRollingLayout);
        rollingType = typedArray.getInt(R.styleable.VerticalRollingLayout_rolling_type, 0);
//        mRepeat = typedArray.getBoolean(R.styleable.VerticalRollingLayout_repeat, false);
        mRollingInterval = typedArray.getInt(R.styleable.VerticalRollingLayout_rolling_interval, 3500);
        mAnimDuration = typedArray.getInt(R.styleable.VerticalRollingLayout_rolling_duration, 1000);
        typedArray.recycle();
        InitRollingAnim();
    }

    public int getRollingType() {
        return rollingType;
    }

    /**
     * @param rollingType {@link #TOP_BOTTOM} {@link #BOTTOM_TOP}
     */
    public void setRollingType(int rollingType) {
        this.rollingType = rollingType;
    }

    public void setAdapter(ListAdapter listAdapter) {
        this.mListAdapter = listAdapter;
        initChildView();
    }

    public void notifyDataSetChanged() {
        initChildView();
    }

    private void initChildView() {
        if (null == mListAdapter) {
            return;
        }
        this.removeAllViews();
        for (int i = 0; i < mListAdapter.getCount(); i++) {
            View itemView = mListAdapter.getView(i, null, this);
            this.addView(itemView);
        }
    }

    /**
     * 初始化动画
     */
    private void InitRollingAnim() {
        this.clearAnimation();
        TranslateAnimation rollingAnimIn = null;
        TranslateAnimation rollingAnimOut = null;
        switch (rollingType) {
            case BOTTOM_TOP://从下到上
                rollingAnimOut = getRollingAnim(0f, 1f);
                rollingAnimIn = getRollingAnim(-1f, 0f);
                break;
            case TOP_BOTTOM://从上到下
            default:
                rollingAnimIn = getRollingAnim(1f, 0f);
                rollingAnimOut = getRollingAnim(0f, -1f);
                break;
        }
        this.setFlipInterval(mRollingInterval);
        this.setInAnimation(rollingAnimIn);
        this.setOutAnimation(rollingAnimOut);
    }

    /**
     * 动画
     *
     * @param fromY
     * @param toY
     * @return
     */
//    //int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue
    private TranslateAnimation getRollingAnim(float fromY, float toY) {
        TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF
                , 0
                , Animation.RELATIVE_TO_SELF
                , 0
                , Animation.RELATIVE_TO_SELF
                , fromY
                , Animation.RELATIVE_TO_SELF
                , toY);
        translateAnimation.setDuration(mAnimDuration);
        return translateAnimation;
    }

}

自定义属性

 
    
        
        
            
            
        
        
        
        
        
        
        
    

使用方法

     

。。。

       vrl_home.rollingType = VerticalRollingLayout.BOTTOM_TOP
//homeRollingAdapter 是继承ListAdapter 的   和ListAdapter 用法一样
        vrl_home.setAdapter(homeRollingAdapter)
        vrl_home.startFlipping()

你可能感兴趣的:(android kotlin 垂直滚动(消息轮播))