2017-09-25 41 views
0

我需要實現一個片段,它執行處理不同數據集的函數,並且還需要不同的佈局文件來呈現多個視圖。我想爲所有與多個佈局視圖關聯的數據的後端操作實現一個公共片段。我該怎麼做?如何在一個片段中使用多個佈局文件?

+0

喜歡的東西,與多個XML佈局文件,我可以與主XML佈局文件切換一個活動適當 – vinitpradhan18

回答

0

取而代之的是你可以試試這個解決方案

與所有做你的問題(後端操作)所提到的所需片段即任務的一些常見任務的方法創建基礎片段。

使用不同的佈局創建儘可能多的片段,並擴展基礎片段。

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 
    } 

} 
+0

我認爲這會工作。我會盡力讓你知道。 – vinitpradhan18

+0

這將工作,我希望..我知道如果你有問題 – Anonymous

0

您可以根據膨脹在自己調節不同的佈局,像這樣:

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; 
} 
0

最好的辦法是隻使用一個活動即;主要活動。在這個主要活動中,您可以根據您的數據加載多個片段。 讓我告訴你如何。

假設你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 
} 
+0

我已經在使用導航抽屜活動。在該活動中,我已經在爲每個菜單項使用片段。現在在這些片段中,我需要實現多個佈局功能。 – vinitpradhan18

相關問題