//源码
public class BulletinView extends LinearLayout {
private boolean isStop = true;//是否停止
private View view1, view2; //上下切换的两个view,view2在上面,view1在下面
private LayoutInflater inflater;
private BulletinViewadapter adapter;//适配器
private int viewType = 0;//当前显示的条目下标
private LinearLayout linearLayout;// 条目容器
private OnItemClickListener onItemClickListener;//条目点击事件
private TranslateAnimation mTranslateAnimationStart, mTranslateAnimationOut;//条目切换动画
private boolean initData = false;//是否初始化
public boolean isInitData() {
return initData;
}
public BulletinView(Context context) {
super(context);
}
public BulletinView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
inflater = LayoutInflater.from(getContext());
linearLayout = new LinearLayout(getContext());
}
public BulletinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
linearLayout = new LinearLayout(getContext());
inflater = LayoutInflater.from(getContext());
}
public void init() {
if (view1 == null) {
removeAllViews();
addView(linearLayout);
}
if (viewType >= adapter.getItemCount()) {
viewType = 0;
}
int viewType2 = getPson(viewType + 1);
linearLayout.removeAllViews();
view1 = adapter.onBindViewHolder(adapter.onCreateViewHolder(inflater, linearLayout, viewType, adapter.getType(viewType)),
viewType, adapter.getData().get(viewType), adapter.getType(viewType));
view2 = adapter.onBindViewHolder(adapter.onCreateViewHolder(inflater, linearLayout, viewType2, adapter.getType(viewType)),
viewType2, adapter.getData().get(viewType2), adapter.getType(viewType));
view1.setLayoutParams(getLayoutParams());
view2.setLayoutParams(getLayoutParams());
// 滚动方向为从上往下时打开
// linearLayout.addView(view2);
// linearLayout.addView(view1);
//滚动方向从下往上
linearLayout.addView(view1);
linearLayout.addView(view2);
setAnimate(view1);
setAnimate(view2);
view1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null)
onItemClickListener.onItemClickListener(adapter.getData().get(viewType), viewType, v);
}
});
view2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null)
onItemClickListener.onItemClickListener(adapter.getData().get(viewType), viewType, v);
}
});
}
public int getPson(int type) {
if (type == adapter.getItemCount()) {
return 0;
}
return type;
}
private Animation.AnimationListener animationListener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// linearLayout.removeView(view2);
view1 = view2;//将当前可见布局赋值给view1;表示下次移除的布局
}
@Override
public void onAnimationRepeat(Animation animation) {
}
};
/**
* 属性动画
* 平移
* 从上往下
*/
// private TranslateAnimation setAnimate(View view) {
imageView中凡是有get,set方法的属性,都可以通过属性动画操作
创建属性动画对象,并设置移动的方向和偏移量
translationX是imageview的平移属性
// TranslateAnimation objectAnimator = new TranslateAnimation(0, 0, -(float) getHeight(), 0);
设置移动时间
// objectAnimator.setDuration(500);
开始动画
// objectAnimator.setFillAfter(true);
// view.setAnimation(objectAnimator);
// objectAnimator.start();
//
// return objectAnimator;
// }
/**
* 属性动画
* 平移
* 从下往上
*/
private TranslateAnimation setAnimate(View view) {
// imageView中凡是有get,set方法的属性,都可以通过属性动画操作
// 创建属性动画对象,并设置移动的方向和偏移量
// translationX是imageview的平移属性
TranslateAnimation objectAnimator = new TranslateAnimation(0, 0, 0, -(float) getHeight());
// 设置移动时间
objectAnimator.setDuration(500);
// 开始动画
objectAnimator.setFillAfter(true);
view.setAnimation(objectAnimator);
objectAnimator.start();
return objectAnimator;
}
private Handler mHandler = new Handler();
private Runnable r = new Runnable() {//循环滚动
@Override
public void run() {
//do something
//每隔1s循环执行run方法
if (!isStop) {
init();
viewType++;
mHandler.postDelayed(this, 2000);
} else {
mHandler.removeCallbacks(r);
}
}
};
public void setAdapter(BulletinViewadapter adapter) {//设置适配器
if (adapter != null) {
linearLayout.setOrientation(LinearLayout.VERTICAL);
this.adapter = adapter;
isStop = false;
init();
if (!initData) {
mHandler.postDelayed(r, 2000);
}
initData = true;
}
}
public void onStop() {//停止滚动
isStop = true;
mHandler.removeCallbacks(r);
}
public void onStart() {//开始滚动
isStop = false;
if (adapter != null)
mHandler.postDelayed(r, 500);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {//条目点击事件
if (onItemClickListener != null) {
this.onItemClickListener = onItemClickListener;
}
}
public static abstract class BulletinViewadapter<T> {//适配器抽象类
private List<T> data;
public BulletinViewadapter(List<T> data) {
this.data = data;
}
public List<T> getData() {
return data;
}
public abstract int getItemCount();//总条数
public abstract View onBindViewHolder(View itemView, int position, T itemData, int type);//布局绑定参数
protected abstract View onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int position, int type);//创建布局
protected int getType(int position) {
return position;
}
}
public interface OnItemClickListener<T> {
void onItemClickListener(T itemData, int pointer, View view);
}
// //设置开始动画,可不设置
// public void setmTranslateAnimation(TranslateAnimation mTranslateAnimationStart
// , TranslateAnimation mTranslateAnimationOut) {
// this.mTranslateAnimationStart = mTranslateAnimationStart;
// this.mTranslateAnimationOut = mTranslateAnimationOut;
// }
}
创建 滚动适配器
public class HomeAdapter extends BulletinView.BulletinViewadapter<String> {
public HomeAdapter(List<String> data) {
super(data);
}
@Override
public int getItemCount() {
return getData().size();
}
@Override
public View onBindViewHolder(View itemView, int position, String itemData,int viewType) {// 绑定数据
TextView textView = itemView.findViewById(R.id.tv_line);
textView.setText(itemData);
return itemView;
}
@Override
public View onCreateViewHolder(LayoutInflater inflater, ViewGroup parent,int position, int viewType) {//绑定布局 根据返回的viewType 设置不同布局
View view;
if (viewType!=0){
view =inflater.inflate(R.layout.lines, parent,false);
}else {
view =inflater.inflate(R.layout.lines_two, parent,false);
}
return view;
}
@Override
protected int getType(int viewType) {//布局类型
return viewType%2; //用来区分绑定不同的布局
}
}
//使用方法
bulletinView=findViewById(R.id.buttonPanel);
List<String> list1 = new ArrayList<>();
for (int i = 0; i < 100000; i++) {
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("4");
list1.add("5");
list1.add("6");
}
adapter=new HomeAdapter(list1);
bulletinView.setAdapter(adapter);
bulletinView.setOnItemClickListener(new BulletinView.OnItemClickListener<String>() {
@Override
public void onItemClickListener(String itemData, int pointer, View view) {
Toast.makeText(MainActivity.this,itemData,Toast.LENGTH_LONG).show();
}
});
demo github地址 https://github.com/IHoveYou/bulletinview 好用请点赞哈