简体   繁体   English

notifyDataSetChanged不刷新RecyclerView

[英]notifyDataSetChanged doesn't refresh RecyclerView

I have a weird problem. 我有一个奇怪的问题。 I switched to RecyclerView from ListView and I can't refresh or notify of change in my ListView . 我从ListView切换到RecyclerView ,我无法刷新或通知我的ListView的更改。 I tried calling Item.this.notifyDataSetChanged(); 我试着调用Item.this.notifyDataSetChanged(); and other methods to refresh View but it doesn't work. 和其他方法刷新View但它不起作用。 Instead RecyclerView is refreshed when I scroll(regardless of direction). 而是滚动时(无论方向如何)刷新RecyclerView How can I notify my RecyclerView when there is a change? 如何更改时如何通知我的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;
                }
            });




}

I've been struggling hard with a similar issue, trying to handle a use-case where all of the adapter's content has to be replaced and the recycler-view should start from scratch : calling notifyDataSetChanged() , swapAdapter() with numerous combinations of subsequent calls to view/layout-manager invalidation requests resulted in nothing but a (seemingly) empty recycler-view. 我一直在努力解决类似的问题,试图处理一个用例,其中所有适配器的内容都必须被替换,而notifyDataSetChanged() -view应该从头开始 :调用notifyDataSetChanged()swapAdapter()与多种组合对view / layout-manager失效请求的后续调用只会导致(看似)空的回收器视图。 The view didn't even try to rebind the view holders. 该视图甚至没有尝试重新绑定视图持有者。

What seemed to have worked it out is this hack-ish fix: 似乎已经解决的问题是这个黑客攻击修复:

view.swapAdapter(sameAdapter, true);
view.scrollBy(0, 0);

As it turns out, scrollBy (even with 0 offsets) drives the recycler-view into laying out its views and executing the pending view-holders rebinding. 事实证明, scrollBy (即使有0个偏移)驱动scrollBy -view进行布局,并执行挂起的视图持有者重新绑定。

if you want notify your recycleListView just simple call notifyDataSetChanged(); 如果你想通知你的recycleListView只是简单的调用notifyDataSetChanged(); in your class adapter. 在你的类适配器中。 this is my method in my adapter class : 这是我的适配器类中的方法:

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

   }
}

in your code you dont actually set or change your listObjects in the OnClickListener and please try notifyDataSetChanged(); 在您的代码中,您实际上没有在OnClickListener中设置或更改listObjects,请尝试notifyDataSetChanged(); instead of Item.this.notifyDataSetChanged(); 而不是Item.this.notifyDataSetChanged();

I am not sure why is this, but notifyDataSetChanged(); 我不知道为什么会这样,但是notifyDataSetChanged(); didn't work. 没用。 So I tried keeping track of changed items and refreshing them manually with notifyItemChanged(int); 所以我尝试跟踪更改的项目并使用notifyItemChanged(int);手动刷新它们notifyItemChanged(int); and so far it seems to be working. 到目前为止它似乎正在起作用。 I am still not sure why refreshing whole RecyclerView didn't work. 我仍然不确定为什么刷新整个RecyclerView不起作用。

Initialize your Adapter Again with changed data and use method 'swapadapter'. 使用更改的数据再次初始化适配器并使用'swapadapter'方法。 hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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