package in.myinnos.indexfastscrollrecycler.adapter;

/**
 * Created by MyInnos on 01-02-2017.
 */

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SectionIndexer;
import android.widget.TextView;


import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.myinnos.alphabetsindexfastscrollrecycler.utilities_fs.StringMatcher;
import in.myinnos.indexfastscrollrecycler.R;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements SectionIndexer {

    private List<String> mDataArray;
    private String mSections = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public RecyclerViewAdapter(List<String> dataset) {
        mDataArray = dataset;
    }

    @Override
    public int getItemCount() {
        if (mDataArray == null)
            return 0;
        return mDataArray.size();
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(mDataArray.get(position));
    }

    @Override
    public int getPositionForSection(int section) {
        // If there is no item for current section, previous section will be selected
        for (int i = section; i >= 0; i--) {
            for (int j = 0; j < getItemCount(); j++) {
                if (i == 0) {
                    // For numeric section
                    for (int k = 0; k <= 9; k++) {
                        if (StringMatcher.match(String.valueOf(mDataArray.get(j).charAt(0)), String.valueOf(k)))
                            return j;
                    }
                } else {
                    if (StringMatcher.match(String.valueOf(mDataArray.get(j).charAt(0)), String.valueOf(mSections.charAt(i))))
                        return j;
                }
            }
        }
        return 0;
    }

    @Override
    public int getSectionForPosition(int position) {
        return 0;
    }

    @Override
    public Object[] getSections() {
        String[] sections = new String[mSections.length()];
        for (int i = 0; i < mSections.length(); i++)
            sections[i] = String.valueOf(mSections.charAt(i));
        return sections;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_alphabet)
        TextView mTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
@@ -303,16 +303,43 @@
 tView;%0A%0A
+import java.util.ArrayList;
 %0Aimport 
@@ -416,87 +416,8 @@
 fe;%0A
-import in.myinnos.alphabetsindexfastscrollrecycler.utilities_fs.StringMatcher;%0A
 impo
@@ -634,55 +634,44 @@
 ate 
-String mSections = %22ABCDEFGHIJKLMNOPQRSTUVWXYZ%22
+ArrayList%3CInteger%3E mSectionPositions
 ;%0A%0A 
@@ -1241,24 +1241,30 @@
 dViewHolder(
+final 
 ViewHolder h
@@ -1354,292 +1354,244 @@
     
-%7D%0A%0A
     
-@Override%0A    public int getPositionForSection(int section) %7B%0A        // If there is no item for current section, previous section will be selected%0A        for (int i = section; i %3E= 0; i--) %7B%0A            for (int j = 0; j %3C getItemCount(); j++) %7B%0A                if (i == 0) %7B
+holder.mTextView.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                mDataArray.remove(holder.getAdapterPosition());%0A                notifyDataSetChanged();
 %0A   
@@ -1599,24 +1599,26 @@
         
+%7D%0A
         
 // For n
@@ -1613,324 +1613,359 @@
     
-// For numeric section
+%7D);
 %0A    
+%7D%0A%0A
     
-            for (int k = 0; k %3C= 9; k++) %7B%0A   
+@Override%0A    public int getSectionForPosition(int position) %7B%0A        return 0;%0A
     
+%7D%0A%0A
     
-             if (StringMatcher.match(String.valueOf(mDataArray.get(j).charAt(0)), String.valueOf(k)))%0A                            return j;%0A                    %7D%0A                %7D else %7B%0A                    if (StringMatcher.match(
+@Override%0A    public Object%5B%5D getSections() %7B%0A        List%3CString%3E sections = new ArrayList%3C%3E(26);%0A        mSectionPositions = new ArrayList%3C%3E(26);%0A        for (int i = 0, size = mDataArray.size(); i %3C size; i++) %7B%0A            String section = 
 Stri
@@ -1990,17 +1990,17 @@
 ray.get(
-j
+i
 ).charAt
@@ -2007,490 +2007,352 @@
 (0))
-, String.valueOf(mSections.charAt(i))))%0A                        return j;%0A                %7D%0A            %7D%0A        %7D%0A        return 0;%0A    %7D%0A%0A    @Override%0A    public int getSectionForPosition(int position) %7B%0A        return 0;%0A    %7D%0A%0A    @Override%0A    public Object%5B%5D getSections() %7B%0A        String%5B%5D sections = new String%5BmSections.length()%5D;%0A        for (int i = 0; i %3C mSections.length(); i++)%0A            sections%5Bi%5D = String.valueOf(mSections.charAt(i));%0A        return 
+.toUpperCase();%0A            if (!sections.contains(section)) %7B%0A                sections.add(section);%0A                mSectionPositions.add(i);%0A            %7D%0A        %7D%0A        return sections.toArray(new String%5B0%5D);%0A    %7D%0A%0A    @Override%0A    public int getPositionForSection(int sectionIndex) %7B%0A        return mSectionPositions.get(
 section
-s
+Index)
 ;%0A  

add example for dynamic sections, which are updated on data changes