[英]Android hide inflated view
我有1個SherlockMapActivity控制的4個視圖。 當前,我正在通過removeAllViews()在帶有選項卡的視圖之間切換,然后再次為視圖重新充氣。 這似乎是一種非常低效的解決方法。
有什么方法可以僅“隱藏”已經膨脹的視圖並在前面重新放置新視圖? 我嘗試了setVisibility等的所有變體,但均無濟於事。 這是我現在正在做的事情:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//load our views!
this.baseViewGroup = (ViewGroup)this.findViewById(android.R.id.content);
this.mapView = new MapView(ActivityMain.this, MAP_API_KEY);
this.mapView.setClickable(true);
this.createMenu();
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
Log.v(CLASS_NAME, "tab selected: "+tab.getPosition());
if (0 == tab.getPosition())
{
this.baseViewGroup.removeAllViews();
this.getLayoutInflater().inflate(R.layout.map, this.baseViewGroup);
}
else if (1 == tab.getPosition())
{
this.baseViewGroup.removeAllViews();
this.getLayoutInflater().inflate(R.layout.list, this.baseViewGroup);
}
}
然后,我可以使用ViewControllers(各種)來做一些花哨的事情,以在重新創建視圖時重新啟動視圖的先前狀態,但這簡直太瘋狂了。 有一個更好的方法嗎?
編輯我曾嘗試保存視圖(一次充值,刪除,然后重新添加),但出現這種奇怪的現象。 基本上, 所有展開的視圖都以半透明的方式彼此重疊顯示。 沒有任何setVisibility()使它們完全消失。
我嘗試過的代碼(在適當的地方添加到onCreate()和onTabSelected()中):
//in onCreate()
this.mapLayout = this.getLayoutInflater().inflate(R.layout.map, this.baseViewGroup);
this.moreLayout = this.getLayoutInflater().inflate(R.layout.more, this.baseViewGroup);
//in onTabSelected()
ViewGroup content = (ViewGroup)this.mapLayout.getParent();
content.removeAllViews();
content.addView(this.mapLayout);
不要一次又一次地增加視圖。 而是有4個類級別的視圖變量,例如
private View firstView;
private View secondView;
private View thirdView;
private View fourthView;
現在在每次切換/按下標簽時。 從父級移除所有子級視圖,然后向父級添加適當的視圖。 喜歡,
parentView.removeAllViews();
parentView.addView(secondView);
編輯:
為parentView傳遞null。
而不是這個
this.moreLayout = this.getLayoutInflater().inflate(R.layout.more, this.baseViewGroup);
做這個,
this.moreLayout = this.getLayoutInflater().inflate(R.layout.more, null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.