实现跟手的TabActivity效果

 

 author by afei

利用 android-support-v4 中的ViewPager可以轻松的实现跟手滑动。但是只支持 View 和 Fragment(也是利用view)。

 

之前也考虑过使用 ViewFliping 和 gallery ,但是都要将Activity转成View才可以。但是这样就会失去activity的特性。思来想后,还是改成Fragment靠谱点。

 

总体思路是这样:将本来放在TabActivity中的Activity们都改成 Fragment, 然后修改TabActivity,使其TabHost中content和width和height为0,然后把pager和tabHost关联起来。很trick。。。但是效果达到。

 

 

关键是如何修改activity至fragment,有没有这个可行性?

答案是必须应该可以的!万一有问题,也是可以商量的嘛。。。

1.将 extends Activity 改为 extends Fragment, 如果是 ListActivity 就改为 ListFragment。。。这明显就是为了修改方便啊。。。

2.一定会有XX出现,但是一定不多,fragment 的生命周期和 activity 很相似,包括onActivityResult() onStart() onPause() 等等。都有。

这里主要在onCreate()中,因为我们onCreate() 中会有 setContent(R.layout.layout_id) 恩,这里一定会报错,因为在fragment中单独抽出来了一个函数 onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState),把跟初始化view的相关代码都考到这个函数中就ok了,简单啊。将setContent 变为 inflater.inflate(R.layout.choose_accompany_layout, null) 然后return就ok。

3.最后一步,我们的代码中会有一些用到activity 和context 的地方,比如 getContentResolver() 和 其它的函数,这里只需要变为 getActivity() 就ok。因为fragment 中会和 外界的activity绑定起来。直接调用就可以。

修改完毕!!!

 

 

继承 FragmentTabsPager(代码在ChannelCommon中), 在onCreate()中调用 addTab(), 需要几个tab页就加入几次,然后这可以完美实现跟手滑动的tabActivity效果了!!!哇哈哈。

 

 

FragmentTabsPager 代码 :

 

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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.xiaomi.channel.common.pager_tabs;

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.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabWidget;

import java.util.ArrayList;

import com.xiaomi.channel.common.R;

/**
 * 经过afei修改为抽象类,使用者只需要继承它,并在onCreate() 中调用addTab即可 addTab 时的tabSpec
 * 可以使用布局,无需担心。可见UsageDemo
 * Demonstrates combining a TabHost with a ViewPager to
 * implement a tab UI that switches between tabs and also allows the user to
 * perform horizontal flicks to move between the tabs.
 */
public abstract class FragmentTabsPager extends FragmentActivity {
    TabHost mTabHost;
    ViewPager mViewPager;
    TabsAdapter mTabsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        mViewPager = (ViewPager) findViewById(R.id.pager);

        mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }

    /**
     * This is a helper class that implements the management of tabs and all
     * details of connecting a ViewPager with associated TabHost. It relies on a
     * trick. Normally a tab host has a simple API for supplying a View or
     * Intent that each tab will show. This is not sufficient for switching
     * between pages. So instead we make the content part of the tab host 0dp
     * high (it is not shown) and the TabsAdapter supplies its own dummy view to
     * show as the tab content. It listens to changes in tabs, and takes care of
     * switch to the correct paged in the ViewPager whenever the selected tab
     * changes.
     */
    public static class TabsAdapter extends FragmentPagerAdapter
            implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

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

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

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;

            public DummyTabFactory(Context context) {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mContext));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);
            mTabHost.addTab(tabSpec);
            notifyDataSetChanged();
        }

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

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

        @Override
        public void onTabChanged(String tabId) {
            int position = mTabHost.getCurrentTab();
            mViewPager.setCurrentItem(position);
        }

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

        @Override
        public void onPageSelected(int position) {
            // Unfortunately when TabHost changes the current tab, it kindly
            // also takes care of putting focus on it when not in touch mode.
            // The jerk.
            // This hack tries to prevent this from pulling focus out of our
            // ViewPager.
            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }
}

 

 

这是个通用的类,使用时继承就ok了,比如这样:

 

package com.xiaomi.channel.common.pager_tabs;

import android.os.Bundle;

public class UsageDemo extends FragmentTabsPager {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 只需要将下面注释取消就可以,也就是说,只需要重写onCreate(),并加入tab就ok了。
        // 每一行是一个TabSpec。记的要是Fragment,而不是activity

        mTabsAdapter.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                ChooseAccompanyFragment.class, null);
        mTabsAdapter.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                ChooseAccompanyFragment1.class, null);
        mTabsAdapter.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                ChooseAccompanyFragment2.class, null);
        mTabsAdapter.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                ChooseAccompanyFragment3.class, null);
    }
}

 

纯原创,转载需保留出处噢亲。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据