为了不重复代码我在我的程序中决定创建一个BaseFragment
(扩展Fragment
)以保留一些样板数据并提供正确的暂停/恢复处理,以及newInstance(String myPram)
方法。简而言之:BaseFragment的作用是确保保存和恢复状态,即将myPramas恢复为局部变量......
我接下来要做的是扩展BaseFragment
以便为两个不同的片段处理两种不同类型的布局(每个需要处理MyParam归档):EditFragment
和DetailFragment
。
问题现在出现了,因为没有View为Fragment膨胀,尽管它们被添加为一个事务(因此它们是透明的!)。
EditFragment
和DetailFragment
都提供onCreateView()
方法,为了构建自己的视图,我是正确的,因为我设法创建它们而没有“子类化机制”,当然BaseFragment没有提供onCreateView()
的实现,但没有结果,它们没有绘制UI。
我尝试在这里和那里放置一些super.onCreateView(),但没有结果......我做错了什么? 这是基础片段:
public class TimetableBaseFragment extends Fragment {
public static String ID_FIELD = "id";
public static String ID_DESC = "desc";
private String id;
private String desc;
// Constructor to deliver previous position along with itself
public static Fragment newInstance(String id, String desc) {
TimetableBaseFragment f = new TimetableBaseFragment();
Bundle args = new Bundle();
args.putString(ID_FIELD, id);
args.putString(ID_DESC, desc);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
id = args.getString(ID_FIELD);
desc = args.getString(ID_DESC);
}
if (savedInstanceState != null) {
id = savedInstanceState.getString(ID_FIELD);
desc = savedInstanceState.getString(ID_DESC);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ID_FIELD, id);
outState.putString(ID_DESC, desc);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
id = savedInstanceState.getString(ID_FIELD);
desc = savedInstanceState.getString(ID_DESC);
}
}
public String getExamId() {
return id;
}
public String getExamDesc() {
return desc;
}
}
我想补充的是:
public class TimetableEditFragment extends TimetableBaseFragment {
private LinearLayout mLlEditEmpty;
private ArrayList<ScheduleUser> lessons;
private TimetableEditRecyclerViewAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lessons = new ArrayList<>();
mAdapter = new TimetableEditRecyclerViewAdapter(getActivity());
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_timetable_edit, container, false);
mLlEditEmpty = (LinearLayout) rootView.findViewById(R.id.LOGIC_empty);
RecyclerView mRvEdit = (RecyclerView) rootView.findViewById(R.id.HEADER_rv);
mRvEdit.setHasFixedSize(true);
mRvEdit.setLayoutManager(new LinearLayoutManager(getActivity()));
mRvEdit.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
mRvEdit.setAdapter(mAdapter);
Toolbar examDesc = (Toolbar) rootView.findViewById(R.id.HEADER_examDesc);
examDesc.setTitle("TOOLBAR Testing");
FloatingActionButton add_fab = (FloatingActionButton) rootView.findViewById(R.id.EDIT_fab);
add_fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addLesson(new ScheduleUser());
}
});
return rootView;
}
public void addLesson(ScheduleUser newItem) {
lessons.add(newItem);
mAdapter.notifyItemInserted(lessons.size() - 1);
}
public class TimetableEditRecyclerViewAdapter extends RecyclerView.Adapter<TimetableEditRecyclerViewAdapter.ViewHolder> {
/* Lots of stuff */
}
}
PS:我注意到,很可能孩子onCreateView()根本没有被调用,因为我有一个错误,这是由于在运行时提供资金,但是系统没有通知。
请帮忙!