danoz73 / RecyclerViewFastScroller

A Fast Scroller for the RecyclerView world!
Other
1.13k stars 211 forks source link

SectionIndexer in documentations #37

Open lasotaartur opened 9 years ago

lasotaartur commented 9 years ago

Documentation should contain informations about SectionIndexer in Adapter for SectionIndicators.

jjhesk commented 8 years ago

truely.. i dont understand how to implement the index for dynamic data loading mechanism.. references

jjhesk commented 8 years ago

try this


/**
 * Created by hesk on 26/8/15.
 */
public abstract class ArrAdapter<M, VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> {
    private ArrayList<M> items = new ArrayList<M>();

    public ArrAdapter() {
        setHasStableIds(true);
    }

    public void add(M object) {
        items.add(object);
        notifyDataSetChanged();
    }

    public void add(int index, M object) {
        items.add(index, object);
        notifyDataSetChanged();
    }

    public void addAll(Collection<? extends M> collection) {
        if (collection != null) {
            items.addAll(collection);
            notifyDataSetChanged();
        }
    }

    public void addAll(M... items) {
        addAll(Arrays.asList(items));
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    public void remove(M object) {
        items.remove(object);
        notifyDataSetChanged();
    }

    public M getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

}

    private class StickArrayAdaptera extends ArrAdapter<T, HHV>
            implements StickyRecyclerHeadersAdapter<HHV>, SectionIndexer {
        @Override
        public HHV onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.slm_item, parent, false);
            return new HHV(view);
        }

        @Override
        public HHV onCreateHeaderViewHolder(ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.slm_header, parent, false);
            return new HHV(view);
        }

        @Override
        public long getHeaderId(int position) {
            long t = getFirstCharacterCode(getItem(position));
            //.   return getItem(position).charAt(0);
            return t;
        }

        @Override
        public void onBindViewHolder(HHV holder, int position) {
            holder.setText(getItemPosition(getItem(position)));
        }

        @Override
        public void onBindHeaderViewHolder(HHV holder, int position) {
            holder.setText(getItemPositionFirstCharacter(getItem(position)));
            // TextView textView = (TextView) holder.itemView;
            //  textView.setText(String.valueOf(getItem(position).charAt(0)));
            // holder.itemView.setBackgroundColor(getRandomColor());
        }

        @Override
        public Object[] getSections() {
            return new Object[0];
        }

        @Override
        public int getPositionForSection(int sectionIndex) {
            return 0;
        }

        @Override
        public int getSectionForPosition(int position) {

            if (position >= getItemCount()) {
                position = getItemCount() - 1;
            }

            return 0;
        }
    }