2014-09-26 72 views
1

我正在構建一個Android應用程序(最低SDK級別10)。當應用程序最小化並且用戶稍後返回時,主菜單會顯示兩次按鈕和圖像。只有當用戶在中間使用其他應用程序時纔會發生,如果它立即發生,則菜單正確顯示。Android與片段,重複元素

這裏是片段的代碼:

public class MainMenuFragment extends Fragment implements OnClickListener { 
String mGreeting = "Hello, anonymous user (not signed in)"; 

public interface Listener { 
    public void onStartGameRequested(boolean hardMode); 
    public void onShowAchievementsRequested(); 
    public void onShowLeaderboardsRequested(); 
    public void onSignInButtonClicked(); 
    public void onSignOutButtonClicked(); 
    public void onPlayClicked(); 
    public void onScoreClicked(); 
    public void onFeedbackClicked(); 
} 

Listener mListener = null; 
boolean mShowSignIn = true; 

View v; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    if(v==null){ 
     v = inflater.inflate(R.layout.fragment_mainmenu, container, false); 
     final int[] CLICKABLES = new int[] { 
       R.id.show_leaderboards_button, 
       R.id.sign_in_button, R.id.sign_out_button, R.id.button_play, R.id.feedback 
     }; 
     for (int i : CLICKABLES) { 
      v.findViewById(i).setOnClickListener(this); 
     } 
    } 
    else { 
      ((ViewGroup)v.getParent()).removeView(v); 
     } 
    return v; 
} 

public void setListener(Listener l) { 
    mListener = l; 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    //updateUi(); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    updateUi(); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
} 

public void setGreeting(String greeting) { 
    mGreeting = greeting; 
    //updateUi(); 
} 

void updateUi() { 
    try{ 
     if (getActivity() == null) return; 

    getActivity().findViewById(R.id.sign_in_button).setVisibility(mShowSignIn ? 
      View.VISIBLE : View.GONE); 
    getActivity().findViewById(R.id.sign_out_button).setVisibility(mShowSignIn ? 
      View.GONE : View.VISIBLE); 
    }catch(Exception e){} 
} 

@Override 
public void onClick(View view) { 
    switch (view.getId()) { 
    case R.id.show_leaderboards_button: 
     mListener.onShowLeaderboardsRequested(); 
     break; 
    case R.id.sign_in_button: 
     mListener.onSignInButtonClicked(); 
     break; 
    case R.id.sign_out_button: 
     mListener.onSignOutButtonClicked(); 
     break; 
    case R.id.feedback: 
     mListener.onFeedbackClicked(); 
     break; 
    case R.id.button_play: 
     mListener.onPlayClicked(); 
     break; 
    } 
} 

public void setShowSignInButton(boolean showSignIn) { 
    mShowSignIn = showSignIn; 
    updateUi(); 
} 
} 

預先感謝您的任何幫助。我看不到所謂的兩次,我花了比我承認的時間還多的時間。

編輯1:嘗試克林特Deygoo解決方案。在onCreate內部定義視圖v也不工作。我是這樣做的:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.fragment_mainmenu, container, false); 
     final int[] CLICKABLES = new int[] { 
       R.id.show_leaderboards_button, 
       R.id.sign_in_button, R.id.sign_out_button, R.id.button_play, R.id.feedback 
     }; 
     for (int i : CLICKABLES) { 
      v.findViewById(i).setOnClickListener(this); 
     } 
     return v; 
} 

編輯2:已解決的問題。我刪除updateUi()並停止發生。我不確定它爲什麼有效,但它是。謝謝你們!

+0

嘗試將視圖V的onCreateView內部,這是局部的功能。每次調用onCreateView時都會膨脹v。 – 2014-09-27 02:18:33

回答

0

問題不在於你的onCreateView(你應該使用編輯1)。這是關於嵌套碎片。

Raneez艾哈邁德貼一個很好的答案在這裏 Fragments within Fragments

+0

謝謝你的回答@ adam-wigren。我認爲這可能會導致我找到解決方案,但我還沒有找到辦法。我認爲我不應該膨脹到片段的佈局,正如這裏所說[鏈接](http://developer.android.com/about/versions/android-4.2.html#NestedFragments),但我將如何setOnCLickListener沒有視圖的按鈕? – 2014-10-04 20:21:51