大神github地址:https://github.com/sfsheng0322/MarqueeView
https://github.com/dreamlivemeng/UpMarqueeTextView-master
代碼
/**
*?仿淘寶首頁的?淘寶頭條滾動(dòng)的自定義View
*
*?Created?by?dreamlive?on?2016/7/20.
*/
public?class?UPMarqueeView?extends?ViewFlipper?{
private?Context?mContext;
private?booleanisSetAnimDuration=false;
private?intinterval=2000;
/**
*?動(dòng)畫時(shí)間
*/
private?intanimDuration=500;
public?UPMarqueeView(Context?context,?AttributeSet?attrs)?{
super(context,?attrs);
init(context,?attrs,?0);
}
private?void?init(Context?context,?AttributeSet?attrs,?int?defStyleAttr)?{
this.mContext=context;
setFlipInterval(interval);
AnimationanimIn=AnimationUtils.loadAnimation(mContext,?R.anim.anim_marquee_in);
if?(isSetAnimDuration)?animIn.setDuration(animDuration);
setInAnimation(animIn);
AnimationanimOut=AnimationUtils.loadAnimation(mContext,?R.anim.anim_marquee_out);
if?(isSetAnimDuration)?animOut.setDuration(animDuration);
setOutAnimation(animOut);
}
/**
*?設(shè)置循環(huán)滾動(dòng)的View數(shù)組
*
*?@param?views
*/
public?void?setViews(Listviews)?{
if?(views==?null?||?views.size()?==?0)?return;
removeAllViews();
for?(inti=0;?i
addView(views.get(i));
}
startFlipping();
}
}
MainActivity的內(nèi)容:
/**
*?仿淘寶首頁的?淘寶頭條滾動(dòng)的自定義View
*?Created?by?dreamlive?on?2016/7/20.
*/
public?class?MainActivity?extends?AppCompatActivity?{
private?UPMarqueeView?upview1;
Listdata=newArrayList<>();
Listviews=newArrayList<>();
@Override
protected?void?onCreate(Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initParam();
initdata();
initView();
}
/**
*?實(shí)例化控件
*/
private?void?initParam()?{
upview1=?(UPMarqueeView)?findViewById(R.id.upview1);
}
/**
*?初始化界面程序
*/
private?void?initView()?{
setView();
upview1.setViews(views);
}
/**
*?初始化需要循環(huán)的View
*?為了靈活的使用滾動(dòng)的View虐唠,所以把滾動(dòng)的內(nèi)容讓用戶自定義
*?假如滾動(dòng)的是三條或者一條关贵,或者是其他,只需要把對(duì)應(yīng)的布局,和這個(gè)方法稍微改改就可以了饲趋,
*/
private?void?setView()?{
for?(inti=0;?i
//設(shè)置滾動(dòng)的單個(gè)布局
LinearLayoutmoreView=?(LinearLayout)?LayoutInflater.from(this).inflate(R.layout.item_view,?null);
//初始化布局的控件
TextViewtv1=?(TextView)?moreView.findViewById(R.id.tv1);
TextViewtv2=?(TextView)?moreView.findViewById(R.id.tv2);
//進(jìn)行對(duì)控件賦值
tv1.setText(data.get(i).toString());
if?(data.size()>i?+?1)?{
//因?yàn)樘詫毮莾菏莾蓷l數(shù)據(jù),但是當(dāng)數(shù)據(jù)是奇數(shù)時(shí)就不需要賦值第二個(gè)驱还,所以加了一個(gè)判斷膊毁,還應(yīng)該把第二個(gè)布局給隱藏掉
tv2.setText(data.get(i?+?1).toString());
}else?{
moreView.findViewById(R.id.rl2).setVisibility(View.GONE);
}
//添加到循環(huán)滾動(dòng)數(shù)組里面去
views.add(moreView);
}
}
/**
*?初始化數(shù)據(jù)
*/
private?void?initdata()?{
data=newArrayList<>();
data.add("家人給2歲孩子喝這個(gè),孩子智力倒退10歲!!!");
data.add("iPhone8最感人變化成真绰更,必須買買買買!!!!");
data.add("簡直是白菜價(jià)瞧挤!日本玩家33萬甩賣15萬張游戲王卡");
data.add("iPhone7價(jià)格曝光了!看完感覺我的腰子有點(diǎn)疼...");
data.add("主人內(nèi)疚逃命時(shí)沒帶夠儡湾,回廢墟狂挖30小時(shí)特恬!");
//????????data.add("竟不是小米樂視!看水搶了驍龍821首發(fā)了P炷啤0┕簟!");
}
}