我是这个片段理论的新手,我似乎无法理解我们从哪里得到的R.id.container所以我想在单击按钮后开始一个片段。这是我的方法
我的主要方法,我没有添加任何代码来启动我的片段类。
callCenter.setOnClickListener(view -> openCallCenter());
//on clicked open call center which should start a fragment
private void openCallCenter() {
}
我的片段类:暂时为空;
public class CallCenterFragment extends Fragment {
public static final String TAG = CallCenterFragment.class.getSimpleName();
public CallCenterFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_call_center, container, false);
}
}
我的片段布局现在为空:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="@+id/fragment_container"
tools:context=".fragment.CallCenterFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
SO:我最大的问题是关于我的 mainActivity 类,我应该添加什么代码来启动这个片段,有人可以向我解释一下,因为我已经看到了几个例子,我们从哪里得到transaction.replace(R.id.container)
提前致谢。