notifyDataSetChanged不刷新RecyclerView

时间:2015-12-05 12:25:45

标签: android android-recyclerview

我有一个奇怪的问题。我从RecyclerView切换到ListView,我无法刷新或通知ListView中的更改。我试着打电话给Item.this.notifyDataSetChanged(); 和其他方法刷新View但它不起作用。 而是滚动时(无论方向如何)刷新RecyclerView。如果有变更,我如何通知我的RecyclerView

CODE:

@Override
public void onBindViewHolder(Ids holder, final int position) {
    rowItemClass = (ListViewRow) rowItems.get(position);
    Log.e("swag", "OYOYOYOYOYO");
    if (Globals.isPlaying && Globals.pos == position) {

        if (pausedSamePos == true) {
            holder.pauseed_play.setVisibility(View.VISIBLE);
            holder.playing_pause.setVisibility(View.GONE);
        } else {
            holder.pauseed_play.setVisibility(View.GONE);
            holder.playing_pause.setVisibility(View.VISIBLE);
        }
        holder.song_currenttime_sb.setActive();
        holder.song_duration.setVisibility(View.INVISIBLE);
        holder.song_duration_sb.setVisibility(View.VISIBLE);
        holder.seekbar.setActive();

    } else {
        holder.seekbar.setInactive();
        holder.song_currenttime_sb.setInactive();
        holder.song_icon.setImageResource(rowItemClass.getImageId());
        holder.song_duration_sb.setVisibility(View.INVISIBLE);
        holder.song_duration.setVisibility(View.VISIBLE);
        holder.pauseed_play.setVisibility(View.GONE);
        holder.playing_pause.setVisibility(View.GONE);

    }
    sharedPreference = new SharedPreference();

    holder.song_duration.setTypeface(Globals
            .getTypefaceSecondary(context));
    holder.song_duration_sb.setTypeface(Globals
            .getTypefaceSecondary(context));
    holder.song_name.setTypeface(Globals.getTypefacePrimary(context));
    holder.song_currenttime_sb.setTypeface(Globals
            .getTypefaceSecondary(context));

    holder.song_name.setText(rowItemClass.getTitle());
    holder.song_duration.setText(rowItemClass.getDesc());

    holder.song_duration_sb.setText(rowItemClass.getDesc());
    holder.favorite.setTag(position);
    holder.song_currenttime_sb.setTag(position);
    holder.seekbar.setTag(position);
    holder.clickRegister.setTag(position);
    holder.song_icon.setTag(position);
    holder.song_name.setTag(position);
    holder.song_duration.setTag(position);
    holder.song_duration_sb.setTag(position);
    holder.more_options.setTag(position);
    // int task_id = (Integer) holder.seekbar.getTag();
    final Ids finalHolder = holder;





    holder.clickRegister.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {

                if ((Globals.isPlaying.booleanValue())
                        && (Globals.pos == position)) {
                    pausePlaying();

                } else {

                    Globals.stopPlaying();
                    pausedSamePos = false;
                    Globals.pos = position;
                    Globals.isPlaying = true;
                    Item.this.notifyDataSetChanged();

                    Globals.mp = MediaPlayer.create(context, Integer
                            .valueOf(Item.this.songPos[position])
                            .intValue());
                    Globals.mp.start();
                    Globals.pos = position;

                    Globals.isPlaying = Boolean.valueOf(true);
                    Item.this.notifyDataSetChanged();

                    Globals.mp
                            .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                @Override
                                public void onCompletion(
                                        MediaPlayer mpOnComplete) {
                                    mpOnComplete.release();

                                    Globals.isPlaying = false;
                                    pausedSamePos = false;
                                    Globals.isPlaying = Boolean
                                            .valueOf(false);
                                    finalHolder.menu_options
                                            .startAnimation(new ViewExpandAnimation(
                                                    finalHolder.menu_options));
                                    Item.this.notifyDataSetChanged();
                                }
                            });

                }
            } catch (Exception localException) {
            }

        }
    });

    holder.clickRegister
            .setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    Globals.stopPlaying();
                    Item.this.notifyDataSetChanged();
                    return true;
                }
            });




}

4 个答案:

答案 0 :(得分:7)

我一直在努力解决类似的问题,尝试处理一个用例,其中所有适配器的内容都必须被替换,而回收器视图应该从头开始:calling { {1}},notifyDataSetChanged()具有对视图/布局管理器失效请求的后续调用的多种组合,只会导致(看似)空的回收器视图。该视图甚至没有尝试重新绑定视图持有者。

似乎已经解决的问题是这个黑客攻击修复:

swapAdapter()

事实证明,view.swapAdapter(sameAdapter, true); view.scrollBy(0, 0); (即使有0个偏移)驱动Recycler-view进行布局,并执行待处理的视图持有者重新绑定。

答案 1 :(得分:2)

如果你想通知你的recycleListView只是简单的调用notifyDataSetChanged();在你的类适配器中。这是我的适配器类中的方法:

public class ListAdapter extends RecyclerView.Adapter<Listdapter.ViewHolder> {
....
private List<DesaObject> desaObjects= new ArrayList<DesaObject>();

public void setListObjects(List<DesaObject> desaObjects){
        if(this.desaObjects.size()>0)
            this.desaObjects.clear();
        this.desaObjects = desaObjects;
        notifyDataSetChanged();
    }
....

public static class ViewHolder extends RecyclerView.ViewHolder {
       public final View mView;
       public final AppCompatTextView desa;


       public ViewHolder(View view) {
           super(view);
           mView = view;
           desa = (AppCompatTextView) view.findViewById(R.id.desa);
       }

   }
}

在您的代码中,您实际上没有在OnClickListener中设置或更改listObjects,请尝试notifyDataSetChanged();而不是Item.this.notifyDataSetChanged();

答案 2 :(得分:2)

我不确定为什么会这样,但notifyDataSetChanged();没有用。因此,我尝试跟踪已更改的项目并使用notifyItemChanged(int);手动刷新它们,到目前为止它似乎正在运行。我仍然不确定为什么整个RecyclerView无法正常工作。

答案 3 :(得分:1)

使用更改的数据和使用方法&#39; swapadapter&#39;再次初始化您的适配器。希望它有所帮助。