在我的应用程序中,我有一个带有自定义项视图的ListView。每个Item都有一个自己的SeekBar,它保存ListView中显示的不同人的年龄。我的问题是我可以为每个人滚动SeekBar但是如果Item滚动出来并且下次将SeekBar重置为0.我的CustomAdapter看起来像这样:
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
PeopleHolder holder = null;
if(row == null)
{
LayoutInflater inflater = LayoutInflater.from(context);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new PeopleHolder();
final TextView tvAge = (TextView) row.findViewById(R.id.Age);
tvNote.setTag(position);
holder.age = (SeekBar) row.findViewById(R.id.agekBar);
holder.age.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar){ }
@Override
public void onStartTrackingTouch(SeekBar seekBar){ }
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
tvAge.setText("" + progress);
}
});
holder.name = (TextView) row.findViewById(R.id.Name);
row.setTag(holder);
}
else
{
holder = (PeopleHolder) row.getTag();
}
People p = data.get(position);
holder.name.setText(p.getFullName());
holder.age.setProgress(p.getAge());
return row;
}
private static class PeopleHolder
{
TextView name;
SeekBar age;
}
答案 0 :(得分:4)
确保在进度发生变化时更改此人的年龄值:
// adding a final int that can be accessed from inside the listener
final int personPosition = position;
holder.age.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
...
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
tvAge.setText("" + progress);
// change the age of the person in data
data.get(personPosition).setAge(progress);
}
});
答案 1 :(得分:0)
这是ListViews的工作方式 - 当视图从屏幕滚动时,它被销毁或回收以显示列表中的另一个条目。随着SeekBar的更改,您需要记下其他地方的值(例如,Activity或Fragment中的Map),并在每次创建视图时设置值。