circular auto scrolling horizontalscrollview andro

2020-02-09 12:23发布

问题:

I want to auto scroll an horizontal scrollview which contains a lot of imageview inside a linearlayout when it reached the last imageview the first one appear and so on

I have found this code which have match my needs but when I have added the auto scrolling the circular feature won't work : when the last imageview is reached the first one won't 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 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);
}   

回答1:

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!



回答2:

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.