我找不到问题,每当我滚动视图时都会改变。请帮助我...t认为android回收视图如何覆盖?
1.滚动 前 2.滚动后
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private LayoutInflater inflater;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
inflater = LayoutInflater.from(context);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView=convertView;
if (rowView == null) {
rowView = inflater.inflate(R.layout.list_mobile, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.label);
viewHolder.button = (ImageView) rowView
.findViewById(R.id.logo);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = values[position];
holder.text.setText(s);
if (mp.isPlaying()) {
holder.imageView.setImageResource(R.drawable.stop);
} else {
holder.imageView.setImageResource(R.drawable.play);
}
return rowView;
}static class ViewHolder {
public TextView text;
public ImageView button;
}
}
在主要活动中
protected void onListItemClick(ListView l, View v, int pos, long id) {
if(mp.isPlaying() && itemno == pos){
mp.pause();
playing= true;
ListMobileActivity.this. imageView.setImageResource(R.drawable.play);
}
else if (playing=true && itemno== pos){
mp.start();
playing=false;
ListMobileActivity.this.imageView.setImageResource(R.drawable.stop);
}
if (mp.isPlaying()&& itemno!=pos){
ListMobileActivity.this.imageView.setImageResource(R.drawable.play);
}
if (itemno!=pos)
{
imageView = (ImageView)v. findViewWithTag(pos);
playSample(pos);
}
ListMobileActivity.this.itemno=pos;
//Variable i, here, is from a for loop.
}
private void playSample( int position)
{
AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(songs[position]);
try{
if(mp.isPlaying())
ListMobileActivity. this.mp.stop();
ListMobileActivity. this.mp.reset();
ListMobileActivity. this.mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
ListMobileActivity. this.mp.prepare();
ListMobileActivity. this. imageView.setImageResource(R.drawable.stop);
ListMobileActivity.this. mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
ListMobileActivity. this. imageView.setImageResource(R.drawable.play);
}
});
ListMobileActivity. this. mp.start();
afd.close();
}
catch (IllegalArgumentException e)
{
Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
}
catch (IllegalStateException e)
{
Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
}
catch (IOException e)
{
Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
}
}