I replace fragment in a activity and in my fragment I need a CoordinatorLayout
.
Here is my code :
CoordinatorActivity
class:
public class CoordinatorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coordinator);
Fragment newFragment = new FragmentCoordinator();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fr_main, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
activity_coordinator.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fr_main"
tools:context="com.app.hoatv.CoordinatorActivity">
</FrameLayout>
FragmentCoordinator
class:
public class FragmentCoordinator extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_coordinator, container, false);
}
}
fragment_coordinator.xml
file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.hoatv.CoordinatorActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:elevation="0dp"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme">
<include layout="@layout/layout_group_header"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true">
<android.support.v7.widget.AppCompatTextView
android:layout_height="1000dp"
android:layout_width="match_parent"
android:text="@string/lorem"
android:background="@color/colorAccent"
android:id="@+id/container"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
layout_group_header.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="100dp"
android:background="#a70000">
</LinearLayout>
but when I scrolling NestedScrollView
layout_group_header.xml
not show or hide. It always show in my fragment screen.
What happened in my code? and How I can fix it?
What happened in my code? and How I can fix it?
There are few things which you should consider. First, you are opening a view(LinearLayout
) inside AppBarLayout
which there might be a Toolbar
but that's not just the case.
First part: (Important one): The best idea and correct way of creating such layout
s is to first, remove LinearLayout
from the root of the fragment_coordinator
layout then copy-paste the codes from fragment_coordinator
to activity_coordinator
. After that, use FrameLayout as the content's Container inside NestedScrollView
.
Making Changes Part (As your requirements): After all, you can place codes anywhere you want in the main layout, for example, if you want the layout_group_header
to be hidden after scrolling in the layout
, you may wanna use layout_group_header
codes inside fragment_coordinator
which it will be a scrolling content. Othewise, you can use the codes inside CoordinatorLayout
or anywhere instead of including it like that.
P.s: Design is completely seems to be wrong. Use first part which is an important part of creating such layouts. Note that there will be some changes inside java-kotlin sides too. (Changing layout address or etc)