ViewPager 没有更新正确的细节

ViewPager Not Updating Correct Detail

我有一个 ViewPager,里面装满了不同的人,并向他们分配了工单。我正在尝试按照 master/detail 人流作为视图寻呼机,master 是工单列表,detail 是工单详细信息。

出于某种原因,当我切换 viewpager 时,view pager 适配器会正确更新,主列表也会显示正确的工单列表。但是当我点击卡片视图时,它没有正确更新详细信息容器。有时,它会更新另一个视图页面上的不同详细信息容器。我知道主列表中的 cardview 注册正确,因为我有一条 toast 消息显示与详细信息相关的信息。

我似乎无法理解为什么它偶尔会偶尔正确地更新详细信息片段,然后大多数时候不会更新另一个视图分页器中的详细信息容器。

如果我没有提供足够的信息,请告诉我。

寻呼机适配器:

public class WorkOrderPagerAdapter extends FragmentPagerAdapter {

private Context context;
private String tabTitles[];
private String employeeIds[];
private int numOfEmployees;
private Map<String, Employee> employees;

public WorkOrderPagerAdapter(FragmentManager fm, Context cont, Map<String, Employee> allEmployees) {
    super(fm);
    context = cont;
    employees = allEmployees;

    // Construct the Tab Titles from all Employees
    employeeIds = employees.keySet().toArray(new String[employees.keySet().size()]);
    numOfEmployees = employeeIds.length;
    tabTitles = new String[numOfEmployees];

    for (int i =0; i < numOfEmployees; i++) {
        tabTitles[i] = employees.get(employeeIds[i]).getDisplayName();
    }
}

@Override
public int getCount() {return numOfEmployees;}

@Override
public Fragment getItem(int position) {

    return PageFragment.newInstance(employees.get(employeeIds[position]));
}

@Override
public CharSequence getPageTitle(int position) {
    return tabTitles[position];
}

}

工单回收器视图适配器:

public class WorkOrderRVAdapter extends RecyclerView.Adapter<WorkOrderViewHolder> {

public static final String WORK_ORDER = "workorders.workorder";
private ArrayList<WorkOrder> workOrders;
private Activity mContext;
private View.OnClickListener clickListener;

public WorkOrderRVAdapter(Activity context, ArrayList<WorkOrder> works) {
    mContext = context;
    workOrders = works;

}

@Override
public int getItemCount() {
    return workOrders.size();
}

@Override
public WorkOrderViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.work_order_card_view, viewGroup, false);

    clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            WorkOrderViewHolder holder = (WorkOrderViewHolder) v.getTag();
            int position = holder.getAdapterPosition();

            WorkOrder workOrder = workOrders.get(position);
            Toast.makeText(mContext, workOrder.getWorkOrderId(), Toast.LENGTH_LONG).show();

            WorkOrderDetailFragment workFragment = new WorkOrderDetailFragment();
            Bundle args = new Bundle();
            args.putParcelable(WorkOrderRVAdapter.WORK_ORDER, workOrders.get(position));
            workFragment.setArguments(args);
            mContext.getFragmentManager().beginTransaction().replace(R.id.work_order_detail_frame, workFragment).commit();
        }
    };

    return new WorkOrderViewHolder(v);
}

@Override
public void onBindViewHolder(final WorkOrderViewHolder workOrderViewHolder, int i) {
    workOrderViewHolder.workOrderId.setText(workOrders.get(i).getWorkOrderId());
    workOrderViewHolder.workType.setText(workOrders.get(i).getWorkOrderTask());
    workOrderViewHolder.workAddress.setText(workOrders.get(i).getLocation());

    // Ternary on priority star
    workOrderViewHolder.priorityStar.setImageResource(
            (workOrders.get(i).getPriority() < 1) ?
                    R.mipmap.unselected_star : R.mipmap.selected_star);

    workOrderViewHolder.cv.setOnClickListener(clickListener);
    workOrderViewHolder.cv.setTag(workOrderViewHolder);

}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

}

工单详细信息片段:

public class WorkOrderDetailFragment extends Fragment {

private WorkOrder workOrder;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments().containsKey(WorkOrderRVAdapter.WORK_ORDER)) {
        workOrder = getArguments().getParcelable(WorkOrderRVAdapter.WORK_ORDER);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.work_order_detail, container, false);
    if (workOrder != null) {
        ((TextView)rootView.findViewById(R.id.work_id)).setText(workOrder.getWorkOrderId());
    }

    return rootView;
}

}

编辑 添加页面片段

页面片段:

public class PageFragment extends Fragment{

public static final String ARG_PAGE = "ARG_PAGE";
private ArrayList<WorkOrder> workOrders;

public static PageFragment newInstance(Employee emp) {
    Bundle args = new Bundle();
    if (emp != null) {
        args.putSerializable(ARG_PAGE, emp.getWorkOrders());
    } else {
        args.putSerializable(ARG_PAGE, new ArrayList<WorkOrder>());
    }
    PageFragment fragment = new PageFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // noinspection unchecked
    workOrders = (ArrayList)getArguments().getSerializable(ARG_PAGE);
    getArguments().remove(ARG_PAGE);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_page, container, false);

    RecyclerView workOrdersRV = (RecyclerView)view.findViewById(R.id.work_order_rv);
    workOrdersRV.setHasFixedSize(true);
    StaggeredGridLayoutManager workOrdersLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
    workOrdersRV.setLayoutManager(workOrdersLayoutManager);

    WorkOrderRVAdapter adapter = new WorkOrderRVAdapter(getActivity(), workOrders);
    workOrdersRV.setAdapter(adapter);

    return view;
}

}

您似乎在使用嵌套片段(ViewPager 页作为 PageFragment 实例,其中这些片段包含其他片段,例如 WorkOrderDetailFragment)。

如果正确,请不要使用 getFragmentManager() 更新嵌套片段(例如 WorkOrderDetailFragment)。相反,use getChildFragmentManager() 调用外部片段(PageFragment)。

就个人而言,我完全避免嵌套片段。例如,如果详细信息窗格(屏幕截图中的红色大方块)始终是 WorkOrderDetailFragment,请不要在那里使用片段。