Java 如何在片段中使用多个布局文件?
我需要实现一个片段,它执行一个处理不同数据集的函数,还需要不同的布局文件来呈现多个视图。我想为与多个布局视图关联的数据的所有后端操作实现一个公共片段。我该怎么做呢?你可以试试这个解决方案 使用所有方法创建一个基本片段,这些方法执行片段所需的一些常见任务,即问题(后端操作)中提到的任务 使用不同的布局创建所需的片段,并扩展基本片段Java 如何在片段中使用多个布局文件?,java,android,android-fragments,Java,Android,Android Fragments,我需要实现一个片段,它执行一个处理不同数据集的函数,还需要不同的布局文件来呈现多个视图。我想为与多个布局视图关联的数据的所有后端操作实现一个公共片段。我该怎么做呢?你可以试试这个解决方案 使用所有方法创建一个基本片段,这些方法执行片段所需的一些常见任务,即问题(后端操作)中提到的任务 使用不同的布局创建所需的片段,并扩展基本片段 public class BaseFragment extends Fragment { @Override public View onCreate
public class BaseFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater,container,savedInstanceState);
}
//Add your backend operations for data and common methods
}
public class FragmentA extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Add your first layout here
}
}
public class FragmentB extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Add your second layout here
}
}
相反,您可以尝试此解决方案 使用所有方法创建一个基本片段,这些方法执行片段所需的一些常见任务,即问题(后端操作)中提到的任务 使用不同的布局创建所需的片段,并扩展基本片段
public class BaseFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater,container,savedInstanceState);
}
//Add your backend operations for data and common methods
}
public class FragmentA extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Add your first layout here
}
}
public class FragmentB extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Add your second layout here
}
}
您可以根据自己的条件为不同的布局充气,如下所示:
View mView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(condition_one())
mView = inflater.inflate(R.layout.condition_one_layout,container,false);
else if(condition_two())
mView = inflater.inflate(R.layout.condition_two_layout,container,false);
.
.
.
return mView;
}
您可以根据自己的条件为不同的布局充气,如下所示:
View mView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(condition_one())
mView = inflater.inflate(R.layout.condition_one_layout,container,false);
else if(condition_two())
mView = inflater.inflate(R.layout.condition_two_layout,container,false);
.
.
.
return mView;
}
最好的方法是只使用一项活动,即:;主要活动。在这个主要活动中,您可以根据数据加载多个片段。 让我告诉你怎么做 假设您的activity_main.xml为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
这里我所做的是,如果您的消息有一个字符串first_片段,它必须加载first_片段。类似地,如果您的消息有一个字符串second\u fragment,那么它必须加载second\u fragment
现在,是实现第一个_FRAGMENT.java的时候了:
public class FIRST_FRAGMENT extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.first_fragment, null);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// buttons or textview of fragment must be initialised here.
}
同样,第二个_片段将是相同的
现在假设您想检查当前加载的片段。
你可以这样做
final FIRST_FRAGMENT myFragment = (FIRST_FRAGMENT) getSupportFragmentManager().findFragmentByTag("FIRST FRAGMENT");
if (myFragment != null && myFragment.isVisible()) {
myFragment.handleMessage(message);
// here handleMessage is the function declared in FIRST_FRAGMENT
}
最好的方法是只使用一项活动,即:;主要活动。在这个主要活动中,您可以根据数据加载多个片段。 让我告诉你怎么做 假设您的activity_main.xml为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
这里我所做的是,如果您的消息有一个字符串first_片段,它必须加载first_片段。类似地,如果您的消息有一个字符串second\u fragment,那么它必须加载second\u fragment
现在,是实现第一个_FRAGMENT.java的时候了:
public class FIRST_FRAGMENT extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.first_fragment, null);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// buttons or textview of fragment must be initialised here.
}
同样,第二个_片段将是相同的
现在假设您想检查当前加载的片段。
你可以这样做
final FIRST_FRAGMENT myFragment = (FIRST_FRAGMENT) getSupportFragmentManager().findFragmentByTag("FIRST FRAGMENT");
if (myFragment != null && myFragment.isVisible()) {
myFragment.handleMessage(message);
// here handleMessage is the function declared in FIRST_FRAGMENT
}
比如,一个包含多个xml布局文件的活动,我可以适当地与主xml布局文件切换,一个包含多个xml布局文件的活动,我可以适当地与主xml布局文件切换。我希望它可以工作。如果有问题,请告诉我。如果有问题,请告诉我。我已经在使用导航抽屉活动。在这个活动中,我已经为每个菜单项使用了片段。现在在这些片段中,我需要实现多布局功能。我已经在使用导航抽屉活动。在这个活动中,我已经为每个菜单项使用了片段。现在在这些片段中,我需要实现多个布局功能。