我有一个视图寻呼机,在一个标签中我有父片段,其中包含两个子片段。
候选列表是具有框架容器的父片段我替换了该容器内的一个子项( candidatelistfragment )但是当我更换了一个子项(候选事件开始)时它说没有找到视图。
我用来替换父片段容器中的第一个子代码的代码是(这很好用)
Fragment childFragment = new CandidateListFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.childContainer, childFragment,"listfragment").commit();
这里childContainer是父容器的ID,现在当我必须在其中添加一个孩子时,它说找不到视图。
Fragment childFragment = new CandidateStartEvent();
FragmentTransaction transaction =getChildFragmentManager().beginTransaction();
transaction.replace(R.id.childContainer, childFragment).commit();
答案 0 :(得分:2)
我已经解决了在我使用getFragmentManager
的第二个子片段上发布的问题CandidateStartEvent childFragment = new CandidateStartEvent();
FragmentTransaction transaction =getFragmentManager().beginTransaction();
transaction.replace(R.id.childContainer, childFragment).addToBackStack(null).commit();
答案 1 :(得分:0)
你可以在Parent片段中添加子片段我在这里使用微调器选择替换片段。 1)reports_main_segment.xml 2)ReportFragment .java(父片段)
我有两个像
这样的子片段1)reports_main_segment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gini_white_color"
android:orientation="vertical">
<Spinner
android:id="@+id/report_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_10sdp"
android:background="@drawable/gray_border_edittext"
android:dropDownVerticalOffset="80dp"
android:entries="@array/reports_status"
android:padding="@dimen/_5sdp"
android:textColor="@color/gini_gray_color_b7b7b7"/>
<LinearLayout
android:id="@+id/child_fragment_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
2)ReportFragment.java
public class ReportFragment extends Fragment {
private ViewGroup root;
private Spinner report_spinner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
root = (ViewGroup) inflater.inflate(R.layout.reports_main_segment,
null);
return root;
}
@Override
public void onResume() {
super.onResume();
FindViewById();
}
private void FindViewById() {
report_spinner = (Spinner) root.findViewById(R.id.report_spinner);
report_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String selected = adapterView.getItemAtPosition(i).toString();
if (selected.equalsIgnoreCase("Leads")) {
setFragment(1);
} else if (selected.equalsIgnoreCase("Occupancy")) {
setFragment(2);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// ToDo nothing selection
}
});
}
public void setFragment(int type) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (type == 1) {
LeadsReportFragment fragment = new LeadsReportFragment();
fragmentTransaction.replace(R.id.child_fragment_view, fragment);
} else if (type == 2) {
OccupancyFragment fragment = new OccupancyFragment();
fragmentTransaction.replace(R.id.child_fragment_view, fragment);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}