Java tutorial
/* * Copyright (c) 2011 Adam Shanks, Daniel Huckaby * * 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. */ package com.handlerexploit.news.adapters; import java.util.ArrayList; import com.handlerexploit.news.widgets.PagerHeader; import com.handlerexploit.news.widgets.PagerHeader.OnHeaderClickListener; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; public class PagerAdapter extends FragmentStatePagerAdapter implements ViewPager.OnPageChangeListener, OnHeaderClickListener { private final Context mContext; private final ViewPager mPager; private final PagerHeader mHeader; private final ArrayList<PageInfo> mPages = new ArrayList<PageInfo>(); static final class PageInfo { private final Class<?> clss; private final Bundle args; PageInfo(Class<?> _clss, Bundle _args) { clss = _clss; args = _args; } } public PagerAdapter(FragmentActivity activity, ViewPager pager, PagerHeader header) { super(activity.getSupportFragmentManager()); mContext = activity; mHeader = header; mHeader.setOnHeaderClickListener(this); mPager = pager; mPager.setAdapter(this); mPager.setOnPageChangeListener(this); } public void addPage(Class<?> clss, Bundle args, String title) { PageInfo info = new PageInfo(clss, args); mPages.add(info); mHeader.add(0, title); notifyDataSetChanged(); } @Override public int getCount() { return mHeader.getChildCount(); } @Override public Fragment getItem(int position) { PageInfo info = mPages.get(position); Fragment fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args); return fragment; } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { mHeader.setPosition(position, positionOffset, positionOffsetPixels); } @Override public void onPageSelected(int position) { mHeader.setDisplayedPage(position); } @Override public void onPageScrollStateChanged(int state) { // Ignore } @Override public void onHeaderSelected(int position) { mPager.setCurrentItem(position); } @Override public void onHeaderClicked(int position) { // Ignore } public static interface OnRefreshListener { public void onRefresh(); } }