Android初級教程Fragment到Fragment的通訊初探

來源:互聯網
上載者:User

標籤:

這裡只是給出三個類RightFragment、LeftFragment、MainActivity中的簡易代碼,至於布局怎麼設定,不做贅述。

思路:從片段一擷取與之依託的活動執行個體,片段一可以調用活動裡面的功能;在活動中擷取片段二的活動執行個體,活動可以使用片段二的功能。片段一間接調用片段二的功能。

一、RightFragment:

package com.example.fragmenttest2;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class RightFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {                MainActivity activity = (MainActivity) getActivity();//擷取與之依託的活動執行個體LeftFragment callleft = activity.callleft();callleft.show();//使用活動的功能,這個功能正好是調用片段二的方法。View view = inflater.inflate(R.layout.right_fragment, container, false);return view;}public void show(){System.out.println("RightFragment");}}

二、MainActivity:

package com.example.fragmenttest2;import android.os.Bundle;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {private LeftFragment leftFragment;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button);Button button1 = (Button) findViewById(R.id.button1);button.setOnClickListener(this);button1.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button://按鈕一,實現了一次替換片段的功能AnotherRightFragment fragment = new AnotherRightFragment();FragmentManager fragmentManager = getFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.replace(R.id.right_layout, fragment);transaction.addToBackStack(null);transaction.commit();break;case R.id.button1:leftFragment = (LeftFragment) getFragmentManager()//按鈕二,可直接使用left片段即片段二的功能.findFragmentById(R.id.left_fragment);leftFragment.show();break;default:break;}}public void show(){System.out.println("MainActivity");}public LeftFragment callleft(){//抽取方法,調用片段二的功能leftFragment = (LeftFragment) getFragmentManager().findFragmentById(R.id.left_fragment);return leftFragment;}}

三、LeftFragment:

package com.example.fragmenttest2;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class LeftFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.left_fragment, container, false);return view;}public void show(){//片段二的功能,這裡只為了示範知識列印一行輸出System.out.println("LeftFragment");}}

啟動程式,發現直接列印的一行輸出:

LeftFragment

這樣就實現了片段與活動,以及片段與片段直接的通訊。



Android初級教程Fragment到Fragment的通訊初探

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.