前言
安卓控件TextSwitcher的使用(实现Textview的上下滚动)
这篇中说到ViewSwitcher的使用,这边算是对Switcher的具体说明
使用类的效果展示
使用如下
List<String> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add("=>>>>"+i);
}
vs.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
return LayoutInflater.from(mContext).inflate(R.layout.item_bar,null,false);
}
});
ViewSwitcherAnimation<String> viewSwitcherAnimation = new ViewSwitcherAnimation<String>(vs,list) {
@Override
protected void bindData(String s, View view) {
ImageView imageView = (ImageView) view.findViewById(R.id.iv_bar);
TextView textView = (TextView) view.findViewById(R.id.tv_bar);
imageView.setImageResource(R.mipmap.add);
textView.setText("count"+s);
}
};
基础使用
和TextSwitcher一样也是设置工厂,然后再更新的地方刷新页面就好了
- xml布局
<ViewSwitcher
android:id="@+id/viewSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
- 代码设置
ViewSwitcher vs = (ViewSwitcher) findViewById(R.id.viewSwitcher);
vs.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
return LayoutInflater.from(mContext).inflate(R.layout.item_bar,null,false);
}
});
int count = 0;
//前一页
count--;
View view = vs.getNextView();
ImageView imageView = (ImageView) view.findViewById(R.id.iv_bar);
TextView textView = (TextView) view.findViewById(R.id.tv_bar);
imageView.setImageResource(R.mipmap.add);
textView.setText("count___"+count);
vs.showPrevious();
//后一页
count++;
View view = vs.getNextView();
ImageView imageView = (ImageView) view.findViewById(R.id.iv_bar);
TextView textView = (TextView) view.findViewById(R.id.tv_bar);
imageView.setImageResource(R.mipmap.add);
textView.setText("count___"+count);
vs.showNext();
可以看到 只要在showNext()
之前调用getNextView()
方法就可以获取需要被展示的页面view,然后对其加以设置布局就可以了。
使用类
对此,我也写了一个简单的使用类
ViewSwitcherAnimation.java
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ViewSwitcher;
import java.util.List;
/**
* ViewSwitcherAnimation
* Author: gjn.
* Time: 2018/3/15.
*/
public abstract class ViewSwitcherAnimation<T> {
private static final String TAG = "ViewSwitcherAnimation";
private static final int DURATION = 1000;
private ViewSwitcher viewSwitcher;
private List<T> datas;
private int marker;
private AnimationSet InAnimationSet;
private AnimationSet OutAnimationSet;
private int delayTime = 2000;
private Handler handler = new Handler();
private Runnable task = new Runnable() {
@Override
public void run() {
nextView();
handler.postDelayed(task, delayTime * 2);
}
};
public ViewSwitcherAnimation(ViewSwitcher viewSwitcher, List<T> views) {
this.viewSwitcher = viewSwitcher;
this.datas = views;
}
public void start() {
stop();
handler.postDelayed(task, delayTime);
}
public void stop(){
handler.removeCallbacks(task);
}
public ViewSwitcherAnimation setData(List<T> views) {
this.datas = views;
return this;
}
public int getMarker() {
return marker;
}
public void setDelayTime(int delayTime) {
this.delayTime = delayTime;
}
public void create(){
marker = -1;
if (datas == null){
Log.w(TAG, "datas is null");
return;
}
if (viewSwitcher == null) {
Log.w(TAG, "viewSwitcher is null");
return;
}
nextView();
createAnimation();
viewSwitcher.setInAnimation(InAnimationSet);
viewSwitcher.setOutAnimation(OutAnimationSet);
start();
}
private void createAnimation() {
AlphaAnimation alphaAnimation;
TranslateAnimation translateAnimation;
int h = viewSwitcher.getHeight();
if (h <= 0) {
viewSwitcher.measure(0,0);
h = viewSwitcher.getMeasuredHeight();
}
InAnimationSet = new AnimationSet(true);
OutAnimationSet = new AnimationSet(true);
alphaAnimation = new AlphaAnimation(0,1);
translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, h, Animation.ABSOLUTE, 0);
InAnimationSet.addAnimation(alphaAnimation);
InAnimationSet.addAnimation(translateAnimation);
InAnimationSet.setDuration(DURATION);
alphaAnimation = new AlphaAnimation(1,0);
translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -h);
OutAnimationSet.addAnimation(alphaAnimation);
OutAnimationSet.addAnimation(translateAnimation);
OutAnimationSet.setDuration(DURATION);
}
private void nextView() {
marker = ++marker % datas.size();
View view = viewSwitcher.getNextView();
bindData(datas.get(marker),view);
viewSwitcher.showNext();
}
protected abstract void bindData(T t, View view);
}