简体   繁体   中英

How to limit multiple seekbars to 100% in recyclerview

This question has been somewhat answered in this thread , but I need it to work for however many items are in the recyclerview. For example, if I have three items in the recyclerview I'd like the three seekbars to depend on each other for a maximum total of 100% (or just simply 100).

Here is my code, which is in the custom adapter:

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();
}

As you can tell the code is very similar to that of the thread I linked because I wanted to see if it would work when making some changes. For example, what I've tried so far is made a List<Integer> which contains all of the progresses, and they've been set to 0 by looping through however many mMembers there are. In other words, if there are 3 mMembers (items in the recyclerview) then there will be 3 seekbars starting at 0.

I believe the code I need to work on is here:

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 + ")!");
        }

and the reason I know this is where the problem lies is because the ID being returned is always 0. So how should I go about this so that I can grab ALL the seekbars which exist, rather than grabbing just the first position? I have tried setting some type of counter, but I'm mainly stuck at how I'm going to iterate through all positions of the seekbars...

Edit: Here is a picture example of how it looks. Obviously it should not be 100% and 50%, but rather 50% and 50%.

在此处输入图片说明

instead of return 0; pass return position; it will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM