最近项目中有滚动新闻的需求,和淘宝、京东热点那一样的,于是自己写了一个自定义控件(根据控件高度计算Y值不断绘制Textview),本来挺完美的。各种封装、调用,但版本发布后被各种反馈淹没(机型适配)。有的机型只重复显示一跳信息,有的带“探戈” 效果的显示。T_T 坑,百般修复都不能满足各种机型...
后来,> http://blog.youkuaiyun.com/qq_30379689/article/details/54174838
中获知安卓已经自带了一个此类控件–ViewFlipper。T_T
赶紧的又做了一次封装(此处的需求是,左侧图片不动,只右侧文字上下滚动),代码如下
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlt_vsv_root"
android:layout_width="match_parent"
android:layout_height="@dimen/y81"
android:background="@color/layout_background_f8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/y80"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="@dimen/y26"
android:paddingRight="@dimen/y26">
<ImageView
android:id="@+id/iv_vsv_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/x12"
android:src="@mipmap/icon_scroll_news" />
<ViewFlipper
android:id="@+id/vf_vsv_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoStart="true"
android:flipInterval="2000"
android:inAnimation="@anim/scroll_tv_enter_from_bottom"
android:outAnimation="@anim/scroll_tv_leave_from_bottom" />
</LinearLayout>
<View
android:id="@+id/line_vsv"
android:layout_width="match_parent"
android:layout_height="@dimen/y1"
android:layout_alignParentBottom="true"
android:background="@color/color_dddddd" />
</RelativeLayout>
public class CustomScroolNewsView extends RelativeLayout {
@BindView(R.id.iv_vsv_pic)
ImageView ivPic;
@BindView(R.id.vf_vsv_txt)
ViewFlipper vfTxt;
@BindView(R.id.line_vsv)
View lineVsv;
@BindView(R.id.rlt_vsv_root)
RelativeLayout rltRoot;
public Context mActivity;
private View rootView;
private NewsViewClickListener mListener;
private int mTxtColor = Color.rgb(102, 102, 102);
public CustomScroolNewsView(Context context) {
super(context);
this.mActivity = context;
initView();
}
public CustomScroolNewsView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mActivity = context;
initView();
}
private void initView() {
rootView = LayoutInflater.from(mActivity).inflate(R.layout.view_scrool_view, this, true);
ButterKnife.bind(this, rootView);
ivPic.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
public void setNewsViewClickListener(NewsViewClickListener listenr) {
this.mListener = listenr;
}
/**
* 设置数据源
*
* @param data
*/
public void setResourceData(List<SliderItem> data) {
for (int i = 0; i < data.size(); i++) {
final SliderItem sliderItem = data.get(i);
TextView textView = new TextView(mActivity);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
textView.setText(sliderItem.title);
textView.setTextColor(mTxtColor);
textView.setTextSize(13);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener == null) return;
mListener.onClickNewsView(sliderItem);
}
});
vfTxt.addView(textView);
}
}
/**
* 设置view切换的时间间隔
*
* @param duration
*/
public void setDuration(int duration) {
vfTxt.setFlipInterval(duration);
}
/**
* 设置文字颜色
*
* @param color
*/
public void setTxtColor(int color) {
this.mTxtColor = color;
}
/**
* 设置左侧图片
*
* @param picId
*/
public void setLeftPic(int picId) {
ivPic.setImageResource(picId);
}
public void setBgColor(int colorId) {
rltRoot.setBackgroundColor(colorId);
lineVsv.setVisibility(GONE);
}
public interface NewsViewClickListener {
void onClickNewsView(SliderItem item);
}
}