快速问题,因为我的其他问题从未得到回答。我有RecyclerView
,每个项目SeekBar
都会填充CardView
。我想知道获得此SeekBar
职位的最佳方式。
换句话说,如果recyclerview中有两个项目各自包含自己的SeekBar
,我将如何区分第一个搜索栏和第二个搜索栏?
此外,如果这有帮助,所述SeekBar
的监听器在我的适配器中。
根据要求提供代码示例:
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) {
}
});
方法whichIsIt
应确定触发SeekBar
的{{1}}。
答案 0 :(得分:2)
您可以使用适配器seekBar.setTag(holderPosition);
所以在onProgressChanged(SeekBar seekBar,...)
方法中你做seekBar.getTag()
。这样您就可以在正确的位置更改数据。
P.S:如果OnSeekBarChangeListener与所有seekBars相同,您可以使用它创建一个私有字段。 ;)