Fragment&&Fragment之間傳值的2種方式

github地址

https://github.com/zhouxu88/Fragment_Fragment_Data.git

一、使用接口,通過Activity宿主作為中間橋梁,進(jìn)行數(shù)據(jù)交互

1)定義數(shù)據(jù)接口
2)在Activity中實(shí)現(xiàn)該接口,并實(shí)現(xiàn)接口中定義的方法
3)在Fragment A中聲明接口對象,并調(diào)用接口中的方法
4)Activity中的接口回調(diào)中,向Fragment B傳遞數(shù)據(jù)

關(guān)鍵代碼
接口:

public interface IAddListener { 
   void update(int count); //更新數(shù)據(jù)
}

AFragment:

public class AFragment extends Fragment {  
      private int count; //計數(shù)器,表示向Fragment B傳遞的數(shù)據(jù)   
      private IAddListener listener;   
       @Override 
       public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {      
            View view = inflater.inflate(R.layout.fragment_a, container, false);                
            initView(view);   
             return view;   
   }   

     //初始化View  
      private void initView(View view) {        
      view.findViewById(R.id.btn_add).setOnClickListener(new  View.OnClickListener() {        
      @Override         
      public void onClick(View v) {            
      //數(shù)字加1,向Fragment B傳遞數(shù)據(jù)          
      count++;            
      listener.update(count); //傳遞數(shù)據(jù)                
      this.listener = listener; 
   }
}

Activity:

public class MainActivity extends AppCompatActivity implements IAddListener{   
     private FragmentManager fragmentManager;   
     @Override  
     protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.activity_main);       
        initFragment();    
  }   

 //初始化Fragment  
  private void initFragment() {      
      fragmentManager = getSupportFragmentManager();        
      AFragment aFragment = new AFragment();      
      FragmentTransaction transaction =     
      fragmentManager.beginTransaction();        
      transaction.add(R.id.fragment_a,aFragment,"AFragment");        
      transaction.add(R.id.fragment_b,new BFragment(),"BFragment");        
      transaction.commit();        
      aFragment.setIAddListener(this); //傳遞接口對象    
}   

   //回調(diào)的接口   
   @Override   
   public void update(int count) {       
     BFragment bFragment = (BFragment) fragmentManager.findFragmentByTag("BFragment");        
     bFragment.update(count);  
  }
}

BFragment:

public class BFragment extends Fragment {  
    private TextView resultTv;   


     @Override  
     public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {      
        View view = inflater.inflate(R.layout.fragment_b, container, false);        
        initView(view);    
        return view;   
 }    
 
 
  //初始化View  
  private void initView(View view) {     
       resultTv = (TextView) view.findViewById(R.id.result_tv);   
 }  

  //供Activity調(diào)用的方法  
  public void update(int count) {        
      resultTv.setText(String.valueOf(count)); 
   }
}

二、EventBus傳值

關(guān)鍵代碼
AFragment:

EventBus.getDefault().post(Integer.valueOf(count));

BFragment:

@Overridepublic void onAttach(Context context) {    
    super.onAttach(context);   
     //注冊    
    EventBus.getDefault().register(this);}

@Overridepublic void onDestroyView() { 
   super.onDestroyView();    //解綁    
EventBus.getDefault().unregister(this);
}
//事件接收
@Subscribe
public void onEvent(Integer count){    
resultTv.setText(String.valueOf(count));   
 Toast.makeText(getContext(), "傳遞數(shù)據(jù)成功", Toast.LENGTH_SHORT).show();
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容