我需要實現一個片段,它執行處理不同數據集的函數,並且還需要不同的佈局文件來呈現多個視圖。我想爲所有與多個佈局視圖關聯的數據的後端操作實現一個公共片段。我該怎麼做?如何在一個片段中使用多個佈局文件?
回答
取而代之的是你可以試試這個解決方案
與所有做你的問題(後端操作)所提到的所需片段即任務的一些常見任務的方法創建基礎片段。
使用不同的佈局創建儘可能多的片段,並擴展基礎片段。
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