仅在一个方向使用一个片段作为一个ListView头

仅在一个方向使用一个片段作为一个ListView头

本文介绍了仅在一个方向使用一个片段作为一个ListView头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想实现支持以下两个方向的活动:

I'm trying to implement an activity that supports the two orientations below:

+--------------------------+      +-------------+---------------------------+
|                          |      |             |                           |
|                          |      |             |                           |
|                          |      |             +--------           --------+
|   Header View            |      |             |                           |
|   (from fragment)        |      |             |                           |
|                          |      | Fragment    +--------           --------+
+--------------------------+      |             |                           |
|                          |      |             |          ListFragment     |
|                          |      |             |                           |
+---------         --------+      |             +--------           --------+
|         ListFragment     |      |             |                           |
|                          |      |             |                           |
+---------         --------+      +-------------+---------------------------+
|                          |
|                          |
+--------------------------+

如果我用我的布局XML定义到处片段这并不难。该挑战来自于我想要的头片段与纵向的ListFragment滚动事实。要做到这一点,我的充气XML自己,将其添加为一个标题视图。这工作太,直到我旋转设备(从纵向到横向,周围的其他方法的工作),在这一点上,我得到以下异常:

This isn't hard if I use fragments defined in my layout XML everywhere. The challenge comes from the fact that I want the header fragment to scroll with the ListFragment in portrait orientation. To accomplish this, I'm inflating the XML myself and adding it as a header view. This works too, until I rotate the device (from portrait to landscape, the other way around works), at which point, I get the following exception:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #3: Duplicate id 0x7f06002d, tag null, or parent id 0x0 with another fragment for com.example.MyHeaderFragment
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:275)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)

我觉得这是一个事实,即我是从另一种布局手动一个方向,并自动加载一个片段来了。我想不出解决的办法。

I think this is coming from the fact that I'm loading a fragment manually in one orientation and automatically from the layout in another. I can't think of a way around this.

这我想到了一个解决办法是定义X​​ML中的纵向布局无头视图(标题不会与ListFragment滚动),然后尽快从ListFragment到页眉视图上方移动视图作为视图被创建,但没有奏效。

One solution that I thought of was to define the portrait layout in XML without the header view (the header wouldn't scroll with the ListFragment) and then move the view from above the ListFragment into the header view as soon as the view is created, but this hasn't worked.

这是设计注定要失败的,或是否有人有一个聪明的解决方案?

Is this design doomed to fail or does somebody have a clever solution?

推荐答案

我从片段删除 ID 取值解决了这个。在 FragmentManager 现在已经不再把它们看作是相同的,所以不会抛出异常。

I've resolved this by removing the ids from the fragments. The FragmentManager now no longer sees them as the same, so doesn't throw the exception.

这篇关于仅在一个方向使用一个片段作为一个ListView头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:12