1

我有一个基于 google 提供的文档代码和 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.
    }
}
4

1 回答 1

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 的数组

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();
    }
}

因此,为了使用这些类,您需要为每个 Fragment 创建一个 FragmentViewPagerItem,并为您的 Fragment 传入标题和类。然后像往常一样使用 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);

我正在考虑开源这个,你们怎么看?

于 2013-11-08T10:06:17.587 回答