Reputation: 7686
Currently I have a "Playlist" of videos who I can change to the next position (video player).
The RecyclerView has this full ArrayList and scroll to the next position like this:
mLinearLayoutManager.scrollToPositionWithOffset(position, 10);
Perfect! Now, I want to AUTO SELECT (I don't want clicks here...) the current position and highlight it.
The final product should look like this:
and every time I hit next on the media player, next item (position) will scroll and highlight the background.
How do I pass the position to the Adapter to know the position to highlight/change background? Remember... there is not clicks involves in this task and the item needs to keep highlighted the whole time, only changes when the next video/item/position is selected.
Few changes on the Adapter to make it work
private int highlightItem = 0;
public void toggleSelection(int pos) {
this.highlightItem = pos;
}
toggleSelection(position)
will be used from my Activity where I storage the current position all the time.
After that, the final change in my Adapter was:
if (position == highlightItem) {
holder.mMainView.setSelected(true);
} else {
holder.mMainView.setSelected(false);
}
Upvotes: 2
Views: 3130
Reputation: 24211
You need to change your adapter a bit. Write a toggleSelection
function in your adapter and pass the position to the function. Keep the track of the position of the items in your playlist in a global array. So that, when a song is played you know its position in the RecyclerView
.
I'm adding an example of an adapter. You can have your toggleSelection
function customized.
public class ToggleSelectionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Cursor mCursor;
private SparseBooleanArray selectedItems;
public ToggleSelectionListAdapter(Cursor cursor) {
mCursor = cursor;
selectedItems = new SparseBooleanArray();
}
public void toggleSelection(int pos) {
if (selectedItems.get(pos, false)) {
selectedItems.delete(pos);
} else {
selectedItems.put(pos, true);
}
notifyItemChanged(pos);
}
public int getSelectedItemCount() {
return selectedItems.size();
}
public void clearSelections() {
selectedItems.clear();
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(final View itemView) {
super(itemView);
// Initialize your items of each row here
}
public void bindView(int pos) {
try {
if (mCursor.isClosed())
return;
mCursor.moveToPosition(pos);
// Maintain a checked item list so that you can have a track if the item is clicked or not
if (checkedItems.contains(number) itemView.setBackgroundResource(R.drawable.background_selected);
else itemView.setBackgroundResource(R.drawable.background_normal);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkedItems.contains(number)) {
checkedItems.remove(number);
} else {
checkedItems.add(number);
}
// Get the index of which item should toggle the background
int idx = mRecyclerView.getChildAdapterPosition(v);
toggleSelection(idx);
}
}
});
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {
ViewHolder vh = (ViewHolder) holder;
vh.bindView(position);
}
}
@Override
public int getItemCount() {
if (mCursor == null) {
return 0;
}
int n = mCursor.getCount();
return n;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
synchronized public void swapCursor(Cursor cursor) {
mCursor = cursor;
notifyDataSetChanged();
}
}
Upvotes: 1