隐藏/显示编程上下文操作栏 [英] Hiding/showing the contextual action bar programmatically

查看:134
本文介绍了隐藏/显示编程上下文操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例:

我有一个 ViewPager 作为其唯一视图元素,设置了三个片段 S代表其活动三页(使用 FragmentPagerAdapter )。其中一个片段是 ListFragment ,载而其他两个常规片段取值一些信息图表。

在列表中的 ListFragment 允许确认元素,并检查元素带来了上下文操作栏,presenting操作的列表可能选定/检查的项目。

我的问题:

当我选择了几个列表项中,CAB显示出来。但是,当我切换页面时,CAB仍然显示,presenting的行动。更糟糕的是,在 ViewPager 似乎有时会破坏表片段的观点 - 因此在选择的动作抛出一个java.lang.IllegalStateException:内容视图尚未创建例外。

我的问题:

  1. 如何获得,当用户移离 ListFragment 页的CAB消失?
  2. (可选)如何获得CAB重新出现时, ListFragment 的回复是,有些项目仍然起作用。
解决方案

您需要首先添加 ViewPager.OnPageChangeListener ViewPager.OnPageChangeListener viewPager

onPageSelected 方法调用的方法在你的 ListFragment 来解散你的出租车。

编辑:

要获得当前活动的片段,你可以使用它的标签。标签可以用这种方法来computated:

 私有静态字符串makeFragmentName(INT的ViewID,INT指数){
   回归机器人:切换:+的ViewID +:+指数;
}
 

在这里的ViewID是viewpager和索引选项卡索引(位置)

的id

My use case:

I have an activity with a ViewPager as its only view element, set up with three Fragments for its three pages (using a FragmentPagerAdapter). One of these fragments is a ListFragment, while the other two regular Fragments containing some infographics.

The list in the ListFragment allows "checking" elements, and checking elements brings up the contextual action bar, presenting a list of actions possible on the selected/checked items.

My problem:

When I select a couple of list items, the CAB shows up. But when I switch pages, the CAB stays visible, presenting the actions. To make matters worse, the ViewPager seems to sometimes destroy the view of the list fragment - hence selecting the actions throws a "java.lang.IllegalStateException: Content view not yet created" exception.

My questions:

  1. How do I get the CAB to disappear when the user moves away from the ListFragment page?
  2. (optional) How do I get the CAB to reappear when the ListFragment back is and some items are still checked.

解决方案

You need to first add a ViewPager.OnPageChangeListener with ViewPager.OnPageChangeListener to your viewPager.

In the onPageSelected method call a method in your ListFragment to dismiss your CAB.

EDIT:

To get the current active fragment you can use it's tag. The tag can be computated with this method:

private static String makeFragmentName(int viewId, int index) {
   return "android:switcher:" + viewId + ":" + index;
}

where viewId is the id of the viewpager and index the tab index (position)

这篇关于隐藏/显示编程上下文操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆