这个问题在这个thread中有所回答,但我需要它才能工作,因为许多项目都在recyclerview中。例如,如果我在recyclerview中有三个项目,我希望三个搜索栏相互依赖,最多总计100%(或者只是100)。
这是我的代码,它位于自定义适配器中:
public class MembersAdapter extends RecyclerView.Adapter<MembersAdapter.ViewHolder> {
//Tag
private static String TAG = "MembersAdapter";
//Max amount for all seekbars.
private final int TOTAL_AMOUNT = 100;
private final List<Integer> mAllProgress = new ArrayList<>();
private List<GroupMembers> mMembers;
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);
}
}
/**
* Constructor
*
* @param members : The members.
*/
public MembersAdapter(List<GroupMembers> members) {
this.mMembers = members;
}
@Override
public MembersAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout_members, parent, false);
return new ViewHolder(v);
}
@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());
//mAllProgress AMOUNTS equal to the SIZE or NUMBER of mMembers which exist.
//All SeekBars of size mMembers (members picked) should be 0% by default.
for (int i = 0; i <= mMembers.size() - 1; i++) {
mAllProgress.add(0);
}
//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) {
switch (id) {
case R.id.percent_seekbar:
holder.mPercent.setText(" " + progress + "%");
//This should return the SeekBars position out of however many seekbars there are.
return 0;
default:
throw new IllegalStateException(
"There should be a seekbar with this id(" + id + ")!");
}
}
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) {
}
});
}
@Override
public int getItemCount() {
return mMembers.size();
}
正如您所知,代码与我链接的代码非常相似,因为我想看看它是否可以在进行一些更改时起作用。例如,我到目前为止所尝试的内容是List<Integer>
,其中包含所有进度,并且通过循环遍历它们已设置为0,但有许多mMembers
。换句话说,如果有3 mMembers
(recyclerview中的项目),那么将有3个搜索栏从0开始。
我相信我需要处理的代码就在这里:
switch (id) {
case R.id.percent_seekbar:
holder.mPercent.setText(" " + progress + "%");
//This should return the SeekBars position out of however many seekbars there are.
return 0;
default:
throw new IllegalStateException(
"There should be a seekbar with this id(" + id + ")!");
}
我知道这就是问题所在的原因是因为返回的ID总是为0.所以我应该如何解决这个问题,这样我才能抓住所有存在的搜索栏,而不是只抓住第一个位置?我试过设置某种类型的计数器,但我主要考虑的是我将如何遍历搜索栏的所有位置...
编辑:这是一个看起来如何的图片示例。显然它不应该是100%和50%,而是50%和50%。
答案 0 :(得分:1)
而不是print(tokyo_time.replace(tzinfo=None))
print("hour: " + tokyo_time.hour)
传递return 0;
它将起作用