滚动时更新自定义列表视图失败

时间:2018-11-03 19:30:11

标签: android listview

我有一个自定义的listView,并且listView的每个视图项中都有一个搜索栏和一个按钮。当用户单击列表视图行的按钮时,该项目的rowView被发送到类(voicePlayer),以更新rowView的搜索栏。该类通过如果我滚动视图变得不可见的listview来正确更新搜索栏,则停止搜索栏的进行(更新)。听起来好像视图的元素在滚动过程中获得了旧地址无效的新地址,即使不可见,我该如何更新视图的UI?

 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);
                    }
                });

            }

        }

voicePlayer

 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);

}

1 个答案:

答案 0 :(得分:0)

您需要使用最新的搜索栏位置更新您在recyclerview中维护的列表,这是由于recyclerview的工作方式,为了提高性能,它将绑定您的视图并在需要时取消绑定,在取消绑定数据期间该行将被清除,并且视图将使用从列表中获取的新数据进行回收,因此,如果列表未更新,您将看到较旧的数据。

更新列表,将更改通知更新程序,并且应该起作用。

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;
    }
}

您需要将最新的搜索栏位置保存在数据列表中,然后在getView中进行获取。即使将行从屏幕上滚动下来,这也可以让您更新并保持位置。