我正在尝试使用两个片段创建一个嵌套片段视图,其中片段将包含ListView / RecyclerView,第二个listview数据将依赖于第一个listview项目。
它类似于任何电子商务应用程序的产品过滤器屏幕。
请看下面的代码:
包含父片段的TestActivity.java:
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
FilterFragment filterFragment = new FilterFragment();
ft.replace(R.id.filter_fragment_container, filterFragment);
ft.commit();
}
}
活动activity_test.xml的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.splice.TestActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
父片段类FilterFragment.java
public class FilterFragment extends Fragment{
public FilterFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_filter, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
insertNestedFragment();
}
// Embeds the child fragment dynamically
private void insertNestedFragment() {
Fragment childFragment = new TestFragment1();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.fragment1_container, childFragment).commit();
}
}
父片段布局文件fragment_filter.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="@dimen/activity_horizontal_margin"
tools:context="com.splice.views.FilterFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="@string/filter_purchase_title" />
<FrameLayout
android:layout_width="match_parent"
android:id="@+id/fragment1_container"
android:layout_height="wrap_content">
</FrameLayout>
Child Fragment类TestFragment1.java
public class TestFragment1 extends Fragment {
public TestFragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_test_fragment1, container, false);
}
}
结果它只显示父片段的内容而不是子片段的内容。
如果我要执行错误的approch,请纠正我。
谢谢。
答案 0 :(得分:1)
我发现您提供的代码存在一些问题:
为了左右分割布局,你可以这样做:
<LinearLayout ...
android:orientation="horizontal"
android:weightSum="2">
<View1
...
android:layout_width="0dp"
android:layout_weight="1"
/>
<View2
...
android:layout_width="0dp"
android:layout_weight="1"
/>
</LinearLayout>