2017-09-25 40 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

相关问题