从活动中获取片段数据的最佳方法

时间:2019-09-21 11:58:20

标签: android android-fragments

我的应用程序有一个主要活动和13个片段。有一个FragmentAdapter可以在单击Next或Previous按钮时更改片段。所有13个片段都有用户交互的不同组件。在最后一个片段中,有一个按钮,通过按下该按钮,所有输入的数据都应传递到父活动(MainActvity)。传递数据后,在活动中做一些工作。所以我想知道达到此要求的最佳方法。 这是设置片段适配器的主要活动。

public class HomeActivity extends AppCompatActivity {
private StepperLayout mStepperLayout;

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

    mStepperLayout = findViewById(R.id.stepperLayout);
    mStepperLayout.setAdapter(new MyStepperAdapter(getSupportFragmentManager(), this));
}
}

这是FragmentAdapter。

public class MyStepperAdapter extends AbstractFragmentStepAdapter {


public MyStepperAdapter(FragmentManager fm, Context context) {
    super(fm, context);
}

@Override
public Step createStep(int position) {
        switch (position) {
            case 0:
                final VehicleInformation step1 = new VehicleInformation();
                return step1;
            case 1:
                final DriverHelperDetails step2 = new DriverHelperDetails();
                return step2;
            case 2:
                final LegalDocument step3 = new LegalDocument();
                return step3;
            case 3:
                final LightCondition step4 = new LightCondition();
                return step4;
            case 4:
                final TyreCondition step5 = new TyreCondition();
                return step5;
            case 5:
                final VehicleCabinCondition step6 = new VehicleCabinCondition();
                return step6;
            case 6:
                final OtherConditions step7 = new OtherConditions();
                return step7;
            case 7:
                final TankerCondition step8 = new TankerCondition();
                return step8;
            case 8:
                final CompressorCondition step9 = new CompressorCondition();
                return step9;
            case 9:
                final OtherEquipCondition step10 = new OtherEquipCondition();
                return step10;
            case 10:
                final EmergencyTEquip step11 = new EmergencyTEquip();
                return step11;
            case 11:
                final PersonalProtectEquip step12 = new PersonalProtectEquip();
                return step12;
            case 12:
                final Complete step13 = new Complete();
                return step13;
        }

    return null;
}

这是一个片段示例。

public class VehicleInformation extends Fragment implements BlockingStep {
Button btn_next;
TextInputEditText vname;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.vehicleinformation, container, false);

    vname = rootView.findViewById(R.id.v_no);

    return rootView;
}


@Nullable
@Override
public VerificationError verifyStep() {
    return null;
}

@Override
public void onSelected() {

}

@Override
public void onError(@NonNull VerificationError error) {

}

@Override
@UiThread
public void onNextClicked(StepperLayout.OnNextClickedCallback callback) {
    callback.goToNextStep();
}

@Override
@UiThread
public void onCompleteClicked(final StepperLayout.OnCompleteClickedCallback callback) {
}

@Override
@UiThread
public void onBackClicked(StepperLayout.OnBackClickedCallback callback) {
    callback.goToPrevStep();
}

}

1 个答案:

答案 0 :(得分:1)

您可以使用界面传递数据。

Fragment中创建一个interface,如下所示。

public class YourFinalFragment extends Fragment {

  public interface onSomeEventListener {
    public void someEvent(String s);
  }

  onSomeEventListener someEventListener;

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
        try {
          someEventListener = (onSomeEventListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement onSomeEventListener");
        }
  }
.............
button.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    someEventListener.someEvent("Test text to Fragment1");
  }
});

在您的activity中,您必须像下面这样实现interface

public class MainActivity extends Activity implements onSomeEventListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       .........
    }

  @Override
  public void someEvent(String s) {
      // here you can get your desired value sent from fragment
  }
}