有时候我们需要做一些向上滚动播报一些消息,我们来看一看怎么实现
package com.weizhong.shuowan.widget;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ViewFlipper;
import com.weizhong.shuowan.R;
import com.weizhong.shuowan.bean.DownloadInfo;
import com.weizhong.shuowan.observer.ExitActivityObserver;
import com.weizhong.shuowan.utils.LayoutInflaterUtils;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by lzc on 2017/4/5.
* 首页滚动消息视图
*/
public class ScrollMessageView extends ViewFlipper {
private Timer mTimer;
private TimerTask mTimerTask;
private int mCurrentIndex;
private ArrayList<DownloadInfo> mDownloadInfoList;
private DownloadScrollItemLayout mDownloadScrollItemLayoutFirst;//要滚动的布局
private DownloadScrollItemLayout mDownloadScrollItemLayoutSecond;
private ScrollHandler mScrollHandler;
public ScrollMessageView(Context context, AttributeSet attrs) {
super(context, attrs);
mDownloadScrollItemLayoutFirst = (DownloadScrollItemLayout)LayoutInflaterUtils.inflateView(context,R.layout.download_scroll_item_layout);
mDownloadScrollItemLayoutSecond = (DownloadScrollItemLayout)LayoutInflaterUtils.inflateView(context,R.layout.download_scroll_item_layout);
mScrollHandler = new ScrollHandler(this);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
setInAnimation(getContext(),R.anim.in_bottomtop);
setOutAnimation(getContext(), R.anim.out_topbottom);
}
public synchronized void setDownloadInfos(ArrayList<DownloadInfo> downloadInfos){
if(mTimer != null){
mTimer.cancel();
}
mTimer = new Timer();
if(mTimerTask != null){
mTimerTask.cancel();
}
mTimerTask = new TimerTask() {
@Override
public void run() {
nextInfo();
}
};
mTimer.schedule(mTimerTask,5000,5000);
mCurrentIndex = 0;
mDownloadInfoList = downloadInfos;
removeAllViews();
if(mDownloadInfoList.size() > 1){
mDownloadScrollItemLayoutFirst.setDownloadInfo(downloadInfos.get(0));
mDownloadScrollItemLayoutSecond.setDownloadInfo(downloadInfos.get(1));
addView(mDownloadScrollItemLayoutFirst);
addView(mDownloadScrollItemLayoutSecond);
}
}
private void nextInfo(){
if(mScrollHandler != null){
mScrollHandler.post(new Runnable() {
@Override
public void run() {
mCurrentIndex = mCurrentIndex + 1;
if(mCurrentIndex >= mDownloadInfoList.size()){
mCurrentIndex = 0;
if(mOnDataEndListener != null){
mOnDataEndListener.onDataEnd();
}
return;
}
if(mCurrentIndex%2 == 0){
mDownloadScrollItemLayoutFirst.setDownloadInfo(mDownloadInfoList.get(mCurrentIndex));
}else{
mDownloadScrollItemLayoutSecond.setDownloadInfo(mDownloadInfoList.get(mCurrentIndex));
}
showNext();
}
});
}
}
public interface OnDataEndListener{
void onDataEnd();
}
private OnDataEndListener mOnDataEndListener;
public void setOnDataEndListener(OnDataEndListener onDataEndListener){
mOnDataEndListener = onDataEndListener;
}
private static class ScrollHandler extends Handler{
WeakReference<ScrollMessageView> weakReference;
public ScrollHandler(ScrollMessageView scrollMessageView) {
this.weakReference = new WeakReference<ScrollMessageView>(scrollMessageView);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(this.weakReference != null && this.weakReference.get() != null){
this.weakReference.get().onReceiveMessage(msg);
}
}
}
private void onReceiveMessage(Message msg){
}
@Override
public void onActivityDestory() {
if(mTimer != null){
mTimer.cancel();
mTimer = null;
}
if(mTimerTask != null){
mTimerTask.cancel();
mTimerTask = null;
}
mDownloadScrollItemLayoutFirst = null;
mDownloadScrollItemLayoutSecond = null;
}
}
in_bottomtop.xml文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="750"
android:fromYDelta="100.0%p"
android:toYDelta="0.0" />
<alpha android:duration="750"
android:fromAlpha="0"
android:toAlpha="1"/>
</set>
out_topbottom.xml文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="750"
android:fromYDelta="0.0"
android:toYDelta="-100.0%p" />
<alpha android:duration="750"
android:fromAlpha="1"
android:toAlpha="0"/>
</set>