I’m just trying to fetch 5 images (all less than 50 KB size) from server and then trying to load them into viewpager2 slide show at the top of fragment using glide and urls, also the images are coming and loading perfectly but after 5-7 seconds delay in the beginning. I’m unable to make out why such delay is happening even though I’m using faster network, can you please help me to fix this ?
My 2nd issue is – The Circular Indicator is also not showing/coming, though it appears perfectly when I try to load drawable images. But with glide + urls, Circular Indicator disappears below viewpager2 layout.
item_photo.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:padding="3dp"
app:cardCornerRadius="3dp">
<ImageView
android:id="@+id/img_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/img_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:background="@color/greenDark"
android:gravity="center"
android:padding="2dp"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.HomeFragment">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2_1"
android:layout_width="match_parent"
android:layout_height="250dp" />
<me.relex.circleindicator.CircleIndicator3
android:id="@+id/circleIndicator3_1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_margin="2dp"
app:ci_width="5dp"
app:ci_height="5dp"
app:ci_margin="5dp"
android:visibility="visible"
app:ci_orientation="horizontal"
app:ci_gravity="center"
app:ci_drawable="@drawable/bg_circle_indicator_3_selected"
app:ci_drawable_unselected="@drawable/bg_circle_indicator_3_unselected"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/home"
android:textColor="@color/orange"
android:textSize="30sp"
android:textStyle="bold" />
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never">
</androidx.core.widget.NestedScrollView>
</androidx.appcompat.widget.LinearLayoutCompat>
</FrameLayout>
Photo.java – (model used)
package com.project.localad;
public class Photo {
String img_url;
String img_txt;
public Photo(String img_url, String img_txt) {
this.img_url = img_url;
this.img_txt = img_txt;
}
public Photo() {
}
public String getImg_url() {
return img_url;
}
public void setImg_url(String img_url) {
this.img_url = img_url;
}
public String getImg_txt() {
return img_txt;
}
public void setImg_txt(String img_txt) {
this.img_txt = img_txt;
}
}
PhotoViewPager2Adapter.java
import java.util.List;
public class PhotoViewPager2Adapter extends RecyclerView.Adapter<PhotoViewPager2Adapter.PhotoViewHolder> {
private List<Photo> mListPhoto;
Context context;
public PhotoViewPager2Adapter(List<Photo> mListPhoto, Context context) {
this.mListPhoto = mListPhoto;
this.context = context;
}
@NonNull
@Override
public PhotoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo, parent, false);
return new PhotoViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PhotoViewHolder holder, int position) {
Glide.with(context).load(mListPhoto.get(position).getImg_url())
.disallowHardwareConfig()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.fitCenter()
.into(holder.img_photo);
holder.img_text.setText(mListPhoto.get(position).getImg_txt());
holder.img_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, mListPhoto.get(holder.getLayoutPosition()).getImg_txt(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mListPhoto.size();
}
public class PhotoViewHolder extends RecyclerView.ViewHolder {
private final ImageView img_photo;
private TextView img_text;
public PhotoViewHolder(@NonNull View itemView) {
super(itemView);
img_photo = itemView.findViewById(R.id.img_photo);
img_text = itemView.findViewById(R.id.img_text);
}
}
}
HomeFragment.java
public class HomeFragment extends Fragment {
private ViewPager2 mViewPager2;
private List<Photo> mListPhoto;
private FragmentHomeBinding binding;
SharedPreferences local_ad_shared_pref;
SharedPreferences.Editor editor;
String pin_code, img_url, img_txt, locationidpk;
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
if (mViewPager2.getCurrentItem() == mListPhoto.size() - 1) {
mViewPager2.setCurrentItem(0);
} else {
mViewPager2.setCurrentItem(mViewPager2.getCurrentItem() + 1);
}
}
};
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
local_ad_shared_pref = getActivity().getSharedPreferences("local_ad_shared_pref", Context.MODE_PRIVATE);
editor = local_ad_shared_pref.edit();
pin_code = local_ad_shared_pref.getString("pin_code", "");
// View Pager 2 Code
mViewPager2 = view.findViewById(R.id.viewPager2_1);
mListPhoto = getListPhoto(pin_code);
PhotoViewPager2Adapter adapter = new PhotoViewPager2Adapter(mListPhoto, getContext());
mViewPager2.setAdapter(adapter);
CircleIndicator3 indicator = view.findViewById(R.id.circleIndicator3_1);
indicator.setViewPager(mViewPager2);
adapter.registerAdapterDataObserver(indicator.getAdapterDataObserver());
mViewPager2.setOffscreenPageLimit(2);
mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
mHandler.removeCallbacks(mRunnable);
mHandler.postDelayed(mRunnable, 3000);
}
});
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private List<Photo> getListPhoto(String pin_code) {
List<Photo> list = new ArrayList<>();
Call<List<Photo>> call = apicontroller.getInstance()
.getApi()
.fetchSliderImages(pin_code);
call.enqueue(new Callback<List<Photo>>() {
@Override
public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
}
List<Photo> obj = response.body();
String url0 = "http://74.225.131.20/localshop/localad/localadhomesliderimages/" + obj.get(0).getImg_url();
String url1 = "http://74.225.131.20/localshop/localad/localadhomesliderimages/" + obj.get(1).getImg_url();
String url2 = "http://74.225.131.20/localshop/localad/localadhomesliderimages/" + obj.get(2).getImg_url();
String url3 = "http://74.225.131.20/localshop/localad/localadhomesliderimages/" + obj.get(3).getImg_url();
String url4 = "http://74.225.131.20/localshop/localad/localadhomesliderimages/" + obj.get(4).getImg_url();
list.add(new Photo(url0, obj.get(0).getImg_txt()));
list.add(new Photo(url1, obj.get(1).getImg_txt()));
list.add(new Photo(url2, obj.get(2).getImg_txt()));
list.add(new Photo(url3, obj.get(3).getImg_txt()));
list.add(new Photo(url4, obj.get(4).getImg_txt()));
}
}
@Override
public void onFailure(Call<List<Photo>> call, Throwable t) {
Log.d(t.getMessage());
}
});
return list;
}
@Override
public void onPause() {
super.onPause();
mHandler.removeCallbacks(mRunnable);
}
@Override
public void onResume() {
super.onResume();
mHandler.postDelayed(mRunnable, 3000);
}
}
When I’m adding images into drawable then it’s loading very fast without glide, but for glide with urls, it’s taking time to load at the beginning. After 5-7 secs only all pics coming and loading perfctly in viewpager2 slideshow. Please help, thank you very much.
Tried below options in glide, nothing is working out.
.disallowHardwareConfig()
.diskCacheStrategy(DiskCacheStrategy.NONE)