1.自定义View
MarqueeTextView
package com.ruidde.jz.utils; import android.content.Context; import android.text.TextUtils; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.ViewFlipper; import com.ruidde.jz.R; import com.ruidde.jz.model.entity.Notice; import java.util.List; public class MarqueeTextView extends LinearLayout { private List<Notice> noticeList; private Context mContext; private ViewFlipper viewFlipper; private View marqueeTextView; public MarqueeTextView(Context context) { super(context); mContext = context; initBasicView(); } public MarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; initBasicView(); } public void setTextArraysAndClickListener(List<Notice> noticeList) {//1.设置数据源;2.设置监听回调(将textView点击事件传递到目标界面进行操作) this.noticeList = noticeList; initMarqueeTextView(noticeList); } public void initBasicView() {//加载布局,初始化ViewFlipper组件及效果 marqueeTextView = LayoutInflater.from(mContext).inflate(R.layout.marquee_textview_layout, null); LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); addView(marqueeTextView, layoutParams); viewFlipper = (ViewFlipper) marqueeTextView.findViewById(R.id.viewFlipper); viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_bottom));//设置上下的动画效果(自定义动画,所以改左右也很简单) viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top)); viewFlipper.startFlipping(); } public void initMarqueeTextView(List<Notice> noticeList) { if (noticeList.size() == 0) { return; } int i = 0; viewFlipper.removeAllViews(); for (int j = 0; j < noticeList.size(); j++) { Notice notice = noticeList.get(j); TextView textView = new TextView(mContext); textView.setSingleLine(true); textView.setEllipsize(TextUtils.TruncateAt.END); textView.setTextSize(14); textView.setTextColor(getResources().getColor(R.color.marqueecolor)); textView.setText(notice.getNoticeTitle()); textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { itemOnClickListener.onItemClick(notice); } }); LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); viewFlipper.addView(textView, lp); } } public void releaseResources() { if (marqueeTextView != null) { if (viewFlipper != null) { viewFlipper.stopFlipping(); viewFlipper.removeAllViews(); viewFlipper = null; } marqueeTextView = null; } } public ExamineClickLisitenner itemOnClickListener; public interface ExamineClickLisitenner { void onItemClick(Notice notice); } public void setItemOnClickListener(ExamineClickLisitenner itemOnClickListener) { this.itemOnClickListener = itemOnClickListener; } }
(1).其中layout:marquee_textview_layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/transparent" android:orientation="horizontal"> <ViewFlipper android:id="@+id/viewFlipper" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:flipInterval="3000"> </ViewFlipper> </LinearLayout>
(2).动画效果
slide_in_bottom
<?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> slide_out_top
<?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>
3.XML使用
<com.ruidde.jz.utils.MarqueeTextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="9dp" android:layout_marginEnd="2dp" android:layout_toEndOf="@+id/tv_gonggaoT" android:layout_toStartOf="@+id/tv_List"/>
4.activity中使用
lateinit var noticeList: List<Notice>
tv_content.setTextArraysAndClickListener(noticeList)
tv_content.setItemOnClickListener(object : MarqueeTextView.ExamineClickLisitenner { override fun onItemClick(notice: Notice) { TongZhiDetailsActivity.stattTongZhiDetailsActivity(mContext, notice) } })
手工……
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119193.html