我有一个fragment类作为mainActivity类的内部类,并且在这里定义了RecyclerView,并将其附加到SlidingTabLayout上。我正在请求一些JSON数据,并且应该更新此RecyclerView。当我在mainActivity类中的函数之一中解析JSON数据后调用ListingsFragment.mAdapter.notifyDataSetChanged()时,没有任何反应:(在我的getData()中,我从对象的ArrayList将数据加载到RecyclerView,在较早解析JSON结果之后,我将数据复制到了该对象。任何帮助或思想表示赞赏。谢谢。这是我的片段课 public static class ListingsFragment extends Fragment { private RecyclerView mListRecyclerView; private static ListAdapter mAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.listings_fragment, container, false); mListRecyclerView = (RecyclerView) view.findViewById(R.id.listing_recyclerView); mListRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()) .marginResId(R.dimen.leftmargin, R.dimen.rightmargin) .build()); mAdapter = new ListAdapter(getActivity(), getData()); mListRecyclerView.setAdapter(mAdapter); mListRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); return view; } 最佳答案 再用setAdapter代替notifyDataSetChanged怎么样?更新:据我所知,这不仅是一种解决方法,可能是必要的。调查了RecyclerView的数据逻辑实现private class RecyclerViewDataObserver extends AdapterDataObserver {@Overridepublic void onChanged() { assertNotInLayoutOrScroll(null); if (mAdapter.hasStableIds()) { // TODO Determine what actually changed. // This is more important to implement now since this callback will disable all // animations because we cannot rely on positions. mState.mStructureChanged = true; setDataSetChangedAfterLayout(); } else { mState.mStructureChanged = true; setDataSetChangedAfterLayout(); } if (!mAdapterHelper.hasPendingUpdates()) { requestLayout(); }}@Overridepublic void onItemRangeChanged(int positionStart, int itemCount) { assertNotInLayoutOrScroll(null); if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount)) { triggerUpdateProcessor(); }}....布局更改取决于AdapterHelper的UpdateOp队列,但是与onItemRangeInserted不同,onChanged实际上没有在队列中放置任何UpdateOP,我想这还没有实现(应该写在并将总更改拆分为原子更新)。就个人而言,在// TODO Determine what actually changed.与notifyDataSetChanged或setAdapter之间确实没有太大的性能差异,因为应该完成几乎相同数量的UI渲染工作(与onMeasure相比,对象实例开销不大)。swapAdapter专为卡式单次操作而设计,因此最好弄清楚实际更改的内容并以此方式执行RecyclerView其他一些示例也使用notifyItemRangeChanged(int, int)像这样:http://javatechig.com/android/android-recyclerview-example
08-18 00:49