1、Fragment通訊簡單介紹:Fragments之間是不可以直接通訊的,他們之間的通訊是經過Activity這個中間件來通訊的, 爲了讓Fragment跟它的Activity通訊,咱們能夠在Fragment類中定義一個接口,並在它所屬的Activity中實現該接口。Fragment在它的onAttach()方法執行期間捕獲該接口的實現,而後就能夠調用接口方法,以便跟Activity通訊。java
2、需求:利用ListFragment實現一個浮動的二級菜單,點擊左邊菜單在右邊顯示與之對應的二級菜單列表。效果以下圖所示:android
廢話很少說了,直接給你們上代碼:api
1、ListFragmentLeft(左邊的ListFragment菜單) app
package com.yw.myapiupdate.fragment; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ListFragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.yw.myapiupdate.R; @SuppressLint("NewApi") public class ListFragmentLeft extends ListFragment{ private LeftFragmentCallback callback; /* String[] lists = new String[]{ "賈克斯", "無雙劍姬", "蕾歐娜", "安妮", "潘森", "蓋倫" };*/ @Override public void onAttach(Activity activity) { super.onAttach(activity); try{ callback = (LeftFragmentCallback)activity; }catch(Exception e){ } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.listfragmentleft_layout, container, false); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,TestData.getInstance().getData(-1))); } @Override public void onListItemClick(ListView l, View v, int position, long id) { callback.leftCallback(position); /*Toast.makeText(getActivity(), "You have selected:\t " + lists[position], Toast.LENGTH_SHORT).show(); */ } /** * 回調接口 * @author yw-tony * */ public interface LeftFragmentCallback{ public void leftCallback(int position); } }
與之對應的佈局文件:ide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#33ff00" android:orientation="vertical" > <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:drawSelectorOnTop="false" /> </LinearLayout>
2、點擊左邊菜單時彈出的右邊菜單類(ListFragmentRight)工具
package com.yw.myapiupdate.fragment; import com.yw.myapiupdate.R; import android.annotation.SuppressLint; import android.app.ListFragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; @SuppressLint("NewApi") public class ListFragmentRight extends ListFragment{ /* String[] listRight = new String[]{ "天啓者", "趙信", "嘉文四世", "稻草人", "瑞文", "船長", "盲僧" };*/ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.listfragment_right, container, false); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,TestData.getInstance().getData(1))); } @Override public void onListItemClick(ListView l, View v, int position, long id) { ((MyFragmentActivity)getActivity()).windowDiss(); /*Toast.makeText(getActivity(), "You have selected:\t " + listRight[position], Toast.LENGTH_SHORT).show(); */ } }
與之對應的佈局文件:佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff66ff" android:orientation="vertical" > <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:drawSelectorOnTop="false" /> </LinearLayout>
3、以上二者之間的通訊橋樑Activity(MyFragmentActivity)this
package com.yw.myapiupdate.fragment; import android.annotation.SuppressLint; import android.app.Activity; import android.app.FragmentManager; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.PopupWindow; import com.yw.myapiupdate.R; import com.yw.myapiupdate.fragment.ListFragmentLeft.LeftFragmentCallback; @SuppressLint("NewApi") public class MyFragmentActivity extends Activity implements LeftFragmentCallback,OnClickListener{ /* String[] lists = new String[]{ "天啓者", "趙信", "嘉文四世", "稻草人", "瑞文", "船長", "盲僧", "那身男爵" };*/ private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TestData.getInstance().setData(); setContentView(R.layout.listfragment_layout); btn = (Button)findViewById(R.id.listfragment_btn); btn.setOnClickListener(this); /*Display display = getWindowManager().getDefaultDisplay(); if(display.getWidth() > display.getHeight()){ FragmentRed red = new FragmentRed(); getFragmentManager().beginTransaction().replace(R.id.fragment_linear_layout, red).commit(); }else{ FragmentYellow yellow = new FragmentYellow(); getFragmentManager().beginTransaction().replace(R.id.fragment_linear_layout, yellow).commit(); }*/ initMenu(); } private LayoutInflater inflater; private View view; private PopupWindow window; private void initMenu(){ inflater = LayoutInflater.from(this); view = inflater.inflate(R.layout.popupwindow_layout, null); window = new PopupWindow(view,WindowManager.LayoutParams.FILL_PARENT,WindowManager.LayoutParams.FILL_PARENT); window.setWidth(WindowManager.LayoutParams.FILL_PARENT); window.setHeight(WindowManager.LayoutParams.FILL_PARENT); window.setFocusable(true); window.setOutsideTouchable(true); window.setBackgroundDrawable(new BitmapDrawable()); } /** * 顯示窗體 * @param anchor */ public void show(View anchor){ if(window!=null && !window.isShowing()){ window.showAsDropDown(anchor); } } /** * 關閉窗體 */ public void windowDiss(){ if(window!=null && window.isShowing()){ window.dismiss(); } } @Override public void leftCallback(int position) { try{ /*ListFragmentRight listRight = (ListFragmentRight) getFragmentManager.findFragmentById( R.id.listfragment_right);*/ FragmentManager manager = getFragmentManager(); ListFragmentRight listRight = (ListFragmentRight) manager.findFragmentById(R.id.listfragment_right); if(listRight != null){ listRight.setListAdapter(new ArrayAdapter<String>(MyFragmentActivity.this, android.R.layout.simple_list_item_1,TestData.getInstance().getData((position+1)))); }else{ } }catch(Exception e){ } } @Override public void onClick(View v) { switch(v.getId()){ case R.id.listfragment_btn: if(window!=null && !window.isShowing()){ show(btn); } break; } } }
與之對應的佈局:spa
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_linear_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:baselineAligned="false"> </LinearLayout>
4、提供靜態數據源的工具類code
package com.yw.myapiupdate.fragment; import java.util.ArrayList; import java.util.List; public class TestData { private List<String> listParent = new ArrayList<String>(); private List<String> listA = new ArrayList<String>(); private List<String> listB = new ArrayList<String>(); private List<String> listC = new ArrayList<String>(); private List<String> listD = new ArrayList<String>(); private TestData(){} private static TestData instance; public static TestData getInstance(){ if(instance == null){ synchronized (TestData.class) { if(instance == null){ instance = new TestData(); } } } return instance; } /** * 獲取數據 * @param target * @return */ public List<String> getData(int target){ // List<String> lists = new ArrayList<String>(); switch(target){ case 1: return listA; // break; case 2: return listB; // break; case 3: return listC; // break; case 4: return listD; // break; } return listParent; } public void setData(){ listParent.add("A"); listParent.add("B"); listParent.add("C"); listParent.add("D"); listA.add("A1"); listA.add("A2"); listA.add("A3"); listB.add("B1"); listB.add("B2"); listB.add("B3"); listB.add("B4"); listC.add("C1"); listC.add("C2"); listD.add("D1"); listD.add("D2"); listD.add("D3"); listD.add("D4"); listD.add("D5"); } }
程序運行後的效果圖:
好了,ListFragment以及他們之間的通訊到此就結束了,你?弄明白了嗎?