2015-08-09 59 views
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; 
       } 
      });} 

幫助,謝謝。

回答

0

我才意識到,最好的方式來解決是加入該兩行的代碼:

holder.sbAnalog.setProgress(list.get(position).getPosition()); 

holder.etAnalogPosition.setText(String.valueOf(list.get(position).getPosition())); 

注意,我的列表中的每個元素是我創建了一個自定義對象,其中之一有一個getPosition()方法基本上返回該元素的seekBar位置,由setPosition()方法設置。

所以,如果你有同樣的問題,我建議你做出這種方式,或者乾脆創建一個存儲每個元素的位置的arrayAdapter,這樣你就可以setget,就像我一樣即時通訊我的getView()碼。

希望它有幫助!