将布尔值从片段传递到托管活动

时间:2018-08-18 01:57:12

标签: java android android-fragments

我有一个实现ClickListener的Fragment。托管活动要么显示一个空白片段,要么在片段容器(FrameLayout)中显示所单击项目的值。

我正在处理轮换更改,并且我需要知道片段中是否已单击某个项目。如果已单击,则要显示所有方向更改的数据。如果未单击任何内容,我想显示空白片段,该片段的简单步骤为“请单击”。

我只需要知道如何将此布尔值传递回活动,我相信一个接口是一个选择,但需要一些编码帮助:

适配器,它指示在片段中单击项目时会发生什么:

    holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction transaction = mFragmanager.beginTransaction();
                if (position == 0){
                    IngredientsListFragment ingredientsListFragment = IngredientsListFragment.newInstance(mRecipeList, mRecipePosition, position, mIngredientsDataSet);
                    RecipeStepsFragmentTwo recipeStepsFragmentTwo = (RecipeStepsFragmentTwo) mFragmanager.findFragmentById(R.id.recipe_details_two);
                    transaction.remove(recipeStepsFragmentTwo);
                    transaction.add(R.id.recipe_details_three, ingredientsListFragment);
                    transaction.commit();

                } else {
                    RecipeStepsFragmentThree recipeStepsFragmentThree;
                    recipeStepsFragmentThree = RecipeStepsFragmentThree.newInstance(mRecipeList, mRecipePosition, (position -1));
                    //TODO: Why am I being prompted for V4? Am I handling correctly?

                    //TODO: Am I handling removal of previous frag correctly?
                  RecipeStepsFragmentTwo recipeStepsFragmentTwo = (RecipeStepsFragmentTwo) mFragmanager.findFragmentById(R.id.recipe_details_two);
                    transaction.remove(recipeStepsFragmentTwo);
                    transaction.add(R.id.recipe_details_three, recipeStepsFragmentThree);
                    transaction.commit();
                }


            }
        });

托管活动:

  if (savedInstanceState == null) {


        RecipeStepsFragmentTwo recipeStepsFragmentTwo = RecipeStepsFragmentTwo.newInstance(mRecipeList, mPosition);
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction()
                .add(R.id.recipe_details_two, recipeStepsFragmentTwo)
                .commit();



        //two-pane
        if (findViewById(R.id.two_pane_constraint_layout) != null & getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            mTwopane = true;
            BlankFragment blankFragment = new BlankFragment();
            fm.beginTransaction()
                    .add(R.id.recipe_details_three, blankFragment)
                    .commit();
        }

    }

    else {
        mRecipeList = savedInstanceState.getParcelableArrayList(RECIPE_LIST);
        mPosition = savedInstanceState.getInt(RECIPE_POSITION);
        FragmentManager z = getSupportFragmentManager();
        //check if fragment was clicked, if so no longer necessary to show blank
        RecipeStepsFragmentThree recipeStepsFragmentThree = (RecipeStepsFragmentThree) z.findFragmentById(R.id.recipe_details_three);
        if (recipeStepsFragmentThree != null){
            return;
        }
        BlankFragment blankFragment = (BlankFragment) z.findFragmentById(R.id.recipe_details_three);
        if (blankFragment != null & getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            z.beginTransaction().remove(blankFragment).commit();
        }
        if (blankFragment == null & getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            BlankFragment newLandscapeFrag = new BlankFragment();
            z.beginTransaction().add(R.id.recipe_details_three, newLandscapeFrag).commit();
        }


    }

2 个答案:

答案 0 :(得分:1)

您可以简单地通过getActivity.isFlag = true或flase完成 isFlag处于活动状态,您可以通过getActivity访问它。

答案 1 :(得分:1)

  1. 在Fragment中定义接口和抽象方法。如果您单击“适配器”,则将此委托传递给适配器,或者最好单击“片段”。

代码

public class MyFragment extends Fragment{
private Context mContext;

public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
    }

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mDelegate = (Delegate) mContext;
        ...
        ...
        ...
        onclick -> {
           mDelegate.passValue(value) 
         }

}

public interface Delegate{
 public void passValue(boolean value);

}

您的活动

public class MyActivity extends AppCompatActivity implements MyFragment. Delegate{

    @override    
    public void passValue(boolean value){
    } 

}