简体   繁体   English

从子片段调用父视图 - 视图寻呼机

[英]Calling parent view from child fragment - view pager

I have a fragment with a searchBox and underneath that viewpager is placed , I get access to the searchBox from child like this : (this.parentFragment as TicketListFragment).binding.searchBpx.setOnTextChange {}我有一个带有 searchBox 的片段,并在该视图页面下方放置了一个片段,我可以像这样从子级访问 searchBox : (this.parentFragment as TicketListFragment).binding.searchBpx.setOnTextChange {}

in each child fragment everthing works fine , the problem is here: Fragemnt A: (this.parentFragment as TicketListFragment).binding.searchBpx.setOnTextChange { Log.d("accessing to the searchBox from frag a") }在每个子片段中一切正常,问题在这里: Fragemnt A: (this.parentFragment as TicketListFragment).binding.searchBpx.setOnTextChange { Log.d("accessing to the searchBox from frag a") }

Fragemnt B: (this.parentFragment as TicketListFragment).binding.searchBpx.setOnTextChange { Log.d("accessing to the searchBox from frag B") } Fragemnt B: (this.parentFragment as TicketListFragment).binding.searchBpx.setOnTextChange { Log.d("accessing to the searchBox from frag B") }

when i go to frag B , and get back to the frag A , the searchBox.setOnTextChange is called from frag B !!, any idea to fix this issue?当我转到 frag B 并返回 frag A 时,从 frag B 调用searchBox.setOnTextChange !!,有解决此问题的想法吗?

You are accessing the parent search view from its children FragmentA and FragmentB.您正在从其子 FragmentA 和 FragmentB 访问父搜索视图。 And you set the setOnTextChange on both the fragment.并且您在两个片段上都设置了setOnTextChange

So before going to another fragment you should remove the listener from the current fragment.因此,在转到另一个片段之前,您应该从当前片段中删除侦听器。

For Example : going From A -> B: Remove the setOnTextChange from Fragment A then proceed to B.例如:从 A -> B:从片段 A 中删除setOnTextChange然后继续到 B。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM