更换标签时的黑色片段内容

时间:2013-10-01 13:12:15

标签: android android-fragments android-viewpager supportmapfragment fragmentstatepageradapter

我在其中有TabHost和ViewPager的片段活动。 活动有两个选项卡:“列表”选项卡和“地图”选项卡。 在列表选项卡上我有我的数据列表(一些机构)和地图选项卡(使用谷歌地图API)我在地图上显示标记的机构。

当活动打开时,默认选项卡为List。从右向左滑动或选择“地图”选项卡会将我带到带有标记的地图。

问题是,当我再次选择列表选项卡时,我有一个黑色内容而不是ListView及其项目。但是,从“列表”选项卡中再次选择“地图”选项卡时,“地图”显示正确。

这是活动来源:

public class Institutions extends FragmentActivity {

public static final int TAB_LIST = 0;
public static final int TAB_MAP = 1;

private TabHost mTabHost;
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.institutions);

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

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

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

    View v = createTab(TAB_LIST);

    mTabsAdapter.addTab(mTabHost.newTabSpec("list").setIndicator(v),
            InstitutionList.class, null);

    v = createTab(TAB_MAP);

    mTabsAdapter.addTab(mTabHost.newTabSpec("map").setIndicator(v),
            InstitutionMap.class, null);

    TextView activity_title = (TextView) findViewById(R.id.activity_title);

    activity_title.setText(getIntent().getStringExtra(
            Institution.INSTITUTION_TITLE));
}

private View createTab(int type) {
    View v = getLayoutInflater().inflate(R.layout.tab, null);

    TextView label = (TextView) v.findViewById(R.id.label);
    ImageView icon = (ImageView) v.findViewById(R.id.icon);

    icon.setVisibility(View.GONE);

    icon.getLayoutParams().width = 1;
    icon.getLayoutParams().height = 1;

    switch (type) {
    case TAB_LIST:
        label.setText(getString(R.string.list_title));
        break;

    case TAB_MAP:
        label.setText(getString(R.string.map_title));
        break;

    default:
        break;
    }

    return v;
}
}

以下是TabsAdapter的来源:

public class TabsAdapter extends FragmentStatePagerAdapter 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 Class<? extends Fragment> clss;
    private final Bundle args;

    private Tab tab;

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

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

    public TabFactory(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<? extends Fragment> clss,
        Bundle args) {
    tabSpec.setContent(new TabFactory(mContext));

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

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

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

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

    return info.tab;
}

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

    for (int i = 0; i < getCount(); i++) {
        if (i == position)
            continue;

        Tab t = mTabs.get(i).tab;

        if (t != null) {
            t.onTabUnselected();
        }
    }
}

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

@Override
public void onPageSelected(int position) {
    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) {
}
}

0 个答案:

没有答案