标签监听器中的混合片段类型

时间:2012-07-09 22:48:02

标签: android android-fragments

我有一个基于谷歌提供的文档代码和sherlock操作栏类的标签监听器。

我想让我的两个标签使用ListFragment类,一个使用自定义GridFragment类。

然而,标签监听器一次只能看一个类型,我是否需要创建一个标准片段并在其中放置一个列表视图或网格视图?或者我需要为每个?创建一个单独的标签监听器?或者除了任何类型的片段,标准,列表或网格之外,还有一种方法可以使用标签监听器,只要它继承自片段类。

package com.NYXDigital.LookBunnyFind;

import android.app.Activity;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;



public class TabListener<T extends SherlockFragment> implements ActionBar.TabListener {
    private SherlockFragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = (SherlockFragment)SherlockFragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

1 个答案:

答案 0 :(得分:0)

我最终创建了一个通用的片段项类,TabListener接受它作为它的参数。此类除了片段类之外,还为您自动创建它。

下面的代码是包含您想要添加的每个片段类以及标题的类。对于添加到选项卡侦听器的每个片段,您将需要一个。

package com.NYXDigital.LookBunnyFind.Widget;

import android.support.v4.app.Fragment;

public class FragmentViewPagerItem {
    private String title;
    private Class<? extends Fragment> fragmentClass;

    public FragmentViewPagerItem(String title, Class<? extends Fragment> fragmentClass){
        this.title = title;
        this.setFragmentClass(fragmentClass);
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public Class<? extends Fragment> getFragmentClass() {
        return fragmentClass;
    }

    public void setFragmentClass(Class<? extends Fragment> fragmentClass) {
        this.fragmentClass = fragmentClass;
    }

    public Fragment newInstanceOfFragmentClass() throws InstantiationException, IllegalAccessException{
        return this.fragmentClass.newInstance();
    }

}

这个适配器被修改为接受FragmentViewPagerItemClass Above

的数组
import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.NYXDigital.LookBunnyFind.Widget.FragmentViewPagerItem;

public class FragmentViewPagerAdapter extends FragmentPagerAdapter {

    private List<FragmentViewPagerItem> mFragments = new ArrayList<FragmentViewPagerItem>();

    public FragmentViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(FragmentViewPagerItem fragment) {
        if (mFragments == null) {
            mFragments = new ArrayList<FragmentViewPagerItem>();
        }
        mFragments.add(fragment);
        notifyDataSetChanged();
    }

    public Fragment getItem(int position) {
        Fragment fragment = null;

        try {
            fragment = mFragments.get(position % mFragments.size())
                    .newInstanceOfFragmentClass();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return fragment;
    }

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

    public CharSequence getPageTitle(int position) {
        return mFragments.get(position % mFragments.size()).getTitle()
                .toUpperCase();
    }
}

因此,为了使用这些类,您需要为每个片段创建一个FragmentViewPagerItem,并传入一个标题以及片段的类。然后使用addFragment方法将其添加到适配器,就像通常一样。然后将其设置为ViewPager的Adapter并绑定标题指示符以查看标题。

// Create adapter for pager and add fragments to be displayed
FragmentViewPagerAdapter adapter = new FragmentViewPagerAdapter(
        getSupportFragmentManager());

// Local
FragmentViewPagerItem localFragment = new FragmentViewPagerItem(
        "Local", BranchListFragment.class);
adapter.addFragment(localFragment);

// Find
FragmentViewPagerItem findFragment = new FragmentViewPagerItem("Find",
        FindFragment.class);
adapter.addFragment(findFragment);

// Events
FragmentViewPagerItem eventsFragment = new FragmentViewPagerItem(
        "Events", EventListFragment.class);
adapter.addFragment(eventsFragment);

// Set the pager with an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);

// Bind the title indicator to the adapter
TabPageIndicator mIndicator = (TabPageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(pager);

我正在考虑开源这个,你们觉得怎么样?