Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nekofar/e1669d3892deda9237de to your computer and use it in GitHub Desktop.
Save nekofar/e1669d3892deda9237de to your computer and use it in GitHub Desktop.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/text_detail_placeholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button_other_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_button_other_activity"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/layout_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:padding="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@android:color/white"
android:text="@string/text_placeholder"/>
</FrameLayout>
</LinearLayout>

TestViewPagerWithFixedFooter

See it in action on an Android 2.2 (API 8) emulator :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.pde.tests.testviewpagerwithfixedfooter.app" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="fr.pde.tests.testviewpagerwithfixedfooter.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="fr.pde.tests.testviewpagerwithfixedfooter.app.DetailActivity"
android:label="@string/title_activity_detail"
android:parentActivityName="fr.pde.tests.testviewpagerwithfixedfooter.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
</activity>
</application>
</manifest>
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:19.1.0'
compile 'com.android.support:appcompat-v7:19.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
package fr.pde.tests.testviewpagerwithfixedfooter.app;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class DetailActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview_placeholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
package fr.pde.tests.testviewpagerwithfixedfooter.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewGroup mRootLayout;
ViewPager mViewPager;
View mFooterView;
Button mOtherActivityButton;
// ----------------------------------
// ACTIVITY LIFECYCLE
// ----------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mRootLayout = (ViewGroup) findViewById(R.id.layout_root);
mViewPager = (ViewPager) findViewById(R.id.pager);
mFooterView = findViewById(R.id.layout_footer);
mOtherActivityButton = (Button) findViewById(R.id.button_other_activity);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
mOtherActivityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DetailActivity.class));
}
});
}
// ----------------------------------
// INNER CLASS
// ----------------------------------
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
return PlaceholderFragment.newInstance(position + 1, R.color.red);
case 1:
return PlaceholderFragment.newInstance(position + 1, R.color.green);
case 2:
return PlaceholderFragment.newInstance(position + 1, R.color.yellow);
}
return PlaceholderFragment.newInstance(position + 1, android.R.color.white);
}
@Override
public int getCount() {
return 3;
}
}
private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
private int footerHeight = 0;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == 1) {
if (footerHeight <= 0) {
footerHeight = mFooterView.getHeight();
}
if (footerHeight > 0) {
int footerTranslation = (int) (2 * footerHeight * positionOffset);
if (footerTranslation > footerHeight) {
footerTranslation = footerHeight;
}
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mFooterView.getLayoutParams();
if (layoutParams.bottomMargin == -footerHeight) {
mFooterView.setVisibility(View.GONE);
}
if (footerTranslation < footerHeight && mFooterView.getVisibility() == View.GONE) {
mFooterView.setVisibility(View.VISIBLE);
}
if (mFooterView.getVisibility() == View.VISIBLE) {
layoutParams.bottomMargin = -footerTranslation;
mFooterView.setLayoutParams(layoutParams);
mRootLayout.invalidate();
}
}
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
public static class PlaceholderFragment extends Fragment {
private static final String BUNDLE_KEY_POS = "BUNDLE_KEY_POS";
private static final String BUNDLE_KEY_COLOR = "BUNDLE_KEY_COLOR";
private TextView textView;
public static PlaceholderFragment newInstance(int position, int color) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(BUNDLE_KEY_POS, position);
args.putInt(BUNDLE_KEY_COLOR, color);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_placeholder, container, false);
view.setBackgroundColor(getResources().getColor(getArguments().getInt(BUNDLE_KEY_COLOR)));
textView = (TextView) view.findViewById(R.id.textview_placeholder);
textView.setText(getString(R.string.text_placeholder_fragment, getArguments().getInt(BUNDLE_KEY_POS)));
return view;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment