我在ListView中遇到了SeekBar和TextView的问题。当用户移动SeekBar时,它会很好用(当SeekBar进度在同一行中发生变化时,它会更新相应行中的TextView)。
问题在于ListView回收视图。快速滚动时,所有行都变得疯狂。我在ViewHolder中找到了一些解决方案并试图实现它,但我没有成功(它仍然无法工作)。
这是扩展BaseAdapter的自定义适配器
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SimpleListAdapter extends BaseAdapter{
//private TextView tvDrink, tvPrice, tvQuantity;
//private ImageView imgV;
//private SeekBar seeker;
private List<Menu> list;
private ViewHolder holder;
private LayoutInflater inflater;
private Activity activity;
public SimpleListAdapter(Activity a, ArrayList<Menu> list){
this.activity = a;
this.list = list;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
v = inflater.inflate(R.layout.simple_item, null);
holder = new ViewHolder();
holder.tvDrink = (TextView) v.findViewById(R.id.drink);
holder.tvPrice = (TextView) v.findViewById(R.id.price);
holder.seeker = (SeekBar) v.findViewById(R.id.seek);
//imgV = (ImageView) v.findViewById(R.id.image);
v.setTag(holder);
}else{
holder = (ViewHolder) v.getTag();
}
Menu m = list.get(position);
holder.tvDrink.setText(m.getDrink());
holder.tvPrice.setText(m.getPrice());
holder.seeker.setOnSeekBarChangeListener(new SeekerListener());
return v;
}
private class SeekerListener implements OnSeekBarChangeListener{
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
View view = (View) seekBar.getParent().getParent();
holder.tvQuantity = (TextView) view.findViewById(R.id.quantitiy);
holder.tvQuantity.setText(""+progress);
// seeker.setTag(tvQuantity);
// tvQuantity.setTag(seeker);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//notifyDataSetChanged();
}
}
private class ViewHolder{
TextView tvDrink;
TextView tvPrice;
TextView tvQuantity;
SeekBar seeker;
}
}
P.S。稍后我将添加ImageView,但由于我使用外部库来处理listview中的图像缓存,所以我没有遇到任何问题。