package com.azoft.carousellayoutmanager.sample;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.azoft.carousellayoutmanager.CarouselLayoutManager;
import com.azoft.carousellayoutmanager.CarouselZoomPostLayoutListener;
import com.azoft.carousellayoutmanager.CenterScrollListener;
import com.azoft.carousellayoutmanager.sample.databinding.ActivityCarouselPreviewBinding;
import com.azoft.carousellayoutmanager.sample.databinding.ItemViewBinding;

import java.util.Random;

public class CarouselPreviewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ActivityCarouselPreviewBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_carousel_preview);

        setSupportActionBar(binding.toolbar);

        final TestAdapter adapter = new TestAdapter();

        // create layout manager with needed params: vertical, cycle
        initRecyclerView(binding.listHorizontal, new CarouselLayoutManager(CarouselLayoutManager.HORIZONTAL, false), adapter);
        initRecyclerView(binding.listVertical, new CarouselLayoutManager(CarouselLayoutManager.VERTICAL, true), adapter);

        // fab button will add element to the end of the list
        binding.fabScroll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                final int itemToRemove = adapter.mItemsCount;
                if (10 != itemToRemove) {
                    adapter.mItemsCount++;
                    adapter.notifyItemInserted(itemToRemove);
                }
            }
        });

        // fab button will remove element from the end of the list
        binding.fabChangeData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                final int itemToRemove = adapter.mItemsCount - 1;
                if (0 <= itemToRemove) {
                    adapter.mItemsCount--;
                    adapter.notifyItemRemoved(itemToRemove);
                }
            }
        });
    }

    private void initRecyclerView(final RecyclerView recyclerView, final CarouselLayoutManager layoutManager, final TestAdapter adapter) {
        // enable zoom effect. this line can be customized
        layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener());

        recyclerView.setLayoutManager(layoutManager);
        // we expect only fixed sized item for now
        recyclerView.setHasFixedSize(true);
        // sample adapter with random data
        recyclerView.setAdapter(adapter);
        // enable center post scrolling
        recyclerView.addOnScrollListener(new CenterScrollListener());
        // enable center post touching on item and item click listener
        layoutManager.setOnItemClickListener(recyclerView,
                new CarouselLayoutManager.OnCenterItemClickListener() {
                    @Override
                    public void onCenterItemClicked(@NonNull RecyclerView recyclerView, int position,
                            @NonNull View view) {
                        final String msg = String.format("Item %1$d was clicked", position);
                        Toast.makeText(CarouselPreviewActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });

        layoutManager.addOnItemSelectionListener(new CarouselLayoutManager.OnCenterItemSelectionListener() {

            @Override
            public void onCenterItemChanged(final int adapterPosition) {
                if (CarouselLayoutManager.INVALID_POSITION != adapterPosition) {
                    final int value = adapter.mPosition[adapterPosition];
                    adapter.mPosition[adapterPosition] = (value % 10) + (value / 10 + 1) * 10;
                    adapter.notifyItemChanged(adapterPosition);
                }
            }
        });
    }

    private static final class TestAdapter extends RecyclerView.Adapter<TestViewHolder> {

        @SuppressWarnings("UnsecureRandomNumberGeneration")
        private final Random mRandom = new Random();
        private final int[] mColors;
        private final int[] mPosition;
        private int mItemsCount = 10;

        TestAdapter() {
            mColors = new int[10];
            mPosition = new int[10];
            for (int i = 0; 10 > i; ++i) {
                //noinspection MagicNumber
                mColors[i] = Color.argb(255, mRandom.nextInt(256), mRandom.nextInt(256), mRandom.nextInt(256));
                mPosition[i] = i;
            }
        }

        @Override
        public TestViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
            return new TestViewHolder(ItemViewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
        }

        @Override
        public void onBindViewHolder(final TestViewHolder holder, final int position) {
            holder.mItemViewBinding.cItem1.setText(String.valueOf(mPosition[position]));
            holder.mItemViewBinding.cItem2.setText(String.valueOf(mPosition[position]));
            holder.itemView.setBackgroundColor(mColors[position]);
        }

        @Override
        public int getItemCount() {
            return mItemsCount;
        }
    }

    private static class TestViewHolder extends RecyclerView.ViewHolder {

        private final ItemViewBinding mItemViewBinding;

        TestViewHolder(final ItemViewBinding itemViewBinding) {
            super(itemViewBinding.getRoot());

            mItemViewBinding = itemViewBinding;
        }
    }
}
@@ -591,32 +591,102 @@
 ScrollListener;%0A
+import com.azoft.carousellayoutmanager.DefaultChildSelectionListener;%0A
 import com.azoft
@@ -835,16 +835,41 @@
 nding;%0A%0A
+import java.util.Locale;%0A
 import j
@@ -3272,98 +3272,92 @@
     
-layoutManager.setOnItemClickListener(recyclerView,%0A                new CarouselLayoutManag
+DefaultChildSelectionListener.initCenterItemListener(new DefaultChildSelectionListen
 er.O
@@ -3393,32 +3393,24 @@
             
-        
 @Override%0A  
@@ -3403,32 +3403,24 @@
   @Override%0A
-        
             
@@ -3460,16 +3460,22 @@
 NonNull 
+final 
 Recycler
@@ -3497,79 +3497,170 @@
 ew, 
-int position,%0A                            @NonNull View view) %7B%0A       
+@NonNull final CarouselLayoutManager carouselLayoutManager, @NonNull final View v) %7B%0A                final int position = recyclerView.getChildLayoutPosition(v);%0A
     
@@ -3667,25 +3667,24 @@
             
- 
 final String
@@ -3704,16 +3704,27 @@
 .format(
+Locale.US, 
 %22Item %251
@@ -3747,32 +3747,24 @@
  position);%0A
-        
             
@@ -3853,32 +3853,24 @@
             
- 
-       
 %7D%0A          
@@ -3867,25 +3867,46 @@
         
-        %7D
+%7D, recyclerView, layoutManager
 );%0A%0A    
@@ -4261,16 +4261,19 @@
 ition%5D;%0A
+/*%0A
         
@@ -4359,16 +4359,16 @@
 ) * 10;%0A
-
         
@@ -4415,32 +4415,35 @@
 apterPosition);%0A
+*/%0A
                 

update sample