notifyDataSetChanged 不刷新 RecyclerView [英] notifyDataSetChanged doesn't refresh RecyclerView

查看:170
本文介绍了notifyDataSetChanged 不刷新 RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a weird problem. I switched to RecyclerView from ListView and I can't refresh or notify of change in my ListView. I tried calling Item.this.notifyDataSetChanged(); and other methods to refresh View but it doesn't work. Instead RecyclerView is refreshed when I scroll(regardless of direction). How can I notify my RecyclerView when there is a change?

代码:

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




}

推荐答案

我不知道为什么会这样,但是 notifyDataSetChanged(); 不起作用.所以我尝试跟踪更改的项目并使用 notifyItemChanged(int); 手动刷新它们,到目前为止它似乎正在工作.我仍然不确定为什么刷新整个 RecyclerView 不起作用.

I am not sure why is this, but notifyDataSetChanged(); didn't work. So I tried keeping track of changed items and refreshing them manually with notifyItemChanged(int); and so far it seems to be working. I am still not sure why refreshing whole RecyclerView didn't work.

这篇关于notifyDataSetChanged 不刷新 RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆