Reputation: 743
I am trying to create a nested fragment view with two fragments in which fragment will contain ListView/RecyclerView, second listview data will be dependent on first listview item.
It will be similar to product filter screen of any e-commerce application.
Please take a look at code below:
TestActivity.java which holds parent fragment:
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();
}
}
layout file for activity 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>
Parent fragment class 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();
}
}
Parent fragment layout file 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 class 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);
}
}
In result it is just showing content of Parent Fragment not content of child fragment.
I want to implement somthing like
Please correct me if I am going for wrong approch to implement this.
Thank you.
Upvotes: 0
Views: 1216
Reputation: 1116
I spotted some problems on the code you presented:
In order to have the split layout left and right, you can make it like:
<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>
Upvotes: 1