如果我在嵌套片段中,我如何从父级的任何片段中获取值?

时间:2016-05-07 02:05:43

标签: java android android-fragments

编辑:这里我试图将getParent()与getChildFragmentManager()结合使用。

setSchemes.java

// .getSpinnerValue() is red in the IDE, the tooltip says "cannot resolve method 'getSpinnerValue()'
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
String parentSpinnerValue = fragmentTransaction.getParent().getSpinnerValue();

我将有许多片段实例,唯一不同的是微调器值。这些片段中的每一个都会在按钮单击时添加嵌套片段的许多实例。我需要我的嵌套片段来“知道”它们属于哪个父片段。所以我想知道嵌套片段的每个实例是否有办法调用创建它的任何片段并从中获取微调器值。

我不确定这是否是需要代码的问题,但我会在这里添加一些上下文。如果需要更多,我很乐意编辑内容。

所以,这里是父片段。我添加了这样的嵌套实例(这是在onCreateView中):

addSet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ++fragIdCount2;
            FragmentManager fragmentManager = getFragmentManager();
            String fragString2 = Integer.toString(fragIdCount2);
            setSchemes frag1 = new setSchemes();
            FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.LinearLayoutChild1, frag1, fragString2);
            fragmentTransaction.commit();
            fragmentManager.executePendingTransactions();

        }
    });

在onPause中,我可以得到如下的微调器值:

Spinner exerciseSpinner = (Spinner) getView().findViewById(R.id.movementName);
    String spinnerText = exerciseSpinner.getSelectedItem().toString();

我需要能够从嵌套片段中获取该微调器值,所以一旦我将所有嵌套实例的值传递到我的控制器对象中,它们就可以携带它们的父片段的微调器值。然后,我可以根据父级的微调器值将嵌套实例放在右侧数组中。

编辑:所以这是我当前状态下的代码。我只是简单地使用我的真实文件名

movementSetsRepsFrag.java(这是父片段)

@Override
public void onPause() {
    super.onPause();



    Spinner exerciseSpinner = (Spinner) getView().findViewById(R.id.movementName);
    String spinnerText = exerciseSpinner.getSelectedItem().toString();



    ControllerData.getInstance().setExSpinnerValue1(spinnerText);

}

// Here's my method that I'll be calling in the nested fragment
public String getSpinnerValue(){
    Spinner exerciseSpinner = (Spinner) getView().findViewById(R.id.movementName);
    String spinnerText = exerciseSpinner.getSelectedItem().toString();

    return spinnerText;
}

setSchems.java(嵌套/子片段)

@Override
public void onPause(){
    super.onPause();


    String parentSpinnerValue = ((movementSetsRepsFrag) getParentFragment()).getSpinnerValue();


    EditText setsEditText = (EditText) getView().findViewById(R.id.sets);
    EditText repsEditText = (EditText) getView().findViewById(R.id.reps);
    EditText weightEditText = (EditText) getView().findViewById(R.id.weight);


    String value = setsEditText.getText().toString() + " x " + repsEditText.getText().toString() + " @ " +
            weightEditText.getText().toString();


    ControllerData.getInstance().setEditTextValue1(value, parentSpinnerValue);

}

ControllerData.java(这是我用于存储我的值并在我将值放入的数组上执行逻辑的单例对象)

String[] exArray1 = new String[15];
String[] exArray2 = new String[15];

// EXERCISE SPINNER SETTERS
// here I'm setting the spinner value to the first index of the arrays
public void setExSpinnerValue1(String text){
    if(exArray1[0] == null){
        exArray1[0] = text;
    }else if(exArray2[0] == null){
        exArray2[0] = text;
    }
}

int a1 = 1;
int b1 = 1;

// SCHEME SETTERS
// my goal here is to take in a value from setSchemes and add it to whatever array has the spinner value at exArray[0]
public void setEditTextValue1(String setSchemeValue, String parentSpinnerValue){

    if(exArray1[0].equals(parentSpinnerValue)){
        if(exArray1[a1] == null){
            exArray1[a1] = setSchemeValue;
            a1++;
        }
    }
    if(exArray2[0].equals(parentSpinnerValue)){
        if(exArray2[b1] == null){
            exArray2[b1] = setSchemeValue;
            b1++;
        }
    }
}

Results.java(这里是我将数组显示为TextViews的地方)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);

    //ControllerData getController = new ControllerData();

    LinearLayout resultsLayout = (LinearLayout) findViewById(R.id.resultsLayout);


    // array1﹕ Bench Press null
    // array2﹕ Squat 2 x 3 @ 5

    if(ControllerData.getInstance().exArray1 != null){
        for(int i = 0; i < 15; i++){
            if(ControllerData.getInstance().exArray1[i] != null){
                String exValue1 = ControllerData.getInstance().exArray1[i];
                TextView textView1 = new TextView(this);
                textView1.setText(exValue1);
                resultsLayout.addView(textView1);
            }
        }
    }if(ControllerData.getInstance().exArray2 != null){
        for(int i = 0; i < 15; i++){
            if(ControllerData.getInstance().exArray2[i] != null){
                String exValue2 = ControllerData.getInstance().exArray2[i];
                TextView textView2 = new TextView(this);
                textView2.setText(exValue2);
                resultsLayout.addView(textView2);
            }
        }

    }

    Log.d("array1", ControllerData.getInstance().exArray1[0] + " " + ControllerData.getInstance().exArray1[1]);
    Log.d("array2", ControllerData.getInstance().exArray2[0] + " " + ControllerData.getInstance().exArray2[1]);

}

1 个答案:

答案 0 :(得分:0)

编辑:在片段中创建片段 以下是使用getChildFragmentManager()在另一个片段(子)中创建片段(孙子)的示例。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    FragmentManager fragmentManager = getChildFragmentManager();
    GrandchildFragment grandchildFragment = new GrandchildFragment();
    fragmentManager.beginTransaction()
        .add(R.id.grandchildFragment, grandchildFragment, "GrandchildFrag")
        .commit();
    return inflater.inflate(R.layout.fragment_child, container, false);
}

结束修改

我认为getParentFragment()正是您所寻找的。请参阅文档herehere。如果您在父片段中有一个公共例程调用public String getSpinnerValue(),那么您可以使用以下内容:

String parentFragmentValue = ((ParentClass) getParentFragment()).getSpinnerValue();

上述也可以通过更清洁的界面来实现。

您还可以在片段创建时将值直接传递给子片段,如下所示:

public ChildFragment() {
    // Required empty constructor
}

static ChildFragment newInstance(String valueInParent) {
    Bundle args;
    ChildFragment childFragment;

    childFragment= new ChildFragment();
    args = new Bundle();
    args.putString("valueInParent", valueInParent);
    childFragment.setArguments(args);
    return childFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    String valueInParent;

    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        valueInParent= getArguments().getString("valueInParent");
    }

}

父母也可以调用子例程,这与上面第一个解决方案相反。

我希望这会有所帮助。