我使用应用程序设置来制作选项卡布局。它在布局中使用操作栏和 ViewPager 来显示多个选项卡。我目前有两个选项卡。
但是我想添加第三个选项卡,除非按下其中一个选项卡 fragment 中的特殊按钮,否则它将被隐藏。在按下按钮时,我认为应用程序可以滑到隐藏的选项卡并使其可见。向后滑动会使它再次隐藏。
- 在 Android 中这甚至可能吗? :)
- 我将如何使用我当前的选项卡管理形式来实现这一点?我发现了一些关于在布局中使用 TabActivity 或 TabHosts 的人的问题。使用这两种实现方式中的一种会更容易吗?
如果您需要任何源代码,请询问。提前致谢。
最佳答案
是的,你所要求的可以完成。
简而言之:
在您指定要使隐藏选项卡可见的Button
的onClickListener
中,您应该调用ActionBar.addTab
。
添加新的 fragment
根据包含您的Fragments
的布局,您可以调用FragmentTransaction.hide
和FragmentTransaction.show
,否则我会假设您正在动态添加 Fragment
,因此使用 FragmentPagerAdapter
,在这种情况下,将新的 Fragment
添加到 List
.
链接
FragmentTransaction - hide , show
您还应该阅读 Adding Fragments docs .
这是一个非常基本的示例:
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();
}
}
虚拟 fragment
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));
}
关于android - 从 Android ActionBar 隐藏选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15469478/