片段未在使用接口的其他片段的onclick中替换

时间:2019-03-04 23:45:37

标签: android android-fragments interface

  

目标:尝试使用接口将数据从FragmentA传递到Activity到FragmentB。

     

问题:单击底部时,未在Fragment a中替换(创建)FragmentB。 (也尝试.add)解决此问题的唯一方法是在创建Activity时启动两个片段。

     

路径:活动-> fragmentA-> onclick替换片段,并通过接口传递数据->活动->将数据从活动传递至片段B。

     

尝试过:    .replace和.add,fragmentb在活动性和fragmentA中

主要活动:

public class MainActivity extends AppCompatActivity implements Communicator {
android.support.v4.app.FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 AFragment savedFragment = (AFragment) 
    getSupportFragmentManager().findFragmentById(R.id.fl_conainter);
    if (savedFragment == null) {
        addFragment();
    }
}


private void addFragment() {

    ft = getSupportFragmentManager().beginTransaction();
    AFragment aFragment = new AFragment();
    ft.add(R.id.fl_conainter,aFragment,"FragA");
    ft.commit();
}

//****Issue is here ****
@Override
public void respond(String value) {
    addSecondFragment();

    FragmentManager manager=getSupportFragmentManager();
   /* BFragment fragment = (BFragment) 
   manager.findFragmentById(R.id.fragmentB);*/
    BFragment fragment = (BFragment) manager.findFragmentByTag("Fragb");
    fragment.changeData(value);

}

private void addSecondFragment() {

    BFragment bFragment = new BFragment();
    ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fl_conainter,bFragment,"Fragb");
    ft.commit();
}
}

片段A:

public class AFragment extends Fragment implements View.OnClickListener {
Communicator communicator;
Button button;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_a, container, false);


}

@Override
public void onActivityCreated (Bundle saveInstanceState) {
    super.onActivityCreated(saveInstanceState);
    button = getActivity().findViewById(R.id.update_btn);
    button.setOnClickListener(this);
    communicator = (Communicator) getActivity();

}

@Override
public void onClick(View view) {

   // AddNextfrag();
    communicator.respond("passing data");
}



    private void AddNextfrag (){
        android.support.v4.app.FragmentTransaction ft;
        ft = getActivity().getSupportFragmentManager().beginTransaction();
        BFragment bFragment = new BFragment();
        ft.add(R.id.fl_conainter,bFragment, "Fragb");
        ft.commit();

    }

}

片段B:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class BFragment extends Fragment {

TextView text;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_b, container, false);
}

@Override
public void onActivityCreated (Bundle saveInstanceState) {
    super.onActivityCreated(saveInstanceState);
text =  getActivity().findViewById(R.id.result_tv);
}


public void changeData(String value){
text.setText(value);
}
}

接口:

public interface Communicator {

 void respond(String value);
}

1 个答案:

答案 0 :(得分:0)

public class AFragment extends Fragment implements
   View.OnClickListener {
       Communicator communicator;
       Button button;
       Activity activity;

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

       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                Bundle savedInstanceState) {
           // Inflate the layout for this fragment
           return inflater.inflate(R.layout.fragment_a, container, false);


       }

       @Override
       public void onActivityCreated (Bundle saveInstanceState) {
           super.onActivityCreated(saveInstanceState);
           button = getActivity().findViewById(R.id.update_btn);
           button.setOnClickListener(this);
           communicator = (Communicator) activity ;

       }

       @Override
       public void onClick(View view) {

          // AddNextfrag();
           communicator.respond("passing data");
       }



           private void AddNextfrag (){
               android.support.v4.app.FragmentTransaction ft;
               ft = getActivity().getSupportFragmentManager().beginTransaction();
               BFragment bFragment = new BFragment();
               ft.add(R.id.fl_conainter,bFragment, "Fragb");
               ft.commit();

           }

       }