我有一个ViewPager,其中包含分配给他们的工单的不同人员。我正在尝试跟随主人/详细信息流与人作为查看寻呼机,主人是工作单的列表,详细信息是工单详细信息。
出于某种原因,当我切换视图寻呼机时,视图寻呼机适配器以及显示正确工单列表的主列表都会正确更新。但是,当我点击cardview时,它不会正确更新详细信息容器。有时,它会更新另一个视图页面上的不同详细信息容器。我知道主列表中的cardview正在正确注册,因为我有一个显示与详细信息相关的信息的Toast消息。
我似乎无法理解为什么偶尔会偶尔正确更新详细信息片段,然后大多数情况下它都不会并且会在另一个视图寻呼机中更新详细信息容器。
如果我没有提供足够的信息,请告诉我。
PagerAdapter:
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;
}
}
答案 0 :(得分:1)
您似乎使用嵌套片段(ViewPager
个网页作为PageFragment
个实例,其中这些片段保留在其他片段上,例如WorkOrderDetailFragment
)。
如果这是正确的,请不要使用getFragmentManager()
更新嵌套片段(例如WorkOrderDetailFragment
)。而是use getChildFragmentManager()
,调用外部片段(PageFragment
)。
就个人而言,我完全避免使用嵌套片段。例如,如果详细信息窗格(屏幕截图中的大红色方框)始终为WorkOrderDetailFragment
,请不要在那里使用片段。