Как я могу дать ViewPager функцию, которая никогда не закончится и будет бесконечно прокручиваться?
xml фрагмент_дома:
<RelativeLayout
android:id="@+id/page_home_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
android:background="#fff"
tools:context=".Fragment.Home.HomeFragment">
<androidx.cardview.widget.CardView
android:id="@+id/card_viewpager"
android:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardCornerRadius="5dp">
<ir.develop.mhp.Action.CustomViewPager.ViewPagerCustomDuration
android:layout_width="match_parent"
android:layout_height="235dp"
android:id="@+id/slider_viewpager"
/>
</androidx.cardview.widget.CardView>
<com.tbuonomo.viewpagerdotsindicator.DotsIndicator
android:id="@+id/indicator"
android:layout_below="@id/card_viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:dotsColor="#C5C5C5"
app:dotsCornerRadius="8dp"
android:layout_margin="5dp"
app:dotsSize="7dp"
app:dotsSpacing="4dp"
android:layout_centerHorizontal="true"
android:layoutDirection="ltr"
app:dotsWidthFactor="2.5"
app:selectedDotColor="@color/colorPrimary"
app:progressMode="false"
/>
</RelativeLayout>
xml item_image_slider:
<ImageView android:layout_width="match_parent"
android:layout_height="235dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/image_slider"
android:layout_margin="10dp"
android:scaleType="fitXY"
android:src="@color/colorPrimary"
xmlns:android="http://schemas.android.com/apk/res/android">
java HomeFragment:
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, null);
cast(view);
setupSlider();
setupRecyclerView();
viewPager.setScrollDurationFactor(3);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == viewPager.SCROLL_STATE_IDLE){
userScrolled = false;
}else {
userScrolled = true;
}
}
});
viewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
@Override
public void transformPage(@NonNull View page, float position) {
}
});
return view;
}
private void cast(View view) {
viewPager = view.findViewById(R.id.slider_viewpager);
indicator = view.findViewById(R.id.indicator);
}
private void setupSlider() {
url = new ArrayList<>();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Config.host + "getSlide.php", null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("slide");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo = jsonArray.getJSONObject(i);
url.add(jo.getString("slide"));
}
sliderAdapter = new SliderAdapter(getActivity(), url);
viewPager.setAdapter(sliderAdapter);
startAutoSlider(jsonArray.length());
indicator.setViewPager(viewPager);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
CustomToast.toastError(getActivity());
}
});
RequestVolley.get(getActivity()).add(jsonObjectRequest);
}
private void startAutoSlider(final int len) {
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (!userScrolled) {
if (viewPager.getCurrentItem() == len - 1) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
}
}
};
timer = new Timer(); // This will create a new Thread
timer.schedule(new TimerTask() { // task to be scheduled
@Override
public void run() {
handler.post(Update);
}
}, DELAY_MS, PERIOD_MS);
}
java SliderAdapter:
public class SliderAdapter extends PagerAdapter {
private Context context;
private List<String> url;
public SliderAdapter(Context context, List<String> url) {
this.context = context;
this.url = url;
}
@Override
public int getCount() {
return url.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View view = LayoutInflater.from(context).inflate(R.layout.item_image_slider , container , false);
ImageView imageView = view.findViewById(R.id.image_slider);
LoadImage.load(context , url.get(position) , imageView);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
Очень важно не использовать библиотеки.