基於Android Fragment功能的範例

來源:互聯網
上載者:User

標籤:

    通過近期空暇時候對Fragment的學習,嘗試著寫了一個小Demo,將在開發的時候能經常使用到的Fragment知識放在一起,寫過了這個Demo對Android Fragment的瞭解更加深入了,以後做起有關的項目也會順手非常多,以下先貼上幾張實現之後的圖片:

實現的功能非常easy,也是最主要的,上下各自是兩個Fragment,上面的Fragment中是一個listview,當點擊item時,以下的Fragment顯示相應的文字具體資訊:

 

               

 

詳細的實現過程例如以下:

①建立projectFragmentExam,資料夾檢視例如以下(把之前的FragmentPreference的demo也加到了一起):

 

②main.xml檔案布局,垂直方向上兩個Fragment,用<Fragment>標籤聲明

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"     android:orientation="vertical"    android:background="#7ecef4"><fragment     android:name="com.example.fragementexam.FragementList"    android:id="@+id/frag_list"    android:layout_width="fill_parent"    android:layout_height="0dp"    android:layout_weight="2"/><fragment     android:name="com.example.fragementexam.FragementDetails"    android:id="@+id/frag_detail"    android:layout_width="fill_parent"    android:layout_height="0dp"    android:layout_weight="1"/></LinearLayout>


③FragmentList.java的代碼,它繼承了ListFragment,注意在onCreateView方法中使用inflater的inflate方法將布局頁面引進:

package com.example.fragementexam;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.ListFragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ListView;import android.widget.SimpleAdapter;public class FragementList extends ListFragment {private String[] values = new String[] { "侏儒", "人類", "暗夜精靈", "矮人", "德萊尼","狼人" };private int[] images = new int[] { R.drawable.gnome,R.drawable.human, R.drawable.nightelf,R.drawable.dwarf, R.drawable.draenei,R.drawable.werewolf };@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.frag_list, container, false);}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();for (int i = 0; i < values.length; i++) {Map<String, Object> listItem = new HashMap<String, Object>();listItem.put("values", values[i]);listItem.put("images", images[i]);listItems.add(listItem);}SimpleAdapter adapter = new SimpleAdapter(getActivity(), listItems,R.layout.list_item, new String[] { "values", "images" },new int[] { R.id.txt_title, R.id.img });setListAdapter(adapter);}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {// String item = (String) getListAdapter().getItem(position);FragementDetails frag = (FragementDetails) getFragmentManager().findFragmentById(R.id.frag_detail);if (frag != null && frag.isInLayout()) {switch (position) {case 0:frag.setText(getString(R.string.Gnome));break;case 1:frag.setText(getString(R.string.Human));break;case 2:frag.setText(getString(R.string.NightElf));break;case 3:frag.setText(getString(R.string.Dwarf));break;case 4:frag.setText(getString(R.string.Draenei));break;case 5:frag.setText(getString(R.string.Werewolf));break;}}Log.i("PDA", "position = " + position);}}


④FragementDetails.java的代碼,這個比較簡單,裡面有一個設定TextView內容的方法,其布局頁面也不過一個TextView

package com.example.fragementexam;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class FragementDetails extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubreturn inflater.inflate(R.layout.frag_detail, container,false);}public void setText(String item){TextView txt = (TextView) getView().findViewById(R.id.txt_detail);txt.setText(item);}}


 

其它的部分就是一些數組,String的定義了,這個demo儘管簡單,卻將Android Fragment方面經常使用到的做了一個綜述,假設自己寫明確了這個的話,今後遇到類似的項目應該要好應付的多,好了,收工!

基於Android 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.