private void addWeightElement() { // currentItemsCnt++; WeightItem item = new WeightItem(); item.position = helper.itemsList.size(); item.weight = 100 / currentItemsCnt; helper.itemsList.add(item); TextView text = new TextView(this); text.setText("pos" + item.position); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.weight = item.weight; text.setLayoutParams(params); text.setId(0x00001000 + item.position); text.setBackgroundColor( Color.rgb( 0xFF / (item.position + 1), 0x7F / (item.position + 1), 0xFF / (item.position + 1))); weightView.addView(text); // Liean container TextView seekText = new TextView(this); seekText.setText("pos " + item.position); seekText.setPadding(0, 0, 10, 0); SeekBar seekBar = new SeekBar(this); seekBar.setId(0x00001000 + item.position + 30); seekBar.setMax(100 / currentItemsCnt); seekBar.setOnSeekBarChangeListener(new SeekListener(item)); seekBar.setPadding(0, 5, 0, 5); seekBar.setProgress(seekBar.getMax()); LinearLayout linearLayout = new LinearLayout(this); // LinearLayout.LayoutParams params = new // LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, // ViewGroup.LayoutParams.WRAP_CONTENT); seekBar.setLayoutParams(params); linearLayout.addView(seekText); linearLayout.addView(seekBar); seekView.addView(linearLayout); TextView ttt = new TextView(this); ttt.setId(0x00002000 + item.position); ttt.setText("weight of item " + item.position + " w = " + item.weight); changeView.addView(ttt); }
public void recountWeights(int immutablePosition, float diff) { int items2change = 0; for (WeightItem weightItem : itemsList) { if (!weightItem.isTouched) items2change++; } float weightDiff = diff / items2change; for (int i = 0, itemsListSize = itemsList.size(); i < itemsListSize; i++) { if (i == immutablePosition) { continue; } WeightItem weightItem = itemsList.get(i); if (weightItem.isTouched) continue; weightItem.weight += weightDiff; } }
@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { if (!b) return; float diff = item.weight - progress; item.weight = progress; item.isTouched = true; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.weight = progress; findViewById(0x00001000 + item.position).setLayoutParams(params); helper.recountWeights(item.position, diff); // change weight of other elements // update other views LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); for (WeightItem weightItem : helper.itemsList) { TextView weTxt = (TextView) findViewById(0x00002000 + weightItem.position); weTxt.setText("weight of item " + weightItem.position + " w = " + weightItem.weight); if (weightItem.position == item.position) { continue; } params1.weight = weightItem.weight; findViewById(0x00001000 + weightItem.position).setLayoutParams(params1); } // TextView weightOfItemTxt = (TextView) findViewById(0x00002000 + item.position); // weightOfItemTxt.setText("weight of item " + item.position + " w = " + // item.weight); total.setText("total weightSum = " + helper.getTotalWeightSum()); weightView.invalidate(); changeView.invalidate(); }