自定义ViewSwitcher, 支持自定义布局和轮播
android系统自带的ImageSwitcher,TextSwitcher只能分别设置单一的ImageView和TextView, 但是在很多情况下,我们希望把ViewSwithcer的动画效果能加载显示自己定义的布局。如实现淘宝头条效果(git制作比较粗糙):
通过查看ImageSwitcher和TextSwitcher的源码,我们会发现它们都继承自ViewSwitcher。所以,我们也可以定义一个继承ViewSwitcher的自己的Switcher
/**
* Created by liujian on on 16/7/8.
*/
public class MySwitcherView extends ViewSwitcher {
private boolean repeat;
private long durationTime = 3 * 1000;
private Handler switcherHandler = new Handler();
private SwitcherViewAdapter switcherViewAdapter;
private Runnable switcherRunnable;
private int showPosition;
public MySwitcherView(Context context) {
super(context);
}
public MySwitcherView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setVisibility(int visibility) {
super.setVisibility(visibility);
if(visibility == View.INVISIBLE || visibility == View.GONE)
stopSwitcherRepeat();
}
/**
* 等同于setFactory,调用者也可直接setFactory设置展示的视图
*
* @param context
* @param resID
*/
public void setContentView(final Context context, final int resID) {
setFactory(new ViewFactory() {
@Override
public View makeView() {
View view = LayoutInflater.from(context).inflate(resID, MySwitcherView.this, false);
if (view.getParent() != null)
((ViewGroup) view.getParent()).removeView(view);
return view;
}
});
}
/**
* @return switcherRunnable == null
*/
public boolean stopSwitcherRepeat() {
if (switcherRunnable == null)
return true;
switcherHandler.removeCallbacks(switcherRunnable);
return false;
}
public void restartSwitcherRepeat() {
if(repeat)
startSwitcherRepeat(durationTime);
}
/**
* 是否循环播放
* @param repeat
*/
public void setAnimationRepeat(boolean repeat) {
this.repeat = repeat;
}
/**
* 循环间隔时间
* @param durationTime
*/
public void setRepeatDuration(long durationTime) {
this.durationTime = durationTime;
}
/**
* 设置nextView数据渲染适配器
* @param switcherViewAdapter
*/
public void setSwitcherViewAdapter(SwitcherViewAdapter switcherViewAdapter) {
this.switcherViewAdapter = switcherViewAdapter;
if(switcherViewAdapter != null && switcherViewAdapter.getViewFactory(MySwitcherView.this) != null){
removeAllViews();
setFactory(switcherViewAdapter.getViewFactory(MySwitcherView.this));
}
startSwitcherRepeat(0);
}
public void showNextView(Runnable runnable) {
if (switcherViewAdapter != null) {
if (showPosition >= switcherViewAdapter.getCount())
showPosition = 0;
if (MySwitcherView.this.getNextView() != null) {
switcherViewAdapter.bindViewData(showPosition, MySwitcherView.this.getNextView());
MySwitcherView.this.showNext();
showPosition++;
if (repeat && runnable != null)
switcherHandler.postDelayed(runnable, durationTime);
}
}
}
private void startSwitcherRepeat(long time){
if (switcherViewAdapter != null && switcherViewAdapter.getCount() > 0) {
if (stopSwitcherRepeat())
switcherRunnable = new SwitcherRunnable(this);
switcherHandler.postDelayed(switcherRunnable, time);
}
}
private static class SwitcherRunnable implements Runnable {
private WeakReference<MySwitcherView> switcherViewWeakReference;
public SwitcherRunnable(MySwitcherView switcherView) {
switcherViewWeakReference = new WeakReference<>(switcherView);
}
@Override
public void run() {
MySwitcherView switcherView = switcherViewWeakReference.get();
if (switcherView != null)
switcherView.showNextView(this);
}
}
public interface SwitcherViewAdapter {
/**
* How many items are in the data set represented by this Adapter.
*
* @return Count of items.
*/
int getCount();
/**
* 用于setFactory
* @param parent
* @return 为null,则需手动调用MySwitcherView.setFactory
*/
ViewFactory getViewFactory(ViewGroup parent);
/**
* nextView数据渲染
*
* @param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param nextView the view that will be displayed after the next views flip.
*/
void bindViewData(int position, View nextView);
}
}
使用方法很简单,有点类似于listview,但由于ViewSwitcher只会同时存在两个childview,所以不需要对其子类做复用加载处理。
使用方法步骤如下:
- 1.新建自己的adapter类,实现MySwitcherView.SwitcherViewAdapter接口。
/**
* Created by liujian on 16/7/8.
*/
public class MySwitcherAdapter implements MySwitcherView.SwitcherViewAdapter{
private List<User> userList;
private LayoutInflater inflater;
public MySwitcherAdapter(Context context, List<User> niuRenList) {
this.inflater = LayoutInflater.from(context);
this.userList = userList;
}
@Override
public int getCount() {
return userList.size();
}
@Override
public ViewSwitcher.ViewFactory getViewFactory(final ViewGroup parent) {
ViewSwitcher.ViewFactory viewFactory = new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
//加载自定义布局
View view = inflater.inflate(R.layout.item_user_info, parent, false);
return view;
}
};
return viewFactory;
}
@Override
public void bindViewData(int position, View nextView) {
//绑定数据
User user = userList.get(position);
ImageView imageView = (ImageView) nextView.findViewById(R.id.imageView);
TextView textView = (TextView) nextView.findViewById(R.id.textView);
imageView.setImageURI(user.getUri());
textView.setText(user.getName());
}
}
- 2.在activity或者fragment中设置switcher参数即可
switcherView = (MySwitcherView)findViewById(R.id.switcherView);
//设置加载动画
switcherView.setInAnimation(getActivity(), R.anim.textswitcher_bottom_in);
switcherView.setOutAnimation(getActivity(), R.anim.textswitcher_bottom_out);
//是否轮播
switcherView.setAnimationRepeat(true);
//轮播间隔
switcherView.setRepeatDuration(REFRESH_NIU_REN_TIME * 1000);
//首次不加载动画
switcherView.setAnimateFirstView(false);
MySwitcherAdapter mySwitcherAdapter = new MySwitcherAdapter(this,userlist);
switcherView.setSwitcherViewAdapter(mySwitcherAdapter);
- 最后在onResume和onPause最好分别开始和暂停动画,节省不必要的开销
//onResume中添加开启
switcherView.restartSwitcherRepeat();
//onPause中添加暂停
switcherView.stopSwitcherRepeat();
(需要源码,可以留言邮箱)