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開發(fā)環(huán)境原始版(windows版)
安裝android開發(fā)環(huán)境原始版(windows版)的詳細(xì)步驟2013-03-03
Android Broadcast原理分析之registerReceiver詳解
這篇文章主要介紹了Android Broadcast原理分析之registerReceiver詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android實現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來的方法
這篇文章主要介紹了Android實現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來的方法,涉及Android針對SD卡的讀取及文件遍歷等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

