我有一个ListFragment
显示用户创建的项目列表。我有一个ListView
,它的id设置为“@android:id/list”,TextView
的ID为“@android:id/empty”。FragmentTransaction.replace()不起作用
我有一个操作栏按钮来显示一个片段,以允许用户为列表创建更多的条目。这里是onOptionsItemSelected()
方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Fragment frag = null;
// Right now I only have code for the addCourse() fragment, will add more soon
switch (item.getItemId()) {
case R.id.addCourse:
frag = new AddCourseFragment();
break;
default:
return super.onOptionsItemSelected(item);
}
// The support library is being dumb and replace isn't actually
getFragmentManager().beginTransaction()
.remove(this).add(getId(), frag).addToBackStack(null).commit();
return true;
}
的AddCourseFragment代码如下:
public class AddCourseFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_addcourse, container, false);
// if (view.findViewById(R.id.timesFrame)!=null) {
Fragment timesFrag = new TimesFragment();
getChildFragmentManager().beginTransaction()
.add(R.id.timesFrame, timesFrag).commit();
// }
return view;
}
}
正如预期的那样,当列表是无人居住的,Android的显示在TextView
文本。然后我点击添加课程按钮,发生这种情况:
它显示空文本以及新的片段。