如何在某个级别停止搜索栏进度?

How to stop seekbar progress at a certain level?

我有一个简单的问题。我有一个 SeekBar 的列表。我的要求是,当用户将第一个 SeekBar 拖动到 70% 时,下一个应该停止在 30%。我如何才能停止 SeekBar 达到特定值或我的情况下的 %?

public class ProductsAdapter extends BaseAdapter {
    private ArrayList<Products> list;
    //private LayoutInflater layoutInflater;
    private int shelfShareSum;
    private int remainingShelfShare;
    private static HashMap<String, Integer> shelfShareValues = new HashMap<String, Integer>();
    private HashMap<Integer, Integer> productShelfShareMap = new HashMap<Integer, Integer>();
    private Context con;
    private SQLiteDatabase database;
    private SQLiteOpenHelper dbHelper;
    //SparseIntArray seekbarValuePos = new SparseIntArray();

    public ProductsAdapter(Context context, ArrayList<Products> list){
        this.list = list;
        //layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        dbHelper = new DBHelper(context);
        database = dbHelper.getWritableDatabase();
        this.con = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        final shelfShareHolder holder;
        if (row == null){

            LayoutInflater mInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = mInflater.inflate(R.layout.shelf_share_list_view, null);
            holder = new shelfShareHolder();
            holder.productTextView = (TextView) row.findViewById(R.id.shelfShareProductTextView);
            holder.percentTextView = (TextView) row.findViewById(R.id.seekbarValue);
            holder.shelfShareSeekbar = (SeekBar) row.findViewById(R.id.seekBar1);
            row.setTag(holder);
        }else {
            holder = (shelfShareHolder) row.getTag();
        }
        Products p = list.get(position);

            //int pID = p.getProductID();
            int prog = p.getSetSlider();

            holder.shelfShareSeekbar.setProgress(list.get(position).getSetSlider());


        holder.shelfShareSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //shelfShareSum = progress;

                holder.percentTextView.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                remainingShelfShare = 100 - shelfShareSum;
                Integer preV = productShelfShareMap.get(((Products)list.get(position)).getProductID());
                if(preV == null){
                    holder.shelfShareSeekbar.setMax(remainingShelfShare);
                }else {
                    holder.shelfShareSeekbar.setMax(Integer.parseInt(holder.percentTextView.getText().toString()) + remainingShelfShare);
                }

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                    /*shelfShareSum +=Integer.parseInt(seekBarValue.getText().toString());
                    Log.i("SUM", "onStopTrackingTouch: "+shelfShareSum);
                    seekBar.setMax(shelfShareSum);*/
                list.get(position).setSetSlider(seekBar.getProgress());
                shelfShareSum = 0;
                //remainingShelfShare = 100;

                productShelfShareMap.put(((Products)list.get(position)).getProductID(), Integer.parseInt(holder.percentTextView.getText().toString()));
                for (HashMap.Entry<Integer, Integer> entry : productShelfShareMap.entrySet()){

                    shelfShareSum += entry.getValue();

                }

                shelfShareValues.put(((Products)list.get(position)).getShelfShareID()+"-"+((Products)list.get(position)).getProductID(), Integer.parseInt(holder.percentTextView.getText().toString()));
                //shelfShareValues.put(((Products)list.get(position)).getShelfShareID()+"-"+((Products)list.get(position)).getCategoryID(), Integer.parseInt(holder.percentTextView.getText().toString())+"-"+((Products)list.get(position)).getProductID());
                //Info.getInstance().setShelfSharePercent(shelfShareValues);

            }
        });
        holder.productTextView.setText(((Products)list.get(position)).getProductName());

        return row;
    }

    public static HashMap<String, Integer> sendshelfShareValuesMap(){
        return shelfShareValues;
    }

    private static class shelfShareHolder{
        TextView productTextView;
        SeekBar shelfShareSeekbar;
        TextView percentTextView;
    }

}

为了有这种行为,我们可以有一个指针,它的值是任何搜索栏最多可以达到多少%。并且可以在每次拖动时更新。

对于下面的演示,我假设我们从服务器获得的任何进度值都是正确的,正如您告诉我的那样。

ProgressModel.java : 用于存储进度信息的 pojo class。

public class ProgressModel {

    //default value set to zero
    public int progress = 0;
}

ListAdapter.java:一个虚拟适配器class,它将根据所需的进度逻辑调整所有项目。

public class ListAdapter extends ArrayAdapter<ProgressModel> {

    private Context mContext;
    private ArrayList<ProgressModel> mProgressModels;
    //set default to 100.
    private int currentMaxProgress = 100;


    public void setCurrentMaxProgress(int progress) {
        currentMaxProgress = progress;
    }

    public ListAdapter(Context context, ArrayList<ProgressModel> progressModels) {
        super(context, R.layout.list_item, progressModels);
        mContext = context;
        mProgressModels = progressModels;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
            holder.seekBar = convertView.findViewById(R.id.seekBar);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ProgressModel progressModel = mProgressModels.get(position);

        holder.seekBar.setProgress(progressModel.progress);


        final ViewHolder finalHolder = holder;
        holder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                //check how much percentage user has dragged and if it exceeds the max limit, show a warning message, and send the seekbar back to its position
                if (progress - progressModel.progress > currentMaxProgress) {
                    finalHolder.seekBar.setProgress(progressModel.progress);
                    Toast.makeText(mContext, "Cant go beyond " + currentMaxProgress + "%", Toast.LENGTH_LONG).show();
                } else {
                    progressModel.progress = progress;
                    notifyDataSetChanged();
                    updateCurrentMaxProgress();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        return convertView;
    }

    //update the currentMaxProgress by iterating over whole array
    private void updateCurrentMaxProgress() {
        int totalProgress = 0;
        for(int i = 0; i < mProgressModels.size(); i++){

            totalProgress = totalProgress + mProgressModels.get(i).progress;
        }
        setCurrentMaxProgress(100 - totalProgress);
    }

    public static class ViewHolder {
        SeekBar seekBar;
    }

}

MainActivity.java :

public class MainActivity extends AppCompatActivity {

    ListAdapter mListAdapter;
    ArrayList<ProgressModel> mProgressModels = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView mListView = findViewById(R.id.list_view);
        mListAdapter = new ListAdapter(this, mProgressModels);
        mListView.setAdapter(mListAdapter);
        loadDataFromServer();
    }

    //assuming from server side it'll always be correct.
    public void loadDataFromServer(){
        int totalProgress = 0;
        for(int i = 1; i<=4; i++){
            int progress = i * 10;
            ProgressModel progressModel = new ProgressModel();
            progressModel.progress = progress;
            mProgressModels.add(progressModel);
            totalProgress = totalProgress + progress;
        }
        mListAdapter.setCurrentMaxProgress(100 - totalProgress);
        mListAdapter.notifyDataSetChanged();
    }


}