Android實現(xiàn)無限循環(huán)滾動
傳統(tǒng)的ViewPager做循環(huán)滾動有兩種思路。
一種是設(shè)置count為Integer.MAX,然后根據(jù)index對實際數(shù)量取模
一種是在開頭在開頭添加end,在末尾添加start。簡單的說就是多兩個,滑動到這兩個的時候直接setCurrentItem到真正的位置。
在觀察pdd的拼單的循環(huán)滾動的時候,想到幾種實現(xiàn)方式。
1、通過Recyclerview,同樣跟ViewPager做循環(huán)滾動的思路類似,多一點要攔截掉所有的觸摸事件。但是這種方式的話無法像pdd的效果那樣設(shè)置進(jìn)入和出去的動畫。
2、通過改造VerticalViewpager的形式,應(yīng)該也是可以的,但是感覺比較麻煩。
3、通過自定義的方式實現(xiàn)。(原本以為挺簡單的,實現(xiàn)了下,代碼不多但是有些小細(xì)節(jié)需要注意下。)
我選擇了自定義的這里只是一個demo,提供一種思路。
最核心的就是上面的item滑出屏幕的時候?qū)⑺黵emove掉然后再加到自定義的ViewGroup的末尾。
public class LoopView extends ViewGroup {
private static final String TAG = "LoopView";
private float dis;
private ObjectAnimator animator;
private int currentIndex = 0;
private Handler handler = new Handler(Looper.getMainLooper());
public LoopView(Context context) {
super(context);
init();
}
public LoopView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LoopView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
void init() {
View view1 = new View(getContext());
view1.setTag("gray");
view1.setBackgroundColor(Color.GRAY);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 200);
addView(view1, layoutParams);
View view2 = new View(getContext());
view2.setTag("red");
view2.setBackgroundColor(Color.RED);
LayoutParams layoutParams1 = new LayoutParams(LayoutParams.MATCH_PARENT, 200);
addView(view2, layoutParams1);
View view3 = new View(getContext());
view3.setTag("green");
view3.setBackgroundColor(Color.GREEN);
LayoutParams layoutParams2 = new LayoutParams(LayoutParams.MATCH_PARENT, 200);
addView(view3, layoutParams2);
animator = ObjectAnimator.ofFloat(this, "dis", 0, 1);
animator.setDuration(2000);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
currentIndex++;
View first = getChildAt(0);
removeView(first);
addView(first);
handler.postDelayed(new Runnable() {
@Override
public void run() {
animator.clone().start();
}
}, 3000);
}
});
}
public void start() {
animator.start();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildren(widthMeasureSpec, heightMeasureSpec);
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(200, MeasureSpec.EXACTLY));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
int top = currentIndex * getMeasuredHeight();
for (int i = 0; i < childCount; i++) {
View childAt = getChildAt(i);
childAt.layout(l, top, r, top + childAt.getMeasuredHeight());
top += childAt.getMeasuredHeight();
}
}
public float getDis() {
return dis;
}
public void setDis(float dis) {
this.dis = dis;
float disY = dis * getHeight();
scrollTo(0, (int) (currentIndex * getHeight() + disY));
}
}
需要注意的就是onLayout的時候?qū)τ趖op的取值。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ListView實現(xiàn)無限循環(huán)滾動
- Android可自定義垂直循環(huán)滾動布局
- Android ViewPager無限循環(huán)滑動并可自動滾動完整實例
- android水平循環(huán)滾動控件使用詳解
- Android使用Recyclerview實現(xiàn)圖片水平自動循環(huán)滾動效果
- Android ViewPager實現(xiàn)智能無限循環(huán)滾動回繞效果
- Android自定義可循環(huán)的滾動選擇器CycleWheelView
- Android高仿京東垂直循環(huán)滾動新聞欄
- Android 使用ViewPager自動滾動循環(huán)輪播效果
相關(guān)文章
Android邊播放邊緩存視頻框架AndroidVideoCache詳解
這篇文章主要為大家介紹了Android邊播放邊緩存視頻框架AndroidVideoCache詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Adapter模式實戰(zhàn)之重構(gòu)鴻洋集團(tuán)的Android圓形菜單建行
這篇文章主要介紹了Adapter模式實戰(zhàn)之重構(gòu)鴻洋集團(tuán)的Android圓形菜單建行的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android開發(fā)之OkHttpUtils的具體使用方法
這篇文章主要介紹了Android開發(fā)之OkHttpUtils的具體使用方法,非常具有實用價值,需要的朋友可以參考下2017-08-08
跨平臺移動WEB應(yīng)用開發(fā)框架iMAG入門教程
這篇文章主要介紹了跨平臺移動WEB應(yīng)用開發(fā)框架iMAG入門教程,iMAG最大的特點是生成各移動平臺的原生代碼,需要的朋友可以參考下2014-07-07
Android WebView實現(xiàn)網(wǎng)頁滾動截圖
這篇文章主要為大家詳細(xì)介紹了Android WebView實現(xiàn)網(wǎng)頁滾動截圖,對整個網(wǎng)頁進(jìn)行截屏,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05

