recyclerview adapter notifydatasetchanged不起作用

时间:2016-01-05 15:17:23

标签: android

当我只使用imageRecyclerViewAdapter.notifyDataSetChanged();时,它无论如何都不起作用。

imageRecyclerViewAdapter = new ImageRecyclerViewAdapter(MainActivity.this,imagesList);
recyclerView.setAdapter(imageRecyclerViewAdapter);

如果我这样做,那将是有效的。

所以我应该怎么做。

我的代码如下。

public class ImageRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{


public static final int TYPE_ITEM = 0;
public static final int TYPE_FOOTER = 1;
private boolean hasFooter;//设置是否显示Footer
private boolean hasMoreData;//设置是否可以继续加载数据
private List<ImageFuli> images;
private Context context;

public ImageRecyclerViewAdapter(Context context, List<ImageFuli> images) {
    this.images = images;
    this.context = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if(viewType == TYPE_FOOTER){//底部 加载view
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.footer_progress, parent, false);
        return new FooterViewHolder(view);
    } else {
        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_list, null);
        ImageViewHolder ivh = new ImageViewHolder(layoutView);
        return ivh;
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if(holder instanceof FooterViewHolder) {
        if(hasMoreData){
            ((FooterViewHolder) holder).mProgressView.setVisibility(View.VISIBLE);
        } else {
            ((FooterViewHolder) holder).mProgressView.setVisibility(View.GONE);
        }
    } else {
        ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
        ImageFuli imageFuli= images.get(position);
        imageViewHolder.imageView.setOriginalSize(imageFuli.getWidth(), imageFuli.getHeight());
        Glide.with(context)
                .load(imageFuli.getUrl())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageViewHolder.imageView);
        ViewCompat.setTransitionName(imageViewHolder.imageView,imageFuli.getUrl());
    }

}

@Override
public int getItemCount() {
    return this.images.size() + (hasFooter ? 1 : 0);
}

@Override
public int getItemViewType(int position) {

    if (position == getBasicItemCount() && hasFooter) {
        return TYPE_FOOTER;
    }
    return TYPE_ITEM;//0
}

private int getBasicItemCount() {
    return images.size();
}

public boolean hasFooter() {
    return hasFooter;
}

public void setHasFooter(boolean hasFooter) {
    if(this.hasFooter != hasFooter) {
        this.hasFooter = hasFooter;
        notifyDataSetChanged();
    }
}


public boolean hasMoreData() {
    return hasMoreData;
}

public void setHasMoreData(boolean isMoreData) {
    if(this.hasMoreData != isMoreData) {
        this.hasMoreData = isMoreData;
        notifyDataSetChanged();
    }
}
public void setHasMoreDataAndFooter(boolean hasMoreData, boolean hasFooter) {
    if(this.hasMoreData != hasMoreData || this.hasFooter != hasFooter) {
        this.hasMoreData = hasMoreData;
        this.hasFooter = hasFooter;
        notifyDataSetChanged();
    }
}


public  class ImageViewHolder extends RecyclerView.ViewHolder {
    private RatioImageView imageView;
    public ImageViewHolder(View itemView) {
        super(itemView);
        imageView = (RatioImageView) itemView.findViewById(R.id.image);
    }
}
public  class FooterViewHolder extends RecyclerView.ViewHolder {
    public final View mProgressView;

    public FooterViewHolder(View view) {
        super(view);
        mProgressView = View.inflate(context,R.layout.footer_progress, null);
    }
}

}

和mainActivity

  public class MainActivity extends AppCompatActivity {

private StaggeredGridLayoutManager gaggeredGridLayoutManager;
private List<ImageFuli> imagesList = new ArrayList<ImageFuli>();
private ImageRecyclerViewAdapter imageRecyclerViewAdapter;
private RecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
private boolean onLoading = false;
private int loadImageCount = 15;
private int loadTimes=1;

@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    init();

    getImagesDataFromHttp();

}

private void init() {
    recyclerView = (RecyclerView)findViewById(R.id.content);
    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresher);
    swipeRefreshLayout.setColorSchemeColors(R.color.colorPrimary);

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            getImagesDataFromHttp();
        }
    });

    recyclerView.setHasFixedSize(true);

    imageRecyclerViewAdapter = new ImageRecyclerViewAdapter(MainActivity.this, imagesList);
    recyclerView.setAdapter(imageRecyclerViewAdapter);
    gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
    recyclerView.setLayoutManager(gaggeredGridLayoutManager);

    swipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            swipeRefreshLayout.setRefreshing(true);
        }
    });
}

private void getImagesDataFromHttp() {
    HttpUtil.httpRequestToGank(ApiUrl.gankApiUrl+(loadImageCount+5*loadTimes)+"/1" , new HttpUtil.HttpUtilCallBack() {
        @Override
        public void onFinsh(String response) {
            try {
                imagesList = ResponseHandleUtil.HandleResponseFromHttp(response, MainActivity.this);
                loadTimes++;
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
//                        imageRecyclerViewAdapter = new ImageRecyclerViewAdapter(MainActivity.this, imagesList);
//                        recyclerView.setAdapter(imageRecyclerViewAdapter);
                    //imageRecyclerViewAdapter.notifyDataSetChanged();
                    recyclerView.getAdapter().notifyDataSetChanged();
                    imageRecyclerViewAdapter.setHasFooter(false);

                    if (swipeRefreshLayout.isRefreshing()) {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }
            });

        }

        @Override
        public void onError(Exception e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, "加载失败!", Toast.LENGTH_SHORT).show();
                    if (swipeRefreshLayout.isRefreshing()) {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }
            });

        }
    });

}

}

2 个答案:

答案 0 :(得分:0)

获取recyclerview的适配器

mRecyclerView.getAdapter().notifyDataSetChanged();

答案 1 :(得分:0)

因为我更改了图像列表的引用.so adapter.notifyDataSetChanged不能再工作了。