Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to create carousel viewpager2 or infinite loop/endless scroll in android viewpager2
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:srcCompat="@drawable/desktop46"
android:contentDescription="Welcome"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
ViewPager2 viewPager2;
private Handler headerHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager2 = findViewById(R.id.view_pager);
viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
List<ViewPagerItem> list = new ArrayList<>();
/* for looping*/
/*list.add(new ViewPagerItem(R.drawable.desktop50));
list.add(new ViewPagerItem(R.drawable.desktop52));
list.add(new ViewPagerItem(R.drawable.desktop75));*/
list.add(new ViewPagerItem(R.drawable.desktop46));
list.add(new ViewPagerItem(R.drawable.desktop31));
list.add(new ViewPagerItem(R.drawable.desktop30));
list.add(new ViewPagerItem(R.drawable.desktop33));
list.add(new ViewPagerItem(R.drawable.desktop34));
list.add(new ViewPagerItem(R.drawable.desktop35));
list.add(new ViewPagerItem(R.drawable.desktop10));
list.add(new ViewPagerItem(R.drawable.desktop50));
list.add(new ViewPagerItem(R.drawable.desktop52));
list.add(new ViewPagerItem(R.drawable.desktop75));
/*for looping*/
list.add(new ViewPagerItem(R.drawable.desktop46));
list.add(new ViewPagerItem(R.drawable.desktop31));
list.add(new ViewPagerItem(R.drawable.desktop30));
viewPager2.setAdapter(new ViewPagerAdapter(this, list, viewPager2));
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
headerHandler.removeCallbacks(headerRunnable);
headerHandler.postDelayed(headerRunnable, 3000); // Slide duration 3 seconds
}
});
}
private Runnable headerRunnable = new Runnable() {
@Override
public void run() {
viewPager2.setCurrentItem(viewPager2.getCurrentItem() + 1, true);
}
};
@Override
protected void onPause() {
super.onPause();
headerHandler.removeCallbacks(headerRunnable);
}
@Override
protected void onResume() {
super.onResume();
headerHandler.postDelayed(headerRunnable, 3000); // Slide duration 3 seconds
}
}
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewHolder> {
private List<ViewPagerItem> mData;
private ViewPager2 viewPager2;
ViewPagerAdapter(MainActivity mainActivity, List<ViewPagerItem> mData, ViewPager2 viewPager2) {
this.mData = mData;
this.viewPager2 = viewPager2;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view_pager, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.setHeader(mData.get(position));
if (position == mData.size() - 2) {
viewPager2.post(runnable);
} /*else if (position == 2) {
viewPager2.post(runnable2);
}*/
}
@Override
public int getItemCount() {
return mData.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageHeader;
ViewHolder(@NonNull View itemView) {
super(itemView);
imageHeader = itemView.findViewById(R.id.image);
}
void setHeader(ViewPagerItem ViewPagerItem) {
imageHeader.setImageResource(ViewPagerItem.getImage());
}
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
viewPager2.setCurrentItem(0, true);
//mData.addAll(mData);
//notifyDataSetChanged();
}
};
/*private Runnable runnable2 = new Runnable() {
@Override
public void run() {
viewPager2.setCurrentItem(mData.size() - 3, true);
//mData.addAll(mData);
//notifyDataSetChanged();
}
};*/
}
package com.bcod.viewpagersample2;
public class ViewPagerItem {
private int image;
ViewPagerItem(int image) {
this.image = image;
}
public int getImage() {
return image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment