思路:利用handler的post方法传入runnable达到循环滚动, handler.removeCallbacks(runnable);停止滚动,核心代码都在runnable中创创建,使用属性动画实现翻滚
public class Myshangxiagun extends LinearLayout {
private TextView mBannerTV1;
private TextView mBannerTV2;
private Handler handler;
private boolean isShow;
private int startY1, endY1, startY2, endY2;
private Runnable runnable;
private List<HomeRedian> list;
private int position = 0;
private int offsetY = 100;
Context context;
public Myshangxiagun(Context context) {
this(context, null);
}
public Myshangxiagun(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Myshangxiagun(final Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = LayoutInflater.from(context).inflate(R.layout.home_shangxiagun, this);
mBannerTV1 = (TextView) view.findViewById(R.id.tv_banner1);
mBannerTV2 = (TextView) view.findViewById(R.id.tv_banner2);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
isShow = !isShow;
if (position == list.size()) //达到循环效果
position = 0;
if (isShow) {
final int i = position++;
mBannerTV1.setText(list.get(i).getTitle());
mBannerTV1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(context, Zhixuninfo.class);
it.putExtra("position", list.get(i).getAid());
it.putExtra("title", list.get(i).getTitle());
it.putExtra("uid", list.get(i).getUid());
it.putExtra("img", list.get(i).getUrl());
context.startActivity(it);
}
});
} else {
final int i = position++;
mBannerTV2.setText(list.get(i).getTitle());
mBannerTV2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(context, Zhixuninfo.class);
it.putExtra("position", list.get(i).getAid());
it.putExtra("title", list.get(i).getTitle());
it.putExtra("uid", list.get(i).getUid());
it.putExtra("img", list.get(i).getUrl());
context.startActivity(it);
}
});
}
/**利用属性动画实现上下翻滚,*/
startY1 = isShow ? 0 : offsetY;
endY1 = isShow ? -offsetY : 0;
ObjectAnimator.ofFloat(mBannerTV1, "translationY", startY1, endY1).setDuration(300).start();
startY2 = isShow ? offsetY : 0;
endY2 = isShow ? 0 : -offsetY;
ObjectAnimator.ofFloat(mBannerTV2, "translationY", startY2, endY2).setDuration(300).start();
handler.postDelayed(runnable, 3000);
}
};
}
//设置数据
public List<HomeRedian> getList() {
return list;
}
//设置数据
public void setList(List<HomeRedian> list) {
this.list = list;
}
//开始滚动
public void startScroll() {
handler.post(runnable);
}
//暂停滚动
public void stopScroll() {
handler.removeCallbacks(runnable);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/touziredian_pic"
android:text="公告"
android:textColor="#e76712"
android:textSize="16sp" />
<View
android:id="@+id/view_divider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toRightOf="@id/tv_title"
android:background="#DDDDDD" />
<TextView
android:textStyle="bold"
android:maxLines="2"
android:ellipsize="end"
android:id="@+id/tv_banner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/view_divider"
android:singleLine="true"
android:text="没有网络"
android:textColor="#333333"
android:textSize="13sp" />
<TextView
android:textStyle="bold"
android:maxLines="2"
android:ellipsize="end"
android:id="@+id/tv_banner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/view_divider"
android:singleLine="true"
android:textColor="#333333"
android:textSize="13sp" />
</RelativeLayout>