简单的小区公告,系统公告

这里写图片描述

支持上下滑动,点击单条公告

NoticeTextView

public class NoticeTextView extends LinearLayout {

    private Context mContext;
    private ViewFlipper viewFlipper;
    private View marqueeTextView;
    private List<String> list;
    private NoticeTextViewClickListener noticeTextViewClickListener;
    private float downX;    //按下时 的X坐标
    private float downY;    //按下时 的Y坐标

    public NoticeTextView(Context context) {
        super(context);
        mContext = context;
        initBasicView();
        initDetector(context);
    }

    private void initDetector(Context context) {

    }


    public NoticeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initBasicView();
    }

    /**
     * 设置初始数据和监听
     * @param list
     * @param noticeTextViewClickListener
     */
    public void setTextArraysAndClickListener(List<String> list,int time, NoticeTextViewClickListener noticeTextViewClickListener) {
        this.list = list;
        this.noticeTextViewClickListener = noticeTextViewClickListener;
        initMarqueeTextView(list, time,noticeTextViewClickListener);
    }

    private void initBasicView() {
        marqueeTextView = LayoutInflater.from(mContext).inflate(R.layout.notice_textview_layout, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        addView(marqueeTextView, layoutParams);
        viewFlipper = (ViewFlipper) marqueeTextView.findViewById(R.id.viewFlipper);
        viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_in_bottom));
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_out_top));
        viewFlipper.startFlipping();
        viewFlipper.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });
    }

    public void initMarqueeTextView(List<String> list,int time, final NoticeTextViewClickListener marqueeTextViewClickListener) {
        if (list.size() == 0) {
            TextView textView = new TextView(mContext);
            textView.setText("暂无公告");
            viewFlipper.addView(textView);
            return;
        }
        viewFlipper.removeAllViews();
        viewFlipper.setFlipInterval(time);
        for (int i = 0; i < list.size(); i++) {

            TextView textView = new TextView(mContext);
            textView.setText(list.get(i));
            final int finalI = i;
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    marqueeTextViewClickListener.onClick(view, finalI);
                }
            });

            textView.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    String action = "";
                    //在触发时回去到起始坐标
                    float x = motionEvent.getX();
                    float y = motionEvent.getY();
                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            //将按下时的坐标存储
                            downX = x;
                            downY = y;
                            //手指放下停止自动循环
                            viewFlipper.stopFlipping();
                            break;
                        case MotionEvent.ACTION_UP:
                            //手指抬起开始循环
                            viewFlipper.startFlipping();
                            //获取到距离差
                            float dx = x - downX;
                            float dy = y - downY;
                            //防止是按下也判断
                            if (Math.abs(dx) > 2 && Math.abs(dy) > 8) {
                                //通过距离差判断方向
                                int orientation = getOrientation(dx, dy);
                                switch (orientation) {
                                    case 'r':
                                        action = "右";
                                        break;
                                    case 'l':
                                        action = "左";
                                        break;
                                    case 't':
                                        action = "上";
                                        viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_in_bottom));
                                        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_out_top));
                                        viewFlipper.showNext();
                                        break;
                                    case 'b':
                                        action = "下";
                                        viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_from_up_in));
                                        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_from_down_out));
                                        viewFlipper.showPrevious();
                                        break;
                                }
                            }

                            //重新设置方向
                            viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_in_bottom));
                            viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_out_top));
                            break;
                    }

                    return false;
                }
            });

            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            viewFlipper.addView(textView, lp);

        }
    }

    public void releaseResources() {
        if (marqueeTextView != null) {
            if (viewFlipper != null) {
                viewFlipper.stopFlipping();
                viewFlipper.removeAllViews();
                viewFlipper = null;
            }
            marqueeTextView = null;
        }
    }

    /**
     * 根据距离差判断 滑动方向
     *
     * @param dx X轴的距离差
     * @param dy Y轴的距离差
     * @return 滑动的方向
     */
    private int getOrientation(float dx, float dy) {
        if (Math.abs(dx) > Math.abs(dy)) {
            //X轴移动
            return dx > 0 ? 'r' : 'l';
        } else {
            //Y轴移动
            return dy > 0 ? 'b' : 't';
        }
    }

    public interface NoticeTextViewClickListener {
        void onClick(View view, int id);
    }

}

notice_textview_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ViewFlipper
        android:id="@+id/viewFlipper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

        android:padding="8dp">
    </ViewFlipper>
</LinearLayout>

anim_from_down_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
    <alpha
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

anim_from_up_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="-50%p"
        android:toYDelta="0" />
    <alpha
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

anim_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="50%p"
        android:toYDelta="0" />
    <alpha
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

anim_out_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="0"
        android:toYDelta="-50%p" />
    <alpha
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

使用

        marqueeTv = (NoticeTextView) findViewById(R.id.marqueeTv);
        list=new ArrayList<>();
        for (int i = 0; i <5 ; i++) {
            list.add("这是公告:"+i);
        }
        marqueeTv.setTextArraysAndClickListener(list, 2000,new NoticeTextView.NoticeTextViewClickListener() {
            @Override
            public void onClick(View view, int id) {
                System.out.println("=====id========="+id);
            }
        });
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值