1

我使用应用程序设置来制作选项卡布局。它在布局中使用操作栏和 ViewPager 来显示许多选项卡。我目前有两个标签。

但是我想添加第三个选项卡,除非按下其中一个选项卡片段中的特殊按钮,否则它将被隐藏。在按钮按下时,我认为应用程序可以滑到隐藏选项卡并使其可见。向后滑动会使它再次隐藏。

  1. 这在Android中甚至可能吗?:)
  2. 我将如何使用我当前的选项卡管理形式来实现这一点?我发现了一些关于人们在布局中使用 TabActivity 或 TabHosts 的问题。使用这两种实现之一会更容易吗?

如果您需要任何源代码,请询问。提前致谢。

4

1 回答 1

3

是的,你问的都可以。

简而言之

在您onClickListener指定Button使隐藏选项卡可见的情况下,您应该调用ActionBar.addTab.

添加新的Fragment

根据包含您的布局Fragments,您可以调用FragmentTransaction.hideand FragmentTransaction.show,否则我会假设您正在Fragment动态添加,因此使用 a FragmentPagerAdapter,在这种情况下将您的新添加Fragment到您的List.

链接

操作栏- addTab

FragmentTransaction -隐藏显示

您还应该阅读添加片段文档

这是一个非常基本的示例:

ViewPager 的适配器

public class PagerAdapter extends FragmentPagerAdapter {

/**
 * The list of {@link Fragment}s used in the adapter
 */
private final List<Fragment> mFragments = new ArrayList<Fragment>();

/**
 * Constructor for <code>PagerAdapter</code>
 * 
 * @param fm The {@link FragmentManager} to use.
 */
public PagerAdapter(FragmentManager fm) {
    super(fm);
}

/**
 * Adds a new {@link Fragment} to the adapter
 * 
 * @param fragment The new {@link Fragment} to add to the list
 */
public void addFragment(Fragment fragment) {
    mFragments.add(fragment);
    notifyDataSetChanged();
}

/**
 * {@inheritDoc}
 */
@Override
public Fragment getItem(int position) {
    return mFragments.get(position);
}

/**
 * {@inheritDoc}
 */
@Override
public int getCount() {
    return mFragments.size();
    }
}

虚拟碎片

public static final class DummyFragment extends Fragment implements View.OnClickListener {

    /**
     * Empty constructor as per the {@link Fragment} docs
     */
    public DummyFragment() {
    }

    /**
     * @param color The color to make the root view
     * @return A new instance of {@link DummyFragment}
     */
    public static DummyFragment getInstance(int color) {
        final Bundle bundle = new Bundle();
        bundle.putInt("color", color);
        final DummyFragment fragment = new DummyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
        rootView.setBackgroundColor(getArguments().getInt("color"));
        return rootView;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // This should be your Button
        view.setOnClickListener(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onClick(View v) {
        // This adds the new tab
        ((MainActivity) getActivity()).addTab(2, Color.BLUE);
        }
   }

调用添加每个 Fragment

    /**
 * Used to add a new {@link Fragment} to {@link ViewPager}'s adapter and
 * adds a new {@link Tab} to the {@link ActionBar}.
 * 
 * @param pageTitle The title of the tab
 * @param color The background color of the {@link Fragment}
 */
public void addTab(int pageTitle, int color) {
    mPagerAdapter.addFragment(DummyFragment.getInstance(color));
    mActionBar.addTab(mActionBar.newTab()
            .setText("" + pageTitle)
            .setTabListener(this));
}
于 2013-03-18T07:51:48.187 回答