Azoft / CarouselLayoutManager

Android Carousel LayoutManager for RecyclerView
Apache License 2.0
2.56k stars 367 forks source link

First Item appearance doesn't change #64

Closed Finnegan1441 closed 6 years ago

Finnegan1441 commented 6 years ago

Im trying to make it so the center items alpha is 1.0 and the items next to it are set to 0.5f like below:

screenshot_20171116-134305

However the very first item in the carousel remains unchanged like this: screenshot_20171116-134313

Here is the code I am using:

carouselLayoutManager.addOnItemSelectionListener(new CarouselLayoutManager.OnCenterItemSelectionListener() {

            @Override
            public void onCenterItemChanged(int adapterPosition) {
                pageIndicatorView.setSelection(adapterPosition);

                creditsCarouselAdapter.viewArrayList[adapterPosition].imageView.setAlpha(1.0f);

                if (adapterPosition > 0 && creditsCarouselAdapter.viewArrayList[adapterPosition-1] != null) {
                    creditsCarouselAdapter.viewArrayList[adapterPosition - 1].imageView.setAlpha(0.5f);
                }
                if (adapterPosition < creditsCarouselAdapter.viewArrayList.length -1 && creditsCarouselAdapter.viewArrayList[adapterPosition+1] != null) {
                    creditsCarouselAdapter.viewArrayList[adapterPosition + 1].imageView.setAlpha(0.5f);
                }

            }
        });

and here is the code for Carousel Adapter where I create a list of viewholders:

public class CreditsCarouselAdapter extends RecyclerView.Adapter<CreditsCarouselAdapter.ViewHolder> {

    private ArrayList<Integer> imageslist;
    private RecyclerView carouselView;
    public ViewHolder[] viewArrayList;

    public CreditsCarouselAdapter(ArrayList<Integer> imageslist, RecyclerView carouselView){

        this.imageslist = imageslist;
        this.carouselView = carouselView;
        this.viewArrayList = new ViewHolder[imageslist.size()];
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View v = layoutInflater.inflate(R.layout.carousel_credit_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.imageView.setImageResource(imageslist.get(position));
        viewArrayList[position] = holder;
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView circle;
        public ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);

            circle = itemView.findViewById(R.id.circleView);

            circle.getLayoutParams().height = carouselView.getHeight()*3/4;
            circle.getLayoutParams().width = carouselView.getWidth()*3/4;
            circle.requestLayout();

            imageView = itemView.findViewById(R.id.creditImageView);

            imageView.getLayoutParams().height = carouselView.getHeight()*7/12;
            imageView.getLayoutParams().width = carouselView.getWidth()*7/12;
            imageView.requestLayout();

        }
    }

}

Note I have also tried this with the carouselView.getChildAt() method.

Finnegan1441 commented 6 years ago

I figured it out. Changed code to:

        carouselLayoutManager.addOnItemSelectionListener(new CarouselLayoutManager.OnCenterItemSelectionListener() {

            @Override
            public void onCenterItemChanged(int adapterPosition) {
                pageIndicatorView.setSelection(adapterPosition);
                creditsCarouselAdapter.currentPosition = adapterPosition;
itsCarouselAdapter.notifyDataSetChanged();

            }
        });

and the onBindView Holder method in CarouselAdapater to:

 public int currentPosition = 0;

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.imageView.setImageResource(imageslist.get(position));
        viewArrayList[position] = holder;
        if (position == currentPosition){
            holder.itemView.setAlpha(1.0f);
        }else {
            holder.itemView.setAlpha(0.5f);
        }
    }

Closing Issue.