一个活动能否以相同的方法使用多个片段之一?

时间:2019-02-26 17:46:45

标签: android android-fragments

一个活动能否以相同的方法使用多个片段之一?

我有许多片段,可以接受用户输入的各种信息。每个片段都是不同的,它们的使用由用户选择的选项卡确定。我目前正在使用switch语句来选择要在“活动”中使用的适当片段:

private static Fragment fragment;

    FragmentManager fm = getSupportFragmentManager();
    fragment = fm.findFragmentById(R.id.fragment_container);
    boolean validTab = true;

    switch (tabSelected){
        case "Procedures":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new ProcedureRecordsFragment();
            Log.d(TAG, "fragment is: " + fragment);
            break;
        case "Prescriptions":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new PrescriptionRecordsFragment();
            Log.d(TAG, "fragment is: " + fragment);
            break;
        case "Immunizations":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new ImmunizationRecordsFragment();
            break;
        case "Medical Encounters":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new MedicalEncounterRecordsFragment();
            break;
        case "Allergies":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new AllergyRecordsFragment();
            break;
        case "Injuries":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new InjuryRecordsFragment();
            break;
        case "Obstetric History":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new ObstetricRecordsFragment();
            break;
        case "Family History":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new FamilyHistoryRecordsFragment();
            break;
        case "Social History":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new SocialHistoryRecordsFragment();
            break;
        case "Habits":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new HabitRecordsFragment();
            break;
        case "Development History":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new DevelopmentRecordsFragment();
            break;
        case "Demographics":
            Log.d(TAG, "To enter: " + tabSelected + "records");
            fragment = new DemographicRecordsFragment();
            break;*/
        default:
            Log.d(TAG, "Invalid tab reference");
            validTab = false;
            break;
        }
        if(validTab == true){
            fragment.setArguments(args);
            fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
        }else if(validTab == false){
            Log.d(TAG, "Do nothing, invalid tab reference");
        }

这将成功显示由所选选项卡确定的正确片段。

每个Fragment都具有返回用户输入的相同方法,此刻它只是一个字符串:

public String getUserInput(){
    return("String from the fragment!");
}

通过活动的getUserInout方法访问Fragments的saveRecord方法,该方法是在成功显示每个Fragment之后添加的:

public void saveRecord(MenuItem item){
    Log.d(TAG, "Entered: saveRecord");
    String s = fragment.getUserInput();
    Log.d(TAG, "String s says: " + s);
    Log.d(TAG, "fragment is: " + fragment);
}

指令String s = fragment.getUserInput();导致Gradle构建“无法解析方法getUserInput” 错误。当我同时注释掉String s = fragment.getUserInput();Log.d(TAG, "String s says: " + s);并运行代码时,Log.d(TAG, "fragment is: " + fragment)显示由Gradle生成的选定片段的引用:例如 D / MedicalRecordActivity:片段为:ProcedureRecordsFragment { 6c827fa} (对于案例实例), D / MedicalRecordActivity:片段是:ProcedureRecordsFragment {6c827fa#0 id = 0x7f080084} (对于saveRecord方法)。

这向我表明Gradle生成的代码创建了由选项卡选择确定的适当片段的一个实例。在我的研究中,我发现没有任何东西可以使用许多片段之一为我提供有关活动的信息,这些片段可以为活动提供对活动片段方法的访问。谁能指出我这些信息?

1 个答案:

答案 0 :(得分:2)

您要寻找的是interface。本质上,这是一个班级可以声称提供的合同。因此,在您的情况下,您具有提供用户输入的类的合同。您可以使用类似这样的内容:

interface UserInputProvider {
  public String getUserInput();
}

然后,您的Fragment类仅需要实现该接口,并标记为它们从接口覆盖该方法:

class MyFragment implements UserInputProvider {
  @Override
  public String getUserInput() {
    // your code to get input here
  }
}

然后,您可以将Fragment转换为UserInputProvider(如果适用):

public void saveRecord(MenuItem item) {
  if (fragment instanceof UserInputProvider) {
    String input = ((UserInputProvider) fragment).getUserInput();
  }
}