Android之Fragment静态实现实例
通过最近空闲时候对Fragment的学习,尝试着写了一个小Demo,将在开发的时候能常用到的Fragment知识放在一起,写过了这个Demo对Android Fragment的了解更加深入了,以后做起有关的项目也会顺手很多,下面先贴上几张实现之后的图片:
实现的功能很简单,也是最基本的,上下分别是两个Fragment,上面的Fragment中是一个listview,当点击item时,下面的Fragment显示对应的文字详细信息:
具体的实现步骤如下:
①创建工程FragmentExam,目录视图如下(把之前的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>
<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);
- }
- }
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 {
- @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);
- }
- }
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静态实现实例相关推荐
- Android笔记 fragment入门 静态加载fragment
fragment最初目的:为了方便平板开发界面的美化 fragment引入原因 一般市面上的软件Activity太过庞杂 每一个Activity都要在清单文件配置 清单文件也比较臃肿查找修改不便 因此 ...
- Android 进阶 Fragment 介绍和使用 (一)
Fragment概述 Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一 ...
- Android之Fragment 真正的完全解析(上)
转载出处:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragme ...
- Android之Fragment(一)
Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板 ...
- 【26】Android - 初识Fragment
目录 目录 前言 相关库包 简单使用 静态加载 动态添加 Fragment 与Activity 通讯 组件获取 消息传递 生命周期 基本状态 回调 测试 进阶使用 如何在 Fragment 中增加 M ...
- Android中Fragment知识点终极整理 避免多个Fragment加载重叠踩坑
Fragment Fragment定义 Fragment为什么被称为第五大组件 Fragment加载到Activity的两种方式 FragmentPagerAdapter和FragmentStateP ...
- android碎片按钮,Android 碎片(Fragment)
Android 碎片(Fragment) 碎片是活动的一部分,使得活动更加的模块化设计.我们可以认为碎片是一种子活动. 下面是关于碎片的重要知识点 - 碎片拥有自己的布局,自己的行为及自己的生命周期回 ...
- android 之Fragment的详解
友情链接:点击打开链接 1.将Activity传值到Fragment 具体步骤: <1>声明碎片事务器对象 private FragmentManager fragmentManager; ...
- Android之 Fragment
什么是Fragment: Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局, ...
最新文章
- RSEM-Ebseq-差异表达分析-无参
- [css] :placeholder-shown和:focus-within这两个伪类你有使用过吗?说说看
- mysql sqlite 分页查询_sqlite分页查询
- 带你阅读linux内核源码:下载源码、编译内核并运行一个最小系统
- 正则表达式匹配以xx开头以xx结尾
- 大秦帝国和芈月传对比
- html外链自动加nofollow,WordPress自动为站外链接添加nofollow标签
- 编译原理学习之:上下文无关文法(Context-free Grammar)和下推自动机(Push-down automata)
- 记录一次dns劫持及其解决办法
- 二次函数图像如何用计算机绘制,如何快速画二次函数图像
- manifestdependency,manifest文件
- BLE广播包4种类型
- 添加布林带择时策略有多便捷!股票量化分析工具QTYX-V2.4.7
- 前端开发:Vue报错Computed property “show“ was assigned to but it has no setter的解决方法
- 随机事件的关系及运算
- i春秋网鼎杯网络安全大赛advanced题目writeup
- MYSQL数据库基本操作——DML
- coder,你会设计交易系统吗(实干篇)?
- 2021-09-25 在uniapp中 打包成app调用地图 提示 uni.authorize is not a function 怎么办 (图文详解)
- python entry 文本框只能输入数字 限定数字