0
嗯,我有一個seekBar到自定義列表視圖中,並且我在onChangeClickListener上使用了setTag/getTag方法,就像建議的here in this question一樣,並且工作正常。但是當我添加一個新元素到listView時,新元素的搜索欄會取得之前在其位置上的元素的進度。Android SeekBar在ListView中使用標籤
注意,我SeekBar sbAnalog
更新我的EditText etAnalogPosition
反之亦然
這裏是我的getView()
代碼:
public View getView(final int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_custom_list_layout, null);
holder = new ViewHolder();
holder.sbAnalog = (SeekBar) view.findViewById(R.id.sbAnalog);
holder.etAnalogPosition = (EditText) view.findViewById(R.id.tvAnalogPosition);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.sbAnalog.setTag(holder.etAnalogPosition);
holder.etAnalogPosition.setTag(holder.sbAnalog);
holder.sbAnalog.setMax(list.get(position).getMax());
holder.sbAnalog.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
EditText et = (EditText) seekBar.getTag();
et.setText(String.valueOf(i));
list.get(position).setPosition(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
holder.etAnalogPosition.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_DONE) {
int value = 0;
try {
value = Integer.parseInt(textView.getText().toString());
} catch (NumberFormatException e) {
SeekBar sb = (SeekBar)textView.getTag();
value = sb.getProgress();
}
//if (value > list.get(position).getMax())
//value = list.get(position).getMax();
SeekBar sb = (SeekBar) textView.getTag();
sb.setProgress(value);
textView.clearFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
textView.setText(String.valueOf(sb.getProgress()));
}
return false;
}
});}
幫助,謝謝。