问题描述
我的用例:
我有一个 ViewPager
作为其唯一视图元素,设置了三个片段
S代表其活动三页(使用 FragmentPagerAdapter
)。其中一个片段是 ListFragment
,载而其他两个常规片段
取值一些信息图表。
在列表中的 ListFragment
允许确认元素,并检查元素带来了上下文操作栏,presenting操作的列表可能选定/检查的项目。
我的问题:
当我选择了几个列表项中,CAB显示出来。但是,当我切换页面时,CAB仍然显示,presenting的行动。更糟糕的是,在 ViewPager
似乎有时会破坏表片段的观点 - 因此在选择的动作抛出一个java.lang.IllegalStateException:内容视图尚未创建
例外。
我的问题:
- 如何获得,当用户移离
ListFragment
页的CAB消失? - (可选)如何获得CAB重新出现时,
ListFragment
的回复是,有些项目仍然起作用。
您需要首先添加 ViewPager.OnPageChangeListener
与 ViewPager.OnPageChangeListener
您 viewPager
。
在 onPageSelected 方法调用的方法在你的 ListFragment
来解散你的出租车。
编辑:
要获得当前活动的片段,你可以使用它的标签。标签可以用这种方法来computated:
私有静态字符串makeFragmentName(INT的ViewID,INT指数){
回归机器人:切换:+的ViewID +:+指数;
}
在这里的ViewID是viewpager和索引选项卡索引(位置)
的idMy use case:
I have an activity with a ViewPager
as its only view element, set up with three Fragment
s for its three pages (using a FragmentPagerAdapter
). One of these fragments is a ListFragment
, while the other two regular Fragment
s 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:
- How do I get the CAB to disappear when the user moves away from the
ListFragment
page? - (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)
这篇关于隐藏/显示编程上下文操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!