<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xiao.noticeview.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="200dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:text="京东"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="快报"
android:background="@drawable/bg_kuaibao"
android:textColor="#ffffffff"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#989898"
android:text=" : "/>
<com.xiao.noticeview.widget.NoticeView
android:id="@+id/notice_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="18dp"
android:layout_marginRight="8dp"
android:alpha="0.3"
android:background="#666666"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="56dp"
android:gravity="center"
android:text="更多"
android:textColor="#666666"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
anim的代码
notify_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--平移-->
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="50%p"
android:toYDelta="0"/>
<!--渐变-->
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
</set>
notify_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--平移-->
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="0"
android:toYDelta="-50%p"/>
<!--渐变-->
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
</set>
//自定义控件
/**
* Created by xiaoyanger on 2017/1/14.
* 轮播公告Veiw
*/
public class NoticeView extends ViewFlipper implements View.OnClickListener {
private Context mContext;
private List<String> mNotices;
public NoticeView(Context context) {
super(context);
}
public NoticeView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mContext = context;
// 轮播间隔时间为3s
setFlipInterval(3000);
// 内边距5dp
setPadding(dp2px(5f), dp2px(5f), dp2px(5f), dp2px(5f));
// 设置enter和leave动画
setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.notify_in));
setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.notify_out));
}
/**
* 添加需要轮播展示的公告
*
* @param notices
*/
public void addNotice(List<String> notices) {
removeAllViews();
mNotices = notices;
for (int i = 0; i < notices.size(); i++) {
// 根据公告内容构建一个Tetview
String notice = notices.get(i);
TextView textView = new TextView(mContext);
textView.setSingleLine();
textView.setText(notice);
textView.setTextSize(13f);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setTextColor(Color.parseColor("#666666"));
textView.setGravity(Gravity.CENTER_VERTICAL);
// 将公告的位置设置为textView的tag方便点击是回调给用户
textView.setTag(i);
textView.setOnClickListener(this);
// 添加到ViewFlipper
NoticeView.this.addView(textView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
}
@Override
public void onClick(View v) {
int position = (int) v.getTag();
String notice = mNotices.get(position);
if (mOnNoticeClickListener != null) {
mOnNoticeClickListener.onNotieClick(position, notice);
}
}
/**
* 通知点击监听接口
*/
public interface OnNoticeClickListener {
void onNotieClick(int position, String notice);
}
private OnNoticeClickListener mOnNoticeClickListener;
/**
* 设置通知点击监听器
*
* @param onNoticeClickListener 通知点击监听器
*/
public void setOnNoticeClickListener(OnNoticeClickListener onNoticeClickListener) {
mOnNoticeClickListener = onNoticeClickListener;
}
private int dp2px(float dpValue) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpValue,
mContext.getResources().getDisplayMetrics());
}
}
MainActivity 的代码
public class MainActivity extends AppCompatActivity {
private List<String> notices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
NoticeView noticeView = (NoticeView) findViewById(R.id.notice_view);
notices = new ArrayList<>();
notices.add("大促销下单拆福袋,亿万新年红包随便拿");
notices.add("家电五折团,抢十亿无门槛现金红包");
notices.add("星球大战剃须刀首发送200元代金券");
noticeView.addNotice(notices);
noticeView.startFlipping();
noticeView.setOnNoticeClickListener(new NoticeView.OnNoticeClickListener() {
@Override
public void onNotieClick(int position, String notice) {
Toast.makeText(MainActivity.this, notices.get(position),Toast.LENGTH_SHORT).show();
}
});
}
}