android – SwipeyTabs – 如何使用ActionBarSherlock库创建Swipey Tabs?

前端之家收集整理的这篇文章主要介绍了android – SwipeyTabs – 如何使用ActionBarSherlock库创建Swipey Tabs?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经使用ViewPagerExtensions创建了swipey标签,但这次我在我的应用程序中只使用一个库ActionBarSherlock,我不知道如何创建谷歌游戏商店的swipey选项卡.如果你能帮我完成,我会非常感谢你.

这是我活动类的onCreate方法

@Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(SampleList.THEME); //Used for theme switching in samples
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_tabs);
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();

        mTabManager = new TabManager(this,mTabHost,R.id.realtabcontent);

        mTabManager.addTab(mTabHost.newTabSpec("simple").setIndicator("Groups"),FragmentStackSupport.CountingFragment.class,null);
        mTabManager.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),LoaderCursorSupport.CursorLoaderListFragment.class,null);
        mTabManager.addTab(mTabHost.newTabSpec("custom").setIndicator("Logs"),LoaderCustomSupport.AppListFragment.class,null);
        mTabManager.addTab(mTabHost.newTabSpec("throttle").setIndicator("Gallery"),LoaderThrottleSupport.ThrottledLoaderListFragment.class,null);

        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }

在第一个图像顶部新付费是当前的屏幕标题,其他两个显示上一个或下一个屏幕瓦片.如何显示上一个和下一个屏幕标题在角落有一些像这样的衰落效果.

解决方法

您需要使用ActionBar Tabs,TabsAdapter和ViewPager(scrolly位).这里有一个完整的教程: http://davidjkelley.net/?p=34(只是使用ABS进口,而不是标准的兼容性库).

以下是一些相关代码

import java.util.ArrayList;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
public class Polling extends FragmentActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
private final static String TAG = "21st Polling:";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);

mTabsAdapter = new TabsAdapter(this,mViewPager);
mTabsAdapter.addTab(bar.newTab().setText(R.string.login),LoginFragment.class,null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.economics),EconFragment.class,null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.elections),ElectionsFragment.class,null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.politics),PoliticsFragment.class,null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.science),ScienceFragment.class,null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.finance),FinanceFragment.class,null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.religion),ReligionFragment.class,null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.military),MilitaryFragment.class,null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.international),InternationalFragment.class,null);
}

public static class TabsAdapter extends FragmentPagerAdapter
    implements ActionBar.TabListener,ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class,Bundle _args) {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(FragmentActivity activity,ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab,Class<?> clss,Bundle args) {
            TabInfo info = new TabInfo(clss,args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }


        public int getCount() {
            return mTabs.size();
        }

        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext,info.clss.getName(),info.args);
        }


        public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {
        }


        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
        }


        public void onPageScrollStateChanged(int state) {
        }


        public void onTabSelected(Tab tab,FragmentTransaction ft) {
            mViewPager.setCurrentItem(tab.getPosition());
            Log.v(TAG,"clicked");
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        public void onTabUnselected(Tab tab,FragmentTransaction ft) {}

        public void onTabReselected(Tab tab,android.app.FragmentTransaction ft) {}

        @Override
        public void onTabSelected(Tab tab,android.app.FragmentTransaction ft) {    
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        public void onTabUnselected(Tab tab,android.app.FragmentTransaction ft) {}
    }
原文链接:https://www.f2er.com/android/311912.html

猜你在找的Android相关文章