daimajia / AndroidImageSlider

An amazing and convenient Android image slider.
MIT License
5.62k stars 1.66k forks source link

sliding images in cardview inside recycler #241

Open maxotov opened 8 years ago

maxotov commented 8 years ago

I want to use image slider in cardview inside recyclerview. Problem is when sliding images one item in slider layout repeat images another item. How to solve this?

My adapter class code:

`public class GoodsAdapter extends RecyclerView.Adapter implements BaseSliderView.OnSliderClickListener{ private final int VIEW_ITEM = 1; private final int VIEW_PROG = 0;

private List<Goods> goodsList;
private boolean loading;
private Activity activity;

public GoodsAdapter(List<Goods> rows, Activity act) {
    goodsList = rows;
    activity = act;
}

@Override
public int getItemViewType(int position) {
    return goodsList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                  int viewType) {
    RecyclerView.ViewHolder vh;
    if (viewType == VIEW_ITEM) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.goods_item, parent, false);

        vh = new ShopViewHolder(v);
    } else {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.progress_item, parent, false);

        vh = new ProgressViewHolder(v);
    }
    return vh;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof ShopViewHolder) {

        Goods goods = goodsList.get(position);

        ((ShopViewHolder) holder).name.setText(goods.getName());
        ((ShopViewHolder) holder).goodsId.setText(String.valueOf(goods.getId()));
        if(goods.getPhotoList()!=null && !goods.getPhotoList().isEmpty()){
            for(Photo photo : goods.getPhotoList()){
                DefaultSliderView imageSliderView = new DefaultSliderView(activity);
                /**TextSliderView textSliderView = new TextSliderView(activity);
                // initialize a SliderLayout
                textSliderView
                        .image(Helper.PREFIX_API_URL+photo.getDescription())
                        .setScaleType(BaseSliderView.ScaleType.Fit)
                        .setOnSliderClickListener(this);

                //add your extra information
                textSliderView.bundle(new Bundle());
                textSliderView.getBundle()
                        .putString("extra",String.valueOf(goods.getId()));*/
                imageSliderView.image(Helper.PREFIX_API_URL+photo.getDescription())
                        .setScaleType(BaseSliderView.ScaleType.Fit)
                        .setOnSliderClickListener(this);
                imageSliderView.bundle(new Bundle());
                imageSliderView.getBundle()
                        .putString("extra",String.valueOf(goods.getId()));

                ((ShopViewHolder) holder).mDemoSlider.addSlider(imageSliderView);
            }
        } else {
            TextSliderView textSliderView = new TextSliderView(activity);
            // initialize a SliderLayout
            textSliderView
                    .image(R.drawable.no_media)
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener(this);
            //add your extra information
            textSliderView.bundle(new Bundle());
            textSliderView.getBundle()
                    .putString("extra",String.valueOf(goods.getId()));

            ((ShopViewHolder) holder).mDemoSlider.addSlider(textSliderView);
        }
        ((ShopViewHolder) holder).mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Default);
        //((ShopViewHolder) holder).mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        ((ShopViewHolder) holder).mDemoSlider.setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);
        ((ShopViewHolder) holder).mDemoSlider.setCustomAnimation(new DescriptionAnimation());
        ((ShopViewHolder) holder).mDemoSlider.setDuration(4000);
        ((ShopViewHolder) holder).mDemoSlider.stopAutoCycle();
        //((ShopViewHolder) holder).mDemoSlider.addOnPageChangeListener(this);
    } else {
        ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
    }
}

public void setLoaded() {
    loading = false;
}

@Override
public int getItemCount() {
    return goodsList.size();
}

public class ShopViewHolder extends RecyclerView.ViewHolder {
    public TextView name;
    public TextView goodsId;
    public SliderLayout mDemoSlider;
    public ImageView overflow;

    public ShopViewHolder(View v) {
        super(v);
        name = (TextView) v.findViewById(R.id.goods_name);
        goodsId = (TextView) v.findViewById(R.id.goods_id);
        mDemoSlider = (SliderLayout) v.findViewById(R.id.slider);
        overflow = (ImageView) v.findViewById(R.id.overflow);

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Goods id = ", goodsId.getText().toString());
                 Intent intent = new Intent(activity, GoodsActivity.class);
                 intent.putExtra("goods_id", goodsId.getText().toString());
                 activity.startActivity(intent);
            }
        });

        overflow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupMenu(overflow, goodsId.getText().toString());
            }
        });
    }
}

public static class ProgressViewHolder extends RecyclerView.ViewHolder {
    public ProgressBar progressBar;

    public ProgressViewHolder(View v) {
        super(v);
        progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
    }
}

@Override
public void onSliderClick(BaseSliderView slider) {
    Log.d("Goods id = ", slider.getBundle().get("extra")+"");
    Intent intent = new Intent(activity, GoodsActivity.class);
    intent.putExtra("goods_id", slider.getBundle().get("extra")+"");
    activity.startActivity(intent);
}`
kavyaub commented 7 years ago

@maxotov Did you find any solution to this. I am also facing a similar problem and wanted a solution.

minafared commented 7 years ago

i faced this problem and i solved it by adding mDemoSlider = (SliderLayout) v.findViewById(R.id.slider); mDemoSlider.removeAllSliders(); // to disable repeating items while scrolling