package com.nf2m.adapter; import android.annotation.SuppressLint; import android.content.Context; import android.database.Cursor; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.SectionIndexer; import com.nf2m.listener.OnItemClickListener; import com.nf2m.listener.OnItemLongClickListener; import com.nf2m.section.AlphabetGroup; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @SuppressLint("UseSparseArrays") public abstract class LibraryCursorAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> implements SectionIndexer { private static final String TAG = "Position List"; @NonNull private final HashMap<Integer, Boolean> selectionList; @NonNull private final List<Integer> positionList; private final String searchUri; @Nullable private Cursor cursor; private final LayoutInflater inflater; OnItemClickListener clickListener; OnItemLongClickListener longClickListener; @SuppressWarnings("deprecation") LibraryCursorAdapter(Context context, Cursor c, String searchUri) { cursor = c; this.searchUri = searchUri; inflater = LayoutInflater.from(context); selectionList = new HashMap<>(); positionList = new LinkedList<>(); } @Nullable Cursor getCursor() { return cursor; } public void swapCursor(@Nullable Cursor newCursor) { if (newCursor != null) { cursor = newCursor; notifyDataSetChanged(); } } //Listede yeni bir öge seçilirse bu metod çağrılır. public void addNewSelection(int position, boolean check) { selectionList.put(position, check); positionList.add(position); notifyItemChanged(position); Log.i(TAG, positionList.toString()); } //Listedeki seçili bir ögeyi seçimini kaldırır public void removeSelection(int position) { int location = getLocation(position); positionList.remove(location); selectionList.remove(position); notifyItemChanged(position); Log.i(TAG, positionList.toString()); } @NonNull public List<Integer> getPositionList() { return positionList; } public boolean isPositionChecked(int position) { return selectionList.get(position) != null; } private int getLocation(int position) { int location = 0; for (int i = 0; i < positionList.size(); i++) { if (position == positionList.get(i)) { location = i; break; } } return location; } public void clearSelection() { selectionList.clear(); positionList.clear(); notifyDataSetChanged(); } public void selectAllItem() { clearSelection(); for (int i = 0; i < cursor.getCount(); i++) { addNewSelection(i, true); } } public void removeSelectAllItems() { for (int i = 1; i < cursor.getCount(); i++) { removeSelection(i); } } int getColumnIndex(String uri) { return cursor.getColumnIndexOrThrow(uri); } View inflateCustomViewAndListView(ViewGroup parent, int resId) { return inflater.inflate(resId, parent, false); } @NonNull public static String convertMinuteToMillis(int millis) { int minute = 0; int second = millis / 1000; while (second > 60) { minute++; second -= 60; } if (second < 10) { return minute + ":0" + second; } return minute + ":" + second; } public void setOnItemClickListener(final OnItemClickListener clickListener) { this.clickListener = clickListener; } public void setOnLongItemClickListener(OnItemLongClickListener itemLongClickListener) { this.longClickListener = itemLongClickListener; } @Override public Object[] getSections() { return AlphabetGroup.values(); } @Override public int getPositionForSection(int sectionIndex) { return 0; } @Override public int getSectionForPosition(int position) { if (position >= cursor.getCount()) { position = cursor.getCount() - 1; } if (cursor.getCount() != 0) { cursor.moveToPosition(position); String songTitle = cursor.getString(cursor.getColumnIndexOrThrow(searchUri)); return compareGroup(songTitle); } return 0; } private int compareGroup(@NonNull String songTitle) { for (AlphabetGroup alphabetGroup : AlphabetGroup.values()) { if (alphabetGroup.getmGroupName().equalsIgnoreCase(String.valueOf(songTitle.charAt(0)))) { return alphabetGroup.ordinal(); } } return AlphabetGroup.None.ordinal(); } }