[英]Button on first fragment moves out of screen when replaced with second fragment
I am trying to use multiple Fragment
s on an Activity
. 我正在尝试在
Activity
上使用多个Fragment
。 What I am doing is: 1. Add the first Fragment
to the Activity
. 我正在做的是:1.将第一个
Fragment
添加到Activity
。 The first Fragment
contains a button at the bottom of the screen. 第一个
Fragment
在屏幕底部包含一个按钮。 2. Replace it with the second Fragment
by adding it to the backstack. 2.通过将其添加到后堆栈,用第二个
Fragment
替换它。
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
: 当我单击第一个
Fragment
底部的按钮时,它会自动向上滑动(我不知道为什么)并移动到第二个Fragment
:
When I go back to the first Fragment
by pressing the back button, the button in the first Fragment
goes out of view: 当我通过按“后退”按钮返回到第一个
Fragment
,第一个Fragment
的按钮不Fragment
:
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 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 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. 如果我从第二个
Fragment
移到第三个Fragment
(与第一个Fragment
相同),则第二个屏幕底部的按钮看起来会很好。 But the button on first Fragment
still goes out of the view. 但是第一个“
Fragment
上的按钮仍然不Fragment
。 The problem only exists in the view of the first Fragment
. 该问题仅存在于第一个
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.
我正在使用appcompact主题,当我替换片段时,我的底部布局不在屏幕上。 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.
因此,如果您还使用App Compact主题,请尝试其他主题。 Hope so it will solve your problem.
希望它能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.