简体   繁体   中英

update custom listview failed while scrolling

I have a custom listView and in every view item of the listView is a seekbar and a button. when the user click on a button of a row of listview the rowView of the item is sent to a class( voicePlayer ) for update the seekbar of the rowView. the class updates the seekbar correctly by if i scroll the listview that the view gets invisible, the progressing(update) of the seekbar is stopped. sounds like the elements of the view obtain new address during the scroll that the old ones are invalid, How should i update the UI of view even when thay are invisible?

 public View getView(final int position, final View view, ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    rowView=inflater.inflate(R.layout.voice_player_listview, null,true);
    final SeekBar voice_player_seekbar =(SeekBar) rowView.findViewById(R.id.voice_player_seekbar);
    final ImageView icon = (ImageView) rowView.findViewById(R.id.download_play_icon);
    icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) 
                    {
                        new voicePlayer(voice_id.get(position), rowView);
                    }
                });

            }

        }

 class voicePlayer {
 MediaPlayer mediaPlayer;
 SeekBar seekbar;
 TextView voice_duration;
 ImageView icon;
 WavesAPI.viewHolder rowView;
 final Handler handler = new Handler();
public void getViews()
{
    this.seekbar= (seekbar) rowView.findViewById ...
    this.voice_duration=(TextView) rowView.findViewById ...
    this.icon=(ImageView) rowView.findViewById ...
}
public voicePlayer(String voice_id,WavesAPI.View rowView) {
    this.rowView=rowView;
    getViews();
                /*mediaPlayer=new MediaPlayer();
                 ... play the voice

                */

                    this.icon.setImageResource(R.drawable.pause);

                    this.updateSeekbar();


                } catch (IOException e) {

                }

            } catch (IOException e) {
            }

    }


}
private void updateSeekbar()
{

    final Runnable runnable = new Runnable() {
        public void run() {
            int position=// calculate the position of the seekbar
            if (mediaPlayer.isPlaying())
            {
                seekbar.setProgress(position);
                handler.postDelayed(this, 50);
                voice_duration.setText("duration ...");
            }
        }
    };

    handler.post(runnable);

}

The list that you maintain in the recyclerview needs to be updated with the latest seekbar position, this is because of the way the recyclerview works, for performance it will bind your view and unbind it as and when needed, during unbind the data for that row is cleared and the view is recycled with new data which it fetches from the list, so if the list is not updated you will see older data.

Update the list, notify the change to the updater and that should work.

private class Adapter extends ArrayAdapter<Data> {

    List<Data> dataList;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

       CustomData data =  dataList.get(position);

       // existing code

       int currentSeekarProgress = data.getSeekbarProgress();
       seekBar.setProgress(currentSeekarProgress);

        return convertView;
    }
}

You would need to save the latest seekbar position in your data list and then fetch it in getView . This would allow you to update and persist the position even when the row is scrolled off the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM