[英]to execute fragment when visible but not recreated
我有這個問題:
目前,我使用BottomNavigationView瀏覽片段...
碼:
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//Fragment fragment = null;
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_container_home);
switch (item.getItemId()){
case R.id.ic_home:
if (currentFragment instanceof MainFragment){
//execute code
}
if(fragmentManager.findFragmentByTag("mainMenu") != null) {
//if the fragment exists, show it.
fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("mainMenu")).commit();
toolbar.setTitle("Flip");
item = menu.getItem(0);
item.setChecked(true);
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.fragment_container_home, new MainFragment(), "mainMenu").commit();
toolbar.setTitle("Flip");
item = menu.getItem(0);
item.setChecked(true);
}
//Hide Others Fragments
if(fragmentManager.findFragmentByTag("categories") != null){
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("categories")).commit();
}
if(fragmentManager.findFragmentByTag("feedUsers") != null){
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("feedUsers")).commit();
}
if(fragmentManager.findFragmentByTag("explore") != null){
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("explore")).commit();
}
if(fragmentManager.findFragmentByTag("opcoes") != null){
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("opcoes")).commit();
}
break;
}
return false;
}
});
我需要的:
單擊時運行一些代碼,可見的片段就是菜單片段本身。
在這:
if (currentFragment instanceof MainFragment){
//execute code
}
我可以僅在片段重新出現而不重新創建時執行一些代碼嗎?
一種替代方法是訪問recyclerview或發送命令以在片段中執行
當在BottomNavigationView中單擊兩次時,我需要訪問RecyclerView Fragment才能將其發送到屏幕頂部。
我想你有不止一種方法
首先,在顯示片段后調用所需的代碼,非常簡單
if(fragmentManager.findFragmentByTag("mainMenu") != null) {
//if the fragment exists, show it.
fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("mainMenu")).commit();
toolbar.setTitle("Flip");
item = menu.getItem(0);
item.setChecked(true);
////////////////////////////////////
// execute the code you want here //
////////////////////////////////////
}
在顯示/隱藏fragment
並事先檢查其visibility
之后,無法設置fragment
的visibility
的第二種方法
在fragment
顯示后添加setVisibility
VISIBLE
fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("mainMenu")).commit();
currentFragment.getView().setVisibility(View.VISIBLE);
在fragment
隱藏后添加setVisibility
GONE
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("categories")).commit();
currentFragment.getView().setVisibility(View.GONE);
並根據需要檢查fragment
的Visibility
if(currentFragment != null){
if(currentFragment().getView().getVisibility() == View.VISIBLE){
// fragment is shown, do some action
}else {
// fragment is hiden, do some action
}
}
如果您無法檢查它並且沒有觸發器來檢查visibility
,則可以使Runnable
每1秒或您想要的任何時間檢查Visibility
第三種方式 ,非常有效率。 使用onResume
但只有在fragment
未運行時才調用onResume
除非您手動調用它。
if(fragmentManager.findFragmentByTag("mainMenu") != null) {
//if the fragment exists, show it.
fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("mainMenu")).commit();
toolbar.setTitle("Flip");
item = menu.getItem(0);
item.setChecked(true);
currentFragment().onResume();
}
//Hide Others Fragments
if(fragmentManager.findFragmentByTag("categories") !=
null){
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("categories")).commit();
currentFragment().onStop();
}
然后
@Override
public void onResume() {
super.onResume();
// your code
}
第三條道路的一些麻煩;
僅當片段在運行時不包含值並且要保留此值,並且在運行時不要使用這些值(除非它們是static
時,才使用這種方式
exp; 如果您在運行時從bitmap
imageView
加載到某個fragment
並隱藏了該fragment
(如您在代碼中所做的那樣),並且在再次顯示該fragment
,您將找到imageView
因為該fragment
在background
運行,另一方面如果調用,則終止該fragment
並重新啟動它,則不會找到位圖或ImageView
。 這就是使用onStop
方法。 它會停止fragment
所有內容( bitmap
和imageView
),並保存直到fragment
RESUME無法開始。
您可以onHiddenChanged(boolean)
重寫onHiddenChanged(boolean)
。
@Override
public void onHiddenChanged(boolean isVisible) {
... // handle fragment visibility change here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.