android – When I Enable Slider in Adapter Last position not showing (SLIDER_VIEW merge with ITEM_VIEW )

Need to Show Slider With a missing fisrt position of ITEM_VIEW

Check IMAGE 1 When Slider Enabled Don’t Showing first position of ITEM_VIEW (Issue is SLIDER_VIEW position merge with ITEM_VIEW position). Need to show ITEM_VIEW missing first position With Slider

Check IMAGE 2 When Slider Not Enabled ITEM_VIEW first position Showing

public class WallpapersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int ITEM_VIEW = 0;
private static final int AD_VIEW = 1;
private static final int SLIDER_VIEW = 2;
public static SharedPreferences sharedPreferences;
final long DELAY_MS = 500;//delay in milliseconds before task is to be executed
final long PERIOD_MS = 6000; // time in milliseconds between successive task executions.
private final Context mContext;
private final OnItemClickListener listener;
private final int itemLayout;
public List<Image> mData;
RequestOptions option;
SharedPref sharedPref;
String status;
List<SliderUtils> sliderImg;
ViewPagerAdapterSlider viewPagerAdapterSlider;
int currentPage = 1;
Timer timer;
private int dotscount;
private ImageView[] dots;

public WallpapersAdapter(Context mContext, List<Image> mData, OnItemClickListener listener) {
    this.mContext = mContext;
    this.mData = mData;
    this.listener = listener;
    this.itemLayout = R.layout.wallpaper_item;

    // Request option for Glide
    option = new RequestOptions().centerCrop();

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    if (viewType == ITEM_VIEW) {
        View view = layoutInflater.inflate(R.layout.wallpaper_item, parent, false);
        return new MainViewHolder(view);

    } else if (viewType == AD_VIEW) {
        View view = layoutInflater.inflate(R.layout.item_native_ad, parent, false);
        return new AdViewHolder(view);

    } else if (viewType == SLIDER_VIEW) {
        View view = layoutInflater.inflate(R.layout.custom_layout, parent, false);
        return new SliderViewHolder(view);
    } else {
        return null;
    }
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
    status = sharedPreferences.getString("status", "free");
    StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
    if (holder.getItemViewType() == ITEM_VIEW) {


        int pos = position - Math.round(position / Config.NATIVE_ADS_PER_ITEM);
        ((MainViewHolder) holder).bindData(mData.get(pos), listener, position);

    } else if (holder.getItemViewType() == AD_VIEW) {

        if (Config.ENABLE_ADS == true & status.equals("free")) {
            ((AdViewHolder) holder).bindAdData();
            layoutParams.setFullSpan(getItemViewType(position) == AD_VIEW);
        }

    } else if (holder.getItemViewType() == SLIDER_VIEW) {
        layoutParams.setFullSpan(getItemViewType(position) == SLIDER_VIEW);
        ((SliderViewHolder) holder).bindSliderData();

    }

}

@Override
public int getItemCount() {
    if (mData.size() > 0) {
        return mData.size() + Math.round(mData.size() / Config.NATIVE_ADS_PER_ITEM);
    }
    return mData.size();
}

@Override
public int getItemViewType(int position) {
    // Here position set 0 showing Slider But not showing last position
    if ((position == 0) && Config.ENABLE_RECENT_SLIDER == true) {
        return SLIDER_VIEW;
    } else if ((position + 1) % Config.NATIVE_ADS_PER_ITEM == 0) {
        return AD_VIEW;
    }
    return ITEM_VIEW;
}

private void sendRequest(ViewPager viewPagerForSlider, TextView textSlider) {

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, Config.SM, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {

                        JSONArray ja_data = response.getJSONArray("slides");

                        Log.d("OHOHOH", String.valueOf(ja_data));


                        for (int i = 0; i < ja_data.length(); i++) {

                            SliderUtils sliderUtils = new SliderUtils();

                            try {
                                JSONObject jsonObject = ja_data.getJSONObject(i);
                                sliderUtils.setSliderImageUrl(jsonObject.getString("slider_image"));
                                sliderUtils.setUrl(jsonObject.getString("url"));
                                sliderUtils.setSlider_name(jsonObject.getString("slider_name"));
                                sliderUtils.setType(jsonObject.getString("slider_type"));
                                sliderUtils.setCat_id(jsonObject.getInt("cat_id"));
                                sliderUtils.setCollection(jsonObject.getString("collection"));
                                //textSlider.setText(jsonObject.getString("slider_name"));

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                            sliderImg.add(sliderUtils);

                        }

                        viewPagerAdapterSlider = new ViewPagerAdapterSlider(sliderImg, mContext);
                        viewPagerForSlider.setAdapter(viewPagerAdapterSlider);
                        dotscount = viewPagerAdapterSlider.getCount();
                        dots = new ImageView[dotscount];

                        final Handler handler = new Handler();
                        final Runnable Update = new Runnable() {
                            public void run() {
                                if (currentPage == dotscount) {
                                    currentPage = 0;
                                }
                                viewPagerForSlider.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);

                    } catch (JSONException e) {
                        e.printStackTrace();

                    }


                }

            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {


        }
    });

    MySingleton.getInstance(mContext).addToRequestQueue(request);

}

public interface OnItemClickListener {
    void onItemClick(View view, Image mData, int position);
}

public class MainViewHolder extends RecyclerView.ViewHolder {

    WallpaperItemBinding binding;

    public MainViewHolder(@NonNull View itemView) {
        super(itemView);
        binding = WallpaperItemBinding.bind(itemView);

    }

    public void bindData(Image main, final OnItemClickListener listener, final int position) {
        sharedPref = new SharedPref(mContext);
        binding.wallpaperName.setText(main.getImage_name());
        binding.viewCount.setText(main.getViews_count());
        if (sharedPref.loadAmoled() == true) {
            binding.constraintBg.setBackgroundColor(mContext.getResources().getColor(R.color.amoled_color_light));
        }

        sharedPref = new SharedPref(mContext);

    }
}

public class AdViewHolder extends RecyclerView.ViewHolder {

    ItemNativeAdBinding binding;

    public AdViewHolder(@NonNull View itemView) {
        super(itemView);
        binding = ItemNativeAdBinding.bind(itemView);
    }

    @SuppressLint("MissingPermission")
    private void bindAdData() {
        final AdLoader adLoader = new AdLoader.Builder(mContext, mContext.getResources().getString(R.string.admob_native_id))
                .forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
                    @Override
                    public void onNativeAdLoaded(NativeAd ad) {

                        binding.admobNativeAdContainer.setNativeAd(ad);

                    }
                }).build();

        adLoader.loadAds(new AdRequest.Builder().build(), 3);

        if (isNetworkConnected()) {
            binding.admobNativeAdContainer.setVisibility(View.VISIBLE);
        }
    }

    @SuppressLint("MissingPermission")
    private boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
    }
}

public class SliderViewHolder extends RecyclerView.ViewHolder {

    CustomLayoutBinding customLayoutBinding;

    public SliderViewHolder(View view) {
        super(view);

        customLayoutBinding = CustomLayoutBinding.bind(view);
    }

    public void bindSliderData() {

        sliderImg = new ArrayList<>();


        customLayoutBinding.viewPagerForSlider.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                customLayoutBinding.dotsIndicator.setViewPager(customLayoutBinding.viewPagerForSlider);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        sendRequest(customLayoutBinding.viewPagerForSlider, customLayoutBinding.textSlider);

    }
}

}

Leave a Comment