与ListView项目交互

时间:2014-03-02 12:37:33

标签: android android-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始终具有SeekBar的进度,但我不知道如何与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;
}

1 个答案:

答案 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);
}

希望这会有所帮助:)