首页 > 代码库 > 基于Android Fragment功能的样例
基于Android Fragment功能的样例
通过近期空暇时候对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 }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.frag_list, container, false); } @Override public 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); } @Override public 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 { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return 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); } }
基于Android Fragment功能的样例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。