Circular auto scroll horizontalscrollview android

I want to automatically scroll the horizontal scroll that contains a lot of images in linear mode when it reaches the last image, the first appears, etc.

I found this code that suits my needs, but when I added automatic scrolling, the circular function will not work: when the last image is reached, the first will not appear

here is my code:

slider = (RelativeLayout)findViewById(R.id.slider);
    RelativeLayout container = (RelativeLayout) findViewById(R.id.slider);
    scrollView = new PSInfiniteScrollView(this,new PSSize(120,120));
    for (int i = 0; i < 10; i++) {
        MyCloneableView img = new MyCloneableView(this);
        img.setId(i + 20);
        img.setImageResource(R.drawable.ic_launcher);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setBackgroundColor(c[i]);
        img.setTag(c[i]);
        scrollView.addItem(img);
    }

    container.addView(scrollView);

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            doLoop();
        }
    });

    t.start();

and here is the doLoop method:

private void doLoop(){
    do {
        scrollView.scrollBy(2, 0);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        scrollView.scrollBy(2, 0);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }   while (true);
}   
+4
source share
2 answers

, RecyclerView , .

  • Recycler View , LinearLayout

    mLinearLayoutManager = new LinearLayoutManager(context);
    mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecyclerView.setLayoutManager(mLinearLayoutManager);
    
  • : Recycler View make getItemCount , Integer.MAX_VALUE.

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }
    

, , ,

@Override
public void onBindViewHolder(ActionItemViewHolder holder, int position) {
    position = position % ACTUAL_SIZE_OF_LIST;
};

, !

RecyclerView , , getItemCount, :

mLinearLayoutManager.scrollToPosition(Integer.MAX_VALUE / 2);

( )

, , , ,

private void scrollToCenter(View v) {
    int itemToScroll = mRecyclerView.getChildPosition(v);
    int centerOfScreen = mRecyclerView.getWidth() / 2 - v.getWidth() / 2;
    mLayoutManager.scrollToPositionWithOffset(itemToScroll, centerOfScreen);
}

, !

+11

, recyclerview

public class AdsViewer extends FrameLayout {

private static final String TAG = "AdsViewer";

private final long SWITCH_TIME = 5000;
@BindView(R.id.ads_recycler_view)
RecyclerView adsRecyclerView;

private AdsAdapter adsAdapter;

private int currentItem = 0;

private Handler switchHandler = new Handler();
private Runnable switchRunnable = new Runnable() {
    @Override
    public void run() {
        showNextAd();
    }
};

public AdsViewer(@NonNull Context context) {
    super(context);
    init();
}

public AdsViewer(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    inflate(getContext(), R.layout.ads_viewer, this);
    ButterKnife.bind(this);
    initRecyclerView();
}

private void initRecyclerView() {
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    adsRecyclerView.setLayoutManager(linearLayoutManager);

    adsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            currentItem = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
            resetTimer();
        }
    });

    // add pager behavior, Add this to have paging scrolling
    //PagerSnapHelper snapHelper = new PagerSnapHelper();
    //snapHelper.attachToRecyclerView(adsRecyclerView);
}

private void resetTimer() {
    switchHandler.removeCallbacks(switchRunnable);
    switchHandler.postDelayed(switchRunnable, SWITCH_TIME);
}

public void clear() {
    if (adsAdapter != null) {
        adsAdapter.clear();
    }
}

public void setAds(List<Ads> ads) {
    if (adsAdapter == null || ads.size() != adsAdapter.getItemCount()) {
        initializeAdapter();
        adsAdapter.addItems(ads);
        resetTimer();
    }
}

private void initializeAdapter() {
    adsAdapter = new AdsAdapter(LayoutInflater.from(getContext()));
    adsRecyclerView.setAdapter(adsAdapter);
}

private void showNextAd() {
    int adsCount = adsAdapter.getItemCount();
    if (adsCount > 1) {
        if (currentItem == (adsCount - 1)) {
            currentItem = 0;
            adsRecyclerView.scrollToPosition(currentItem);
            resetTimer();
        } else {
            currentItem++;
            adsRecyclerView.smoothScrollToPosition(currentItem);
        }
    }
}
}
  • SWITCH_TIME
  • onScrollStateChanged reset .
  • smoothScrollToPosition , .
0

All Articles