Last active
February 16, 2016 22:16
-
-
Save lishiyo/4c53066e1b348f7371b3 to your computer and use it in GitHub Desktop.
Smooth View Pager - AppBar with Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?><!-- | |
~ Copyright 2016 "Henry Tao <hi@henrytao.me>" | |
~ | |
~ Licensed under the Apache License, Version 2.0 (the "License"); | |
~ you may not use this file except in compliance with the License. | |
~ You may obtain a copy of the License at | |
~ | |
~ http://www.apache.org/licenses/LICENSE-2.0 | |
~ | |
~ Unless required by applicable law or agreed to in writing, software | |
~ distributed under the License is distributed on an "AS IS" BASIS, | |
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
~ See the License for the specific language governing permissions and | |
~ limitations under the License. | |
--> | |
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:id="@+id/coordinator" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.v4.view.ViewPager | |
android:id="@+id/view_pager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
<me.henrytao.smoothappbarlayout.SmoothAppBarLayout | |
android:id="@+id/smooth_app_bar_layout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:minHeight="2dp"> | |
<FrameLayout | |
android:id="@+id/header_fragment_placeholder" | |
app:layout_scrollFlags="scroll" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/toolbar" | |
android:background="@android:color/transparent" | |
app:layout_scrollFlags="scroll|enterAlways" | |
app:navigationIcon="@drawable/ic_toolbar_arrow_back" | |
style="@style/AppStyle.MdToolbar" /> | |
<android.support.design.widget.TabLayout | |
android:id="@+id/tab_layout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
</me.henrytao.smoothappbarlayout.SmoothAppBarLayout> | |
</android.support.design.widget.CoordinatorLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<TextView | |
android:id="@+id/title" | |
android:text="Hello World" | |
android:layout_width="match_parent" | |
android:layout_height="120dp" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.henrytao.smoothappbarlayoutdemo.fragment; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import me.henrytao.smoothappbarlayoutdemo.R; | |
public class ExHeaderFragment extends Fragment { | |
public static final String TAG_EX_HEADER = "fragment_blog_header"; | |
@Override | |
public View onCreateView(final LayoutInflater inflater, final ViewGroup vg, final Bundle data) { | |
super.onCreateView(inflater, vg, data); | |
return inflater.inflate(R.layout.ex_header_fragment, vg, false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
// ...all the other code | |
createOrGetBlogHeaderFragment(savedInstanceState); | |
} | |
protected ExHeaderFragment createOrGetBlogHeaderFragment(@Nullable final Bundle savedInstanceState) { | |
final ExHeaderFragment exHeaderFragment; | |
if (savedInstanceState == null) { | |
exHeaderFragment = new ExHeaderFragment(); | |
exHeaderFragment.setArguments(getIntent().getExtras()); | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
getSupportFragmentManager() | |
.beginTransaction() | |
.replace(R.id.header_fragment_placeholder, exHeaderFragment, ExHeaderFragment.TAG_EX_HEADER) | |
.commit(); | |
} | |
}, 3000); | |
} else { | |
exHeaderFragment = (ExHeaderFragment) getSupportFragmentManager().findFragmentByTag(ExHeaderFragment.TAG_EX_HEADER); | |
} | |
return exHeaderFragment; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gif: https://media.giphy.com/media/1x7qw0P0Fw6vm/giphy.gif