android - notifyDataSetChanged 不刷新 RecyclerView

标签 android android-recyclerview

我有一个奇怪的问题。我从 ListView 切换到 RecyclerView,但我无法刷新或通知我的 ListView 中的更改。我尝试调用 Item.this.notifyDataSetChanged(); 和其他刷新 View 的方法,但它不起作用。 当我滚动时(无论方向如何),RecyclerView 都会刷新。当发生变化时,如何通知我的 RecyclerView

代码:

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




}

最佳答案

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

似乎解决了这个 hack-ish 修复:

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

事实证明,scrollBy(即使偏移量为 0)驱动回收器 View 布局其 View 并执行挂起的 View 持有者重新绑定(bind)。

关于android - notifyDataSetChanged 不刷新 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34104870/

相关文章:

javascript - Cookies 在使用 PHONEGAP 的 IPhone IOS 和 ANDROID 上工作吗?

android - 小于 4MB 的即时应用程序

android - 在 RecyclerView 中将 CardView 居中,只有一个元素

android - 选择选项后如何关闭我的 inflater 菜单

android - Play 商店 - 准备发布状态

android - 滚动 AppBarLayout 内部的 RecyclerView 在滚动外部 RecycleView 时禁用滚动滞后

android - 更改 View 在 recyclerview 的所有项目中的可见性

android - scrollview android 中 recyclerview 的滚动事件

android - RecyclerView ViewPager java.lang.NullPointerException android.support.v7.widget.RecyclerView$ItemAnimator$ItemHolderInfo.left

android - stub 、mockito "mock"类和 Robolectric "shadow"对象之间的区别