DiscFragment.java:(更新)
public class DiscFragment extends DagFragment {
// @BindView(R.id.dispFrag)
// LinearLayout discDispLayout;
@BindView(R.id.get_button)
Button getButton;
@NonNull
@Override
public String getFragmentTitle(@NonNull Context context) {
return context.getString(R.string.ex_disp);
}
@OnClick(R.id.getbutton)
void onGetClicked() {
DiscDispFragment fragment = new DiscDispFragment();
FragmentManager fmt = getChildFragmentManager();
if (fmt != null) {
FragmentTransaction fragmentTransaction = fmt.beginTransaction();
fragmentTransaction.replace(R.id.discover, fragment);
fragmentTransaction.commit();
}
}
@Override
protected int getFragmentLayout() {
return R.layout.get_started;
}
}
DiscDispFragment.java:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(getFragmentLayout(), container, false);
ButterKnife.bind(this, rootView);
return rootView;
}
@Override
protected int getFragmentLayout() {
return R.layout.dispFragment;
}
dispFragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/background_color"
android:layoutDirection="locale"
android:orientation="vertical">
<LinearLayout
android:id="@+id/dispFrag"
android:layout_width="match_parent"
android:layout_height="750dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/cancel_button"
android:layout_width="@dimen/progress_bar_small"
android:layout_height="@dimen/progress_bar_small"
android:background="?attr/bg_color"
android:clickable="true"
android:contentDescription="@string/st_away"
android:scaleType="center"
android:tint="@color/white"
android:visibility="visible"
app:srcCompat="@drawable/st_close" />
<TextView
android:id="@+id/st_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="17dp"
android:layout_marginBottom="151dp"
android:background="@drawable/bottom_sheet_background"
android:text="@string/st_to"
android:textAlignment="center"
android:textColor="@color/gray_color"
android:textSize="@dimen/pad_12"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="@dimen/progress_bar"
android:layout_height="@dimen/progress_bar"
android:layout_gravity="center"
android:layout_marginTop="@dimen/element_margin"
android:indeterminate="true"
android:progressBackgroundTint="#606067"
android:progressTint="#8e8e93" />
<TextView
android:id="@+id/disp_state_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/element_margin"
android:gravity="center"
android:paddingStart="@dimen/element_margin"
android:paddingEnd="@dimen/element_margin"
android:text="@string/progress_message"
android:textColor="@color/black_color"
android:textSize="@dimen/st_size" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_error_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/element_margin"
android:background="@color/butn_background"
android:padding="@dimen/butn_padding"
android:textColor="@color/white"
tools:text="@string/hello" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/discays"
android:layout_width="match_parent"
android:layout_height="145dp"
tools:listitem="@layout/item_discovered_display_item" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
get_started.xml :(已更新)
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/round_corners">
<LinearLayout
android:id="@+id/des_get_started"
android:layout_width="match_parent"
android:layout_height="675dp"
android:layout_gravity="center"
android:layout_marginBottom="51dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="51dp" />
<ImageView
android:id="@+id/sheet_image"
android:layout_width="232dp"
android:layout_height="177dp"
android:layout_gravity="center"
android:layout_marginTop="51dp"
android:layout_marginBottom="32dp"
android:contentDescription="@string/disp_name"
android:padding="16dp"
android:src="@drawable/im"
android:textAlignment="center" />
<TextView
android:id="@+id/sheet_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:padding="16dp"
android:text="@string/st_away"
android:textAlignment="center"
android:textColor="@color/darktheme"
android:textSize="@dimen/s21"
android:textStyle="bold" />
<TextView
android:id="@+id/sheet_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="24dp"
android:padding="16dp"
android:text="@string/st_desc"
android:textAlignment="center"
android:textColor="@color/black_darktheme"
android:textSize="@dimen/s_21" />
<Button
android:id="@+id/get_started_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/get_started_background"
android:padding="16dp"
android:text="@string/get_started_button"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="@dimen/size_15dp" />
</LinearLayout>
<FrameLayout
android:id="@+id/discover"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
例外情况:
2020-09-23 20:00:23.509 4897-4897/com.misc.dd E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.misc.dd, PID: 4897
java.lang.IllegalArgumentException: No view found for id 0x184f (unknown) for fragment DiscDispFragment{58583d9} (17f05957-c8f3-4a58-9405-0f7ef88f401d) id=0x184f}
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:315)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
当您写:
DiscDispFragment fragment = new DiscDispFragment();
FragmentManager fmt = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fmt.beginTransaction();
fragmentTransaction.replace(R.id.dispFrag, fragment);
您的意思是您想要:
DiscDispFragment
实例getChildFragmentManager()
方法,您希望新片段完全包含在父片段的视图中-在这种情况下,您DiscFragment
的getting_started
布局R.id.dispFrag
new 替换容器中任何现有的Fragment DiscDispFragment
,将DiscDispFragment
的布局添加到该容器中。你得到一个错误的原因是,R.id.dispFrag
是不是一个FrameLayout
等ViewGroup
你内DiscFragment
的布局。
为了解决这个问题,您有两种选择:
DiscDispFragment
的子片段作为DiscFragment
,则需要在布局中添加FrameLayout
或FragmentContainerView
,这就是您DiscDispFragment
要添加到的布局。DiscDispFragment
成为一个孩子片段,那么你需要不使用getChildFragmentManager()
。requireActivity().getSupportFragmentManager()
将是活动的FragmentManager(您将在“活动”的布局中使用容器ID的位置),或者将使用getParentFragmentManager()
/ getFragmentManager()
(取决于您使用的Fragments的版本)来访问FragmentManager
添加DiscFragment
到其中的内容(在使用您最初添加DiscFragment
到的任何容器ID )。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句