circular auto scrolling horizontalscrollview android

点点圈 提交于 2019-11-29 02:26:37
nvinayshetty

I have Achieved a similar requirement by using an RecyclerView with horizontal linear layout manager here are the code snippets which may help you

  1. To make the Recycler View appear horizontal use a LinearLayout manager and set its orientation to horizontal

    mLinearLayoutManager = new LinearLayoutManager(context);
    mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecyclerView.setLayoutManager(mLinearLayoutManager);
    
  2. To make It circular: In your Recycler View adapter make the getItemCount to return a very large value like Integer.MAX_VALUE.

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

To get the actual item inside your adapter you can use simple modulus operation like this

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

Here is the trick to makes it circular!

Make your RecyclerView to scroll to an index which is half of the value returned by your adapters getItemCount,You can achieve this by the below code:

mLinearLayoutManager.scrollToPosition(Integer.MAX_VALUE / 2);

(this is a simple hack)

I didn't understand what exactly you meant by auto Scrolling ,if you want the items to scroll to center of the screen on clicking it here is the code

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

Hope this helps you!

Here you are I have made a recyclerview with auto scrolling

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 Is the time for scrolling
  • onScrollStateChanged is used to reset timer when user scroll.
  • I use smoothScrollToPosition for all transitions except when going from first to last.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!