2014-03-02 53 views
1

在我的應用我有一個自定義的項目一個ListView看起來像這樣:互動與ListView的項目

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" > 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical" 
     android:minWidth="60dp" 
     android:text="Name" /> 

    <SeekBar 
     android:id="@+id/seekBar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:max="100" /> 
</LinearLayout> 

現在我想的是,TextView的具有永諾搜索欄的進展,但我不知道該怎麼與ListViewItems交互。當然,我知道我必須創建一個OnSeekBarChangeListener並調用類似txt.setText("" + bar.getProgress());但我應該在哪裏創建監聽器,以及我如何確定選中了正確的TextView,因爲所有項都具有相同的具有相同LayoutID的佈局。這裏是我的自定義適配器摘錄:

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

    if(row == null) 
    { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     row = inflater.inflate(layoutResourceId, parent, false); 

     holder = new Holder(); 
     holder.bar = (SeekBar) row.findViewById(R.id.seekBar); 
     holder.txt = (TextView) row.findViewById(R.id.textView); 

     row.setTag(holder); 
    } 
    else 
    { 
     holder = (Holder) row.getTag(); 
    } 

    Person p = data.get(position); 

    holder.bar.setProgress(0); 
    holder.txt.setText(p.getFullName()); 

    return row; 
} 

private static class Holder 
{ 
    SeekBar bar; 
    TextView txt; 
} 
+2

嘗試是這樣的: 搜索欄搜索欄=(搜索欄)row.findViewById (R.id.seekBar); seekBar.setYourListenersHere(yourListener); holder.bar = seekBar; –

+0

但是它可能會出現這樣的問題,我觸摸Item1中的SeekBar,但持有者設置爲Item3,並且Item3中的TextView將發生更改。 – Cilenco

+0

它不應該發生,只是試試看。希望這會有所幫助:) –

回答

1

試試這個:

if(row == null) { 
    LayoutInflater inflater = LayoutInflater.from(context); 
    row = inflater.inflate(layoutResourceId, parent, false); 

    holder = new Holder(); 

    final TextView textView = (TextView) row.findViewById(R.id.textView); 
    holder.txt = textView; 

    SeekBar seekBar = (SeekBar) row.findViewById(R.id.seekBar); 
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
     // Overridden methods 
     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 
      // textView will be accessible here since its final 
      // and also in other overridden methods 
     } 
    }); 
    holder.bar = seekBar; 

    row.setTag(holder); 
} 

希望這有助於:)