Android带有片段,重复元素

时间:2014-09-26 23:28:27

标签: android android-fragments

我正在构建Android应用程序(最低SDK级别10)。当应用程序最小化并且用户稍后返回时,主菜单会显示按钮和图像两次。它只发生在用户在中间使用其他应用程序时,如果它立即发生,则菜单显示正确。

以下是片段的代码:

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中定义View 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(),它就不再发生了。我不确定它为什么会起作用,但事实确实如此。谢谢大家!

1 个答案:

答案 0 :(得分:0)

问题不在你的onCreateView中(你应该使用EDIT 1)。它是关于嵌套碎片的。

Raneez Ahmed在这里发布了一个很好的答案 Fragments within Fragments