Android RecyclerView适配器notifyDataSetChanged无法正常工作

时间:2016-04-12 13:43:45

标签: android android-recyclerview recycler-adapter

我已经阅读了有关stackoverflow的几个问题,解决了这个问题并实现了解决方案,但我仍然无法更新项目。通过按菜单图标,可以调用searchString()。在这个方法里面我想更新不起作用的recyclerview项目。这是我的代码的相关部分:

    /* in my notation, pg is equivalent to page.*/
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_ebook_list, container, false);
    mEbookRecyclerView = (RecyclerView) view.findViewById(R.id.ebook_recycler_view);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mEbookRecyclerView.setLayoutManager(mLayoutManager);
    updateUI();
    return view;
}

@Override
public void onResume() {
    super.onResume();
    updateUI();
}


private void updateUI() {
    if (mAdapter == null) {
        EbookLab ebookLab = EbookLab.get(getActivity());
        List<String> pgs = ebookLab.getPgs();
        mAdapter = new EbookAdapter(pgs);
        mEbookRecyclerView.setAdapter(mAdapter);
    }
}

   /* ******codes for preparation of menus which I have ignored *******/

   private class EbookHolder extends RecyclerView.ViewHolder {
    private String mPg;

    public EbookHolder(LayoutInflater inflater, ViewGroup container) {
        super(inflater.inflate(R.layout.list_item_ebook, container, false));
        mListWebView = (WebView) itemView.findViewById(R.id.list_web_View);
        mListWebView.getSettings().setLoadWithOverviewMode(true);
    }

    public void bindEbook(String pg) {
        mPg = pg;
        mListWebView.loadDataWithBaseURL("file:///android_asset/", mPg, "text/html", "UTF-8", null);
    }
   }

   private class EbookAdapter extends RecyclerView.Adapter<EbookHolder> {
    private List<String> mPgs;
    public EbookAdapter(List<String> pgs) {
        mPgs = pgs;
    }
    @Override
    public EbookHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        return new EbookHolder(layoutInflater, parent);
    }


    @Override
    public void onBindViewHolder(EbookHolder holder, int position) {
        String pg = mPgs.get(position);
        holder.bindEbook(pg);
    }
    @Override
    public int getItemCount() {
        return mPgs.size();
    }
    public void setPgs(List<String> pgs) {
        mPgs.clear();
        mPgs.addAll(pgs);
        notifyDataSetChanged();
    }
}

   public void searchString(String qSrh) {
        EbookLab ebookLab = EbookLab.get(getActivity());
        List<String> pgs = ebookLab.getPgs();
    List<String> tmppgs = new ArrayList<String>(pgs);

   /** Some code for manipulation of tmppgs and updating recyclerview based on the tmppgs **/

   mAdapter.setPgs(tmppgs);
   }

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

我会尝试在View布局中添加一个显示mPg字符串的TextView(并绑定ViewHolder中的值)以确定问题所在。

如果显示项目字符串意味着您的setPgs正确更新RecyclerView,则问题出在WebView loadDataWithBaseURL部分中。

答案 1 :(得分:1)

尝试直接更新pgsList,然后调用notifyDataSetChanged

searchString功能删除 mAdapter.setPgs(tmppgs);

并直接更新mAdapter,

public void searchString(String qSrh) {
        EbookLab ebookLab = EbookLab.get(getActivity());
        List<String> pgs = ebookLab.getPgs();
        List<String> tmppgs = new ArrayList<String>(pgs);
//        mAdapter.setPgs(tmppgs);
        pgsList.clear();
        pgsList.addAll(tmppgs);
        mAdapter.notifyDataSetChanged();
    }

并更新updateUI()功能

       List<String> pgsList = new ArrayList<String>();
private void updateUI() {
    if (mAdapter == null) {
        EbookLab ebookLab = EbookLab.get(getActivity());
        pgsList = ebookLab.getPgs();
        mAdapter = new EbookAdapter(pgsList);
        mEbookRecyclerView.setAdapter(mAdapter);
    }
}