- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我要实现一个复杂的布局。它有 19 个部分,可以根据用户先前输入的大量参数显示或不显示。为了简化代码并且不显示未使用的部分,布局是动态创建的。
一切都在 fragment 中。该 fragment 有一个用作容器的 LinearLayout,在创建 fragment 时,我会生成所有必要的部分。
每个部分都由其自己的本地适配器管理,该适配器负责膨胀该部分的布局并将其添加到容器中。
一切正常。问题是 2 个部分具有完全相同的结构,因此它们共享相同的 xml 布局。因此,这两个部分的内部 View 具有相同的 ID。这不是问题,因为该部分在其适配器中本地管理。当我转到下一个 fragment 然后返回到这个 fragment 时,问题就出现了。系统尝试恢复 View 的先前状态,并且因为这两个部分具有相同的 ID,当恢复第二个部分时,其值也设置为第一个。
是否有任何解决方案来管理它或告诉 fragment 不要恢复其状态(因为无论如何都是手动重新加载的)。
这是当前结构的一个简单示例:
fragment xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
节 xml
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/section_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
fragment 代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_layout, container, false);
if (<condition>)
createSection1(getContext(),view);
if (<condition>)
createSection2(getContext(),view);
return view;
}
private void createSection1(Context context, ViewGroup root){
Section1Adapter adapter = new Section1Adapter(context, root);
// ...
}
private void createSection2(Context context, ViewGroup root){
Section2Adapter adapter = new Section2Adapter(context, root);
// ...
}
适配代码(两者思路相同)
public Section2Adapter(LayoutInflater inflater, ViewGroup root) {
View view = LayoutInflater.from(context).inflate(R.layout.section_layout, root, false);
initView(view);
root.addView(view);
}
最佳答案
你需要做的,就是告诉Android自己在SparseArray
中的哪个键保存哪个EditText
的状态。基本上,您需要做到这一点:
实现此目的的机制在 this amazing article 中有很好的解释。 , 帕夏·杜德卡 (Pasha Dudka) 着。 (也感谢他的精美图片)
只需在文章中搜索“View IDs should be unique”,您就会得到答案。
针对您的特定情况的解决方案的要点如下:
LinearLayout
s.t.您的 CustomLinearLayout
将知道 child 所属的部分,当它处于状态时。这样,您可以将一个部分中的所有子状态保存到专用于该部分的 SparseArray
,并将专用的 SparseArray
添加到 global SparseArray
(就像在图片中一样)EditText
,s.t.您的 CustomEditText
知道它属于哪个部分,并将其状态保存在 SparseArray
中的自定义键中 - 例如section_text_Section1
用于第一部分,section_text_Section2
用于第二部分就个人而言,我更喜欢第一个版本,因为即使您稍后向您的版 block 添加更多 View ,它也能正常工作。第二个不适用于更多 View ,因为在第二个 View 中,进行智能状态保存的不是父 View ,而是 View 本身。
希望这对您有所帮助。
关于android - 恢复具有相同 ID 的两个 View 的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45818015/
有什么方法可以恢复删除的元素吗? 这是我删除元素的代码 myFunction() { var width = window.innerWidth; var February = doc
我有一个 TokuDB 表,由于某种原因缺少 ***_status.tokudb 文件。 我还不确定文件是否由于 TokuDB 崩溃而丢失。 问题是: 有没有办法从主要文件和关键文件(我可以从 tok
我正在 Windows 7 (x86) 上运行带有 Workbench 6.3.8 的 32 位 MySQL Server 5.7.22 本地实例(必须选择 32 位版本 - 所以,较旧的版本)。 我
1、备份 <% SQL="backup database 数据库名 to disk='"&Serve
1、ASP中怎么实现SQL数据库备份、恢复! 答:asp在线备份sql server数据库: 1、备份 <% SQL="ba
我在 R 中使用 stats::filter 函数来理解 R 中的 ARIMA 模拟(如在函数 stats::arima.sim 中)和估计。我知道 stats::filter 将线性过滤器应用于向量
我已经浏览了示例应用程序的文档和代码,并发现 files/objectbox/objectbox/data.mdb 是存储所有数据的默认文件。 假设我的理解是正确的,我有几个问题找不到文档: 我想在我
为了恢复非续订订阅类型的 InAppPurchase,我已经实现了服务器来处理此问题。 但在购买过程中,iTunes 有时不会要求用户验证他们的卡详细信息, 在这种情况下,它会在后台发送应用程序并显示
我的问题是寻找cocos2d游戏期间暂停/恢复状态(包括所有需要保存的数据信息)的设计解决方案。 包括但不限于以下情况: 1).用户选择退出,然后弹出一个对话框供用户选择“直接退出”、“暂停”; 2)
在 Mercurial 中,我有一个旧的变更集,除了对单个文件的更改外,它都很好。我将如何恢复对该单个文件的更改? 即使只是能够在上一个变更集中查看文件的状态也会很好,然后我可以剪切和粘贴。 我的 M
我的一项职能遇到了困难。我想做的是计时器在页面加载后立即启动,并且只有一个带有启动/恢复的按钮。我无法在代码中找出需要更改功能的位置。有人可以帮助我吗?谢谢! HTML: , Javascr
我正在阅读Scrap your type classes 。这为类型类提供了替代方案。然而,我被Paul Chiusano的评论所困扰。其中讨论了恢复 do 表示法 语法。 坦白说,我无法理解 ret
当 OrientDB 因某人重新启动机器而非正常关闭时,OrientDB 最终会处于数据恢复失败的状态。对于如何从这种不正常的关闭中正常恢复有什么建议吗?我们正在寻找系统在断电期间能够自行恢复的方法。
我正在构建一个 Electron 应用程序,如果发生崩溃,它必须重新加载渲染进程窗口。 目前我可以从主进程重新启动应用程序 app.relaunch(); app.quit(); 但我无法检测到窗口崩
我有 3 个 Activity ,比如说 MainActivity、 Activity 2 和 Activity 3。 在 MainActivity 中,我有一个按钮(开始/停止),当我单击此按钮时,
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
Twilio 是否支持暂停和恢复内容播放。换句话说,我有相当长的文件将播放给调用者,并且我正在尝试找到一种方法来实现暂停和恢复功能。在播放某些内容的过程中,我希望用户能够按数字暂停,然后再次按数字从音
我已经提交了 A、B、C、D 和 E。我意识到在提交 B 中发生了一些非常糟糕的事情,所以我想回到 A,这次正确地进行之前搞砸了 B 的更改,然后重新应用 C 、 D 和 E 自动。 您可能想知道为什
我的一个文件被“标记为文本”,图标也发生了变化。实际上这是一个 PHP 文件。我尝试过使用 Help -> Find Action -> Mark As 尝试将其恢复为 PHP 突出显示,但它不起作用
我有一些 SSE 程序,可以将循环中的内存归零,当指针未对齐时,它会引发 SIGSEGV进入我的处理程序。我可以在此类处理程序中获取更多信息吗例行公事,现在我不知道它是在哪里完成的,我也可以吗以某种可
我是一名优秀的程序员,十分优秀!