我创建了一个应用程序,其中我有 1 个 ACTIVITY 和 8 个 fragment ,我在其中将每个 fragment 膨胀到 Activity 中。但我的问题是每个 fragment 都相互重叠。我更改了每个 fragment 的背景颜色,但是当我单击前一个 fragment 的空白按钮时,它会被点击。
请给我一些建议来摆脱这种情况。
Main_Activity.class
public class ActRegistration extends AppCompatActivity {
FragmentManager fm;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_registration);
fm = getSupportFragmentManager();
loadFragment(new FragmentRegLogSkip(), FragmentRegLogSkip.FRAGMENT_KEY);
}
public void loadFragment(Fragment fragmentName, String fragmentKey) {
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.id_registration_layout_frame, fragmentName);
fragmentTransaction.addToBackStack(fragmentKey);
fragmentTransaction.commit();
}
}
Frag_1.class
public class FragmentRegLogSkip extends Fragment implements View.OnClickListener {
private Activity activity;
private Context context;
public static final String FRAGMENT_KEY = "fragment_reg_log_skip";
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = getContext();
LanguageChanger.changeLanguage(context);
View view = inflater.inflate(R.layout.act_registration_screen_login, container, false);
return view;
}
}
最佳答案
不要忘记将 android:clickable="true"
和 android:focusable="true"
添加到第二个 fragment 的父 View 中,以便它捕获点击它们不会传播到下面的 fragment 。像这样:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
关于android - fragment 相互重叠......并且前一个 fragment 正在点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54977885/