quick question since my other question never got answered. I have a RecyclerView
which gets populated with a SeekBar
for every item (CardView
). I would like to know the best way to get this SeekBar
's position.
In other words, how would I be able to differentiate the first seekbar from the second seekbar if there are two items in the recyclerview each containing its own SeekBar
?
Also, if this helps any, the listener for said SeekBar
is in my Adapter.
Sample of code upon request:
class ViewHolder extends RecyclerView.ViewHolder {
private TextView mName, mUsername, mPercent;
private SeekBar mSeekBar;
ViewHolder(View itemView) {
super(itemView);
mName = (TextView) itemView.findViewById(R.id.name_view_members);
mUsername = (TextView) itemView.findViewById(R.id.username_view_members);
mPercent = (TextView) itemView.findViewById(R.id.percent_text);
mSeekBar = (SeekBar) itemView.findViewById(R.id.percent_seekbar);
}
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
GroupMembers individual = mMembers.get(position);
holder.mName.setText(individual.getName());
holder.mUsername.setText(individual.getUsername());
//SeekBar Listener
holder.mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Find out which SeekBar triggered the event
int which = whichIsIt(seekBar.getId(), progress);
//Stored progress (where is it at)
int storedProgress = mAllProgress.get(which);
/**Two cases can occur: User goes left or right with the thumb.
* If RIGHT, we must check how much he's allowed to go in that
* direction (based on other seekbars), and stop him before it's
* too late. If LEFT, free up the space to allow the other seekbars.
**/
if (progress > storedProgress) {
//How much remains based on all seekbars.
int remaining = remaining();
//If progress 100%, don't let user move. (overextend)
if (remaining == 0) {
seekBar.setProgress(storedProgress);
} else {
//Progress available, check the availability of others,
//and let the user move as long as it's below 100% total.
if (storedProgress + remaining >= progress) {
mAllProgress.set(which, progress);
} else {
mAllProgress.set(which, storedProgress + remaining);
}
}
} else if (progress <= storedProgress) {
mAllProgress.set(which, progress);
}
}
private int whichIsIt(int id, int progress) {
holder.mPercent.setText(" " + progress + "%");
return R.id.percent_seekbar;
}
private int remaining() {
int remaining = TOTAL_AMOUNT;
for (int i = 0; i < mAllProgress.size(); i++) {
remaining -= mAllProgress.get(i);
}
if (remaining >= 100) {
remaining = 100;
} else if (remaining <= 0) {
remaining = 0;
}
return remaining;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
The method whichIsIt
should determine which SeekBar
of the RecyclerView
is being triggered.
You can set a tag for each seekBar with the position in the adapter
seekBar.setTag(holderPosition);
So in the method
onProgressChanged(SeekBar seekBar,...)
you doseekBar.getTag()
. This way you can change your data in the right position.P.S: If OnSeekBarChangeListener is the same for all seekBars you can create a private field with it. ;)