我需要实现一个片段,它执行处理不同数据集的函数,并且还需要不同的布局文件来呈现多个视图。我想为所有与多个布局视图关联的数据的后端操作实现一个公共片段。我该怎么做?如何在一个片段中使用多个布局文件?
回答
取而代之的是你可以试试这个解决方案
与所有做你的问题(后端操作)所提到的所需片段即任务的一些常见任务的方法创建基础片段。
使用不同的布局创建尽可能多的片段,并扩展基础片段。
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
}
}
我认为这会工作。我会尽力让你知道。 – vinitpradhan18
这将工作,我希望..我知道如果你有问题 – Anonymous
您可以根据膨胀在自己调节不同的布局,像这样:
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>
该活动由一个线性布局,其内部具有一个片段。 现在取决于您的数据,您可以在此内部线性布局中加载多个片段。
在你MainActivity.java,您可以通过应用检查负载多个片段:
if(message.contains("first_fragment"))
{
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
final FIRST_FRAGMENT fragment_activity = new FIRST_FRAGMENT();
fragmentTransaction.replace(R.id.fragment_container,fragment_activity,"FIRST FRAGMENT");
fragmentTransaction.commit();
}
else if(message.contains("second_fragment"))
{
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
final SECOND_FRAGMENT fragment_activity = new SECOND_FRAGMENT();
fragmentTransaction.replace(R.id.fragment_container,fragment_activity,"SECOND FRAGMENT");
fragmentTransaction.commit();
}
在这里我做了什么,如果你的消息有一个字符串first_fragment,它必须加载FIRST_FRAGMENT。同样,如果你的消息有一个字符串second_fragment,它必须加载SECOND_FRAGMENT。现在
,时间来实现你的FIRST_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.
}
同样,SECOND_FRAGMENT将是相同的。
现在假设您想检查当前加载哪个片段。 你可以这样做。:
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
}
我已经在使用导航抽屉活动。在该活动中,我已经在为每个菜单项使用片段。现在在这些片段中,我需要实现多个布局功能。 – vinitpradhan18
- 1. 使用一个布局文件的多个片段
- 2. 片段的多个布局
- 3. 多布局viewpager与一个片段
- 4. 一个片段中的两个布局
- 5. 如何在同一个布局中使用多个GLSurfaceView组件?
- 6. 多个片段/布局在一个Tabb的actionbarsherlock
- 7. 设置多个片段的布局
- 8. 如何在单个布局中动态添加2个片段?
- 9. 使用xml布局使用多个textview创建一个段落
- 10. 是否需要在一个.xml文件中使用多个布局(线性布局,相对布局)
- 11. 如何移动到另一个布局在同一标签使用片段
- 12. 添加布局到一个片段programmaticcaly
- 13. 动作条具有多个片段:片段布局上移
- 14. 如何在同一个控制器内使用多个布局?
- 15. 如何使用一个RecyclerView适配器使用多个布局?
- 16. 在xml布局文件中声明片段并使用FragmentTransaction.replace
- 17. 在一个文件中多个片段崇高文本2
- 18. 重复使用多个片段中的一个完整片段
- 19. 使用片段布局
- 20. 在同一个可滚动布局/片段中添加多个GridView
- 21. 如何在同一个布局中垂直加载两个片段?
- 22. 如何使用片段布局填充我的内容布局
- 23. 如何在android布局的片段中添加两个listviews?
- 24. 布局片段的FrameLayout换成另一个片段的FrameLayout
- 25. Android ViewPager w /多个片段类每个w /独立的布局文件
- 26. 使用布局xml动态地膨胀一个片段,找不到片段
- 27. 使用自定义布局在一个.pdf文件中组合多个数字?
- 28. 多个MvxBindableListView在一个布局中?
- 29. 如何在单个布局中使用wrap_content设置多个ListView?
- 30. 如何在android的单个活动中使用多个布局?
喜欢的东西,与多个XML布局文件,我可以与主XML布局文件切换一个活动适当 – vinitpradhan18