列表视图中带有回收/ convertView行的重复视图实例问题

时间:2018-11-06 11:13:45

标签: android android-studio listview android-listview

value

inputs


MyAdapter:

public class voice_player_listview_adapter extends ArrayAdapter<String> {
public  ArrayList<String> Duration=null;
public  ArrayList<String> voice_id=null;
private Activity context;
public voice_player_listview_adapter(Activity context,ArrayList<String> voice_id,ArrayList<String> Duration) {
    super(context, R.layout.voice_player_listview,sender);
    // TODO Auto-generated constructor stub
    this.context=context;
    this.voice_id=voice_id;
    this.Duration=Duration;
}
public View getView(final int position, final View convertView, ViewGroup parent) {
    View rowView=convertView;
    final viewHolder holder; //viewHolder already created in some where
    if (rowView==null)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.voice_player_listview, parent, false);
        holder= new viewHolder();
        holder.icon=(ImageView) rowView.findViewById(R.id.download_play_icon);
        holder.voice_duration=(TextView) rowView.findViewById(R.id.voiceDuration);
        holder.voice_player_seekbar=(SeekBar) rowView.findViewById(R.id.voice_player_seekbar);
        rowView.setTag(holder);
    }
       else
    {
        holder= (viewHolder) convertView.getTag();
    }
    holder.voice_duration.setText("00:"+this.Duration.get(position));
    holder.icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new voicePlayer(voice_id.get(position),holder);

                }
            });

            ...


问题:当我单击列表视图行上的播放VoicePlayer:时,只要我不滚动,音乐就会播放,并且搜索栏会正确更新。但是,由于屏幕/列表视图仅适合3个项目,所以当我向下滚动时(每4行),我看到新行的搜索栏正在更新,与我播放并滚动的旧行的进度相同。我认为class voicePlayer { MediaPlayer mediaPlayer; SeekBar seekbar; TextView voice_duration; ImageView icon; viewHolder rowView; final Handler handler = new Handler(); public void getViews() { this.seekbar=rowView.voice_player_seekbar; this.voice_duration=rowView.voice_duration; this.icon=rowView.icon; } public voicePlayer(String voice_id,WavesAPI.viewHolder holder) { this.rowView=holder; getViews(); try { mediaPlayer=new MediaPlayer(); FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory()...); mediaPlayer.setDataSource(fis.getFD()); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.prepare(); mediaPlayer.start(); this.icon.setImageResource(R.drawable.pause); this.updateSeekbar(); this.seekbar_listener(); } catch (IOException e) { } } catch (IOException e) { } } } private void updateSeekbar() { final Runnable runnable = new Runnable() { public void run() { int position= // calculating the position of the mediaPlayer for updating the seekbar seekbar.setProgress(position); handler.postDelayed(this, 50); voice_duration.setText("00:"+((WavesAPI.player_data.mediaPlayer.getDuration()/1000)-(WavesAPI.player_data.mediaPlayer.getCurrentPosition()/1000))); } }; handler.post(runnable); } public void seekbar_listener() { seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { int player_position=// calc new postion of the mediaPlayer mediaPlayer.seekTo(player_position); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } } 会生成与创建并传递给icon相同的旧实例视图。一旦我不考虑convertView并为每个voicePlayer创建一个新的convertView的rowView,就解决了重复实例的问题,但是当我向下滚动一个项目并重新滚动回项,更新已停止。

1 个答案:

答案 0 :(得分:0)

问题解决了。我用ScrollView代替了ListView