当用第二个片段替换时,第一个片段上的按钮移出屏幕

时间:2016-05-31 17:36:00

标签: java android android-fragments android-fragmentactivity

我正在尝试在Fragment上使用多个Activity。我在做的是: 1.将第一个Fragment添加到Activity。第一个Fragment包含屏幕底部的按钮。 2.将其替换为第二个Fragment,将其添加到Backstack中。

当我点击第一个Fragment底部的按钮时,它会自动向上滑动(我不知道原因)并移至第二个Fragment

img1

当我按后退按钮返回第一个Fragment时,第一个Fragment中的按钮就会消失:

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();
}

}

如果我从第二个Fragment移动到第三个Fragment(与第一个Fragment相同),第二个屏幕底部的按钮看起来会很好。但是第一个Fragment上的按钮仍然不在视图范围内。该问题仅存在于第一个<audio controls> <source src="" type="audio/ogg"> <source src="" type="audio/mpeg"> <source src="test1.wav" type="audio/wav"> </audio> 的视图中。请帮忙。

1 个答案:

答案 0 :(得分:0)

我有点类似的问题。我正在使用appcompact主题,当我替换片段时,我的底部布局已经离开了屏幕。然后我尝试了不同的主题,之后它工作得非常好。所以如果你也使用app紧凑主题,那么尝试任何其他主题。希望这样能解决你的问题。