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);
}
I have Achieved a similar requirement by using an RecyclerView with horizontal linear layout manager here are the code snippets which may help you
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);
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With