Fragment里面的RecyclerView没有更新

时间:2017-02-09 14:31:19

标签: java android android-recyclerview fragment

在我的应用程序中,我有一个ViewPager,其中包含5个片段,每个片段中都有一个RecyclerView。点击按钮后,我需要刷新RecyclerView项的内容,以便我向服务器发送请求并在我的活动中获得回复,然后我为所有人调用notifyDataSetChanged(); RecyclerView,但在我刷新ViewPager的2-3页之前,更改不可见。怎么了?这是我的应用程序中的一些代码

片段:

public class ReservedFragment extends Fragment {
List<ReservedTimesModel> arrayList;
ReservedTimesAdapter reservedTimesAdapter;
RecyclerView recyclerView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_reserved, container, false);
    recyclerView = (RecyclerView) view.findViewById(R.id.fragment_recycle);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    if (arrayList != null) {
        if (!arrayList.isEmpty()) {
            reservedTimesAdapter = new ReservedTimesAdapter(arrayList, getActivity());
            recyclerView.setAdapter(reservedTimesAdapter);
        } else {
            view.findViewById(R.id.textview).setVisibility(View.VISIBLE);
        }
    } else {
        view.findViewById(R.id.textview).setVisibility(View.VISIBLE);
    }
    return view;
}

public void refresh() {
    if (reservedTimesAdapter != null) {
        if (arrayList.isEmpty()) {
            getView().findViewById(R.id.textview).setVisibility(View.VISIBLE);
        } else {
            reservedTimesAdapter.notifyDataSetChanged();
        }
    }
}

适配器中的Button点击侦听器内的代码:

context.getReserved();
在为每个片段设置新列表后,

活动刷新中的代码:

viewPager.setVisibility(View.VISIBLE);
reservedFrag.refresh();
deprecatedFrag.refresh();
cancelledFrag.refresh();
cancelledByAdminFrag.refresh();
pendingFrag.refresh();
viewPager.setCurrentItem(4);

1 个答案:

答案 0 :(得分:1)

根据变量值有条件地调用notifyDataSetChanged。也许根本没有被召唤。

由于代码非常小,很难确定。