Android setUserVisibleHint永远不会被调用?

编程入门 行业动态 更新时间:2024-10-28 04:21:25
本文介绍了Android setUserVisibleHint永远不会被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要知道何时可以看到我的片段,我正在使用setMenuVisibility,但现在知道这不是一个好选择.我正在尝试在FragmentStatePagerAdapter Fragment上实现setUserVisibleHint,但是它从未被调用.

I need to know when my fragment is visible, I was using setMenuVisibility but I now know it's not a good option. I'm trying to implement setUserVisibleHint on a FragmentStatePagerAdapter Fragment, however it never gets called.

import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; public class Contacts extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = inflater.inflate(R.layout.fragment_screen_contacts, container, false); return view; } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); Log.d("MyFragment", "This never shows up."); Toast.makeText(getActivity(), "Neither does this", Toast.LENGTH_LONG).show(); } }

我正在运行API级别19,并在我的AndroidManifest上将最低API级别设置为15.还有什么要做的来获取setUserVisibleHint,我在做什么错了?

I'm running API level 19, and set a minimum API Level of 15 on my AndroidManifest. Is there anything else to do to get setUserVisibleHint, what am I doing wrong?

推荐答案

setUserVisibleHint可用,因此您可以通过某种方式告诉系统该片段实际上是不可见的,反之亦然专门隐藏交易的碎片交易.您不能使用它来确定可见性,并且它默认为true.

setUserVisibleHint is available so that you have a way to tell the system the fragment is in fact not visible and not the other way around, when you are doing some fancy fragment transactions that specifically hide it. You can't use it to determine visibility and it defaults to true.

您应该使用isVisible调用来了解该片段是否可见以及该片段或根视图类的onAttach,以便在将其附加到活动或相应的根视图时获取回调.

You should use the isVisible call to know if the fragment is visible and onAttach of the fragment or the root view classes to get callbacks when it's been attached to the activity or the respective root views.

更多推荐

Android setUserVisibleHint永远不会被调用?

本文发布于:2023-11-08 09:41:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   Android   setUserVisibleHint

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!