简体   繁体   中英

Button on first fragment moves out of screen when replaced with second fragment

I am trying to use multiple Fragment s on an Activity . What I am doing is: 1. Add the first Fragment to the Activity . The first Fragment contains a button at the bottom of the screen. 2. Replace it with the second Fragment by adding it to the backstack.

When I click on the button on the bottom of the first Fragment , it automatically slides up (I don't know why) and moves to second Fragment :

img1

When I go back to the first Fragment by pressing the back button, the button in the first Fragment goes out of view:

img2

MainActivity

public class MainActivity extends AppCompatActivity implements UserDetailsFragment.UserDetailsFragmentListener,
        PhotoFragment.PhotoFragmentListener, TestFragment.TestFragmentListener{

@Bind(R.id.container)
FrameLayout frameLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_profile);
    ButterKnife.bind(this);


    // However, if we're being restored from a previous state,
    // then we don't need to do anything and should return or else
    // we could end up with overlapping fragments.
    if (savedInstanceState != null) {
        return;
    }

    initView();
}

private void initView() {

    TestFragment testFragment = new TestFragment();

    FragmentUtil.replaceFragment(this,R.id.container, testFragment);
}

@Override
public void onProfileDetailCompleted(UserModel userModel) {
    PhotoFragment photoFragment = PhotoFragment.newInstance(userModel);
    FragmentUtil.replaceFragment(this, R.id.container, photoFragment);
}

@Override
public void onPhotoUploaded(UserModel userModel) {

}

@Override
public void onSkipPhotoClicked() {
    PhotoFragment photoFragment = PhotoFragment.newInstance(null);
    FragmentUtil.replaceFragment(this, R.id.container, photoFragment);
}

@Override
public void onTest() {
    PhotoFragment photoFragment = PhotoFragment.newInstance(null);
    FragmentUtil.replaceFragment(this, R.id.container, photoFragment);
}

}

TestFragment.java

public class TestFragment extends BaseFragment {


public TestFragment() {
    // Required empty public constructor
}

public interface TestFragmentListener {
    void onTest();
}

private TestFragmentListener mListener;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_test, container, false);
    ButterKnife.bind(this, view);
    return view;
}

@OnClick(R.id.next_btn)
public void onNextButtonClicked() {
    //TODO validate view


    if(mListener!=null)
        mListener.onTest();
}



@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof TestFragmentListener) {
        mListener = (TestFragmentListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement TestFragmentListener");
    }
}

}

FragmentUtils.java

public class FragmentUtil {

public static boolean hadFragment(AppCompatActivity activity) {
    return activity.getSupportFragmentManager().getBackStackEntryCount() != 0;
}

public static void replaceFragment(AppCompatActivity activity, int contentId, BaseFragment fragment) {
    FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();

    transaction.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
    if (hadFragment(activity)) {
        transaction.replace(contentId, fragment, fragment.getClass().getSimpleName());
    } else {
        transaction.add(contentId, fragment, fragment.getClass().getSimpleName());
    }

    transaction.addToBackStack(null);
    transaction.commit();
}

public static void removeFragment(AppCompatActivity activity, BaseFragment fragment) {
    activity.getSupportFragmentManager().beginTransaction()
        .remove(fragment)
        .commit();
}


public static void showFragment(AppCompatActivity activity, BaseFragment fragment) {
    activity.getSupportFragmentManager().beginTransaction()
        .show(fragment)
        .commit();
}

public static void hideFragment(AppCompatActivity activity, BaseFragment fragment) {
    activity.getSupportFragmentManager().beginTransaction()
        .hide(fragment)
        .commit();
}

public static void attachFragment(AppCompatActivity activity, BaseFragment fragment) {
    activity.getSupportFragmentManager().beginTransaction()
        .attach(fragment)
        .commit();
}

public static void detachFragment(AppCompatActivity activity, BaseFragment fragment) {
    activity.getSupportFragmentManager().beginTransaction()
        .detach(fragment)
        .commit();
}

}

If I move from the second Fragment to the third Fragment (which is the same as the first), the button on the bottom of the second screen will look fine. But the button on first Fragment still goes out of the view. The problem only exists in the view of the first Fragment . Please help.

i had somewhat similar issue. I was using the appcompact theme and my bottom layout was going out of the screen when i replace fragment. Then I tried different theme after that it worked perfectly fine. So if your are also using app compact theme then try any other theme. Hope so it will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM