支持上下滑动,点击单条公告
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);
}
});