我正在尝试在Fragment
上使用多个Activity
。我在做的是:
1.将第一个Fragment
添加到Activity
。第一个Fragment
包含屏幕底部的按钮。
2.将其替换为第二个Fragment
,将其添加到Backstack中。
当我点击第一个Fragment
底部的按钮时,它会自动向上滑动(我不知道原因)并移至第二个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
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>
的视图中。请帮忙。
答案 0 :(得分:0)