Fragment的简单使用
最近一直有点忙,刚刚看到一个朋友的留言提到Fragment中加载ListView的问题,这里写了一个非常简单的测试,至于对Fragment的增、删、改实现动态布局构建灵活的UI,以后有时间在讨论:
MainActivity:
package com.home.testfragment; import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.fragment__first_list);
List<String> list = new ArrayList<String>();
list.add("张三");
list.add("李四");
list.add("王五");
list.add("赵六");
ArrayAdapter<String> adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
}
}
FirstFragment:
package com.home.testfragment; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <fragment
android:name="com.home.testfragment.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="@+id/main_fragment_first" /> </LinearLayout>
fragment_first:
<?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:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试fragment的简单使用"
android:textSize="20sp" /> <ListView
android:id="@+id/fragment__first_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>
附上界面效果:
Fragment的简单使用的更多相关文章
- Android开发——Fragment的简单使用总结
前言: 之前搞项目的时候,就使用了这个Fragment,中间遇到了许多坑,把坑都解决了,现在写一篇较为简单的Fragment使用总结 Fragment的简单介绍: 简单来说,Fragment其实可以理 ...
- 【Android】Fragment的简单笔记
被虐了,做某公司笔试时,发现自己连个Fragment的生命周期都写不详细.平时敲代码,有开发工具的便利,有网上各大神的文章,就算忘了也很容易的可以查到,但当要自己不借助外界,却发现自己似乎对该知识点并 ...
- Android-fragment简介-fragment的简单使用
1.fragment简介 在Android3.0版本之前Google还没有推出fragment,在Android3.0版本之后Google推出了fragment,由于Android3.0版本是过渡期版 ...
- fragment (1)简单示例:定义,界面配置,fragment之间的跳转
fragment作用 同一程序中切换界面 比activity轻快,灵活. fragment代码示例 ide : android studio 1.2 sdk : 22 package com.exa ...
- 两个fragment之间简单的跳转
1.在第一个fragment中开启事务,设置标记 Toast.makeText(getActivity(), "切换到下一个fragment中", Toast.LENGTH_SHO ...
- Android 关于Fragment一些简单注意事项
1.Fragment的生命周期一定要了解清楚: 其实没有什么好说的. 注意关键字所执行的方法: 注意下面基本fragmentTransaction.replace();运行 当FragmentA(初始 ...
- Android中Fragment的简单介绍
Android是在Android 3.0 (API level 11)引入了Fragment的,中文翻译是片段或者成为碎片(个人理解),可以把Fragment当成Activity中的模块,这个模块有自 ...
- Android中通过Fragment进行简单的页面切换
首先是activity中的布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constr ...
- Android Fragment(三)ListFragment简单介绍以及Fragment之间通信
一.Fragment通信简单介绍:Fragments之间是不能够直接通信的,他们之间的通信是通过Activity这个中间件来通信的, 为了让Fragment跟它的Activity通信,我们可以在Fra ...
随机推荐
- ACPI电源管理中的S0 S1 S2 S3 S4 S5
电源选项中S0,S1,S2,S3,S4,S5的含义以 ACPI 的规格来说吧!ACPI(Advanced Configuration and Power Interface),即高级配置与电源接口.这 ...
- mybati之#与$的区别
$是用于sql的拼接: //因为user_name是String类型,所以在sql中加上单引号,需要手动的判断数据类型,value是如果没有指定参数的话,value就是默认参数名称,获取穿的参数就是: ...
- 解决手机浏览器和微信中select中border:none;无用和去掉小三角
设置select的边框为none:在PC端是没有问题的,但是放到手机浏览器和微信里上还是会出现边框 解决办法: appearance:none; -moz-appearance:none; /* Fi ...
- 【nodejs学习】2.网络相关
1.官方文档的一个小例子 //http是内置模块 var http = require('http'); http.createServer(function(request, response){ ...
- 打开较大存储量的.sql文件时,出现SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问
1. “消息 15281,级别 16,状态 1,过程 xp_cmdshell,第 1 行SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访 ...
- window对象BOM
BOM的和新对象是window,他表示流浪器的一个实例,作为一个Global对象,有权访问parseInt()等方法 在全局作用域声明的变量,函数都有钱访问 ; function sayName () ...
- n行m列的网格中含有的矩形数
给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形 公式:[ n(n+1)*m(m+1)]/4 直接想问题比较复杂,可以先考虑矩形的长,再考虑矩形的高,由对称性可知最后的结果中m和n对称 ...
- c++中static的使用
static可以用来修饰变量,包括函数的局部变量,类的成员变量.可以用来修饰函数,包括类的成员函数,普通函数. 今天就只说说static修饰类之外的函数的情况.假设你写了一个head.h,一个a.cp ...
- json_encode如何防止汉字转义成unicode
众所周知,json_encode通常会把json中的汉字转义成unicode,但是这并不一定是我们想要的.有时候,我们需要获得汉字形式的json字符串,比如需要获得gbk编码的json字符串(只要把汉 ...
- uC/OS-II内核架构解析(1)---嵌入式RTOS(转)
uC/OS-II内核架构解析(1)---嵌入式RTOS 1. 嵌入式系统基本模型 2. RTOS设计原则 采用各种算法和策略,始终保持系统行为的可预测性.即在任何情况下,在系统运行的任何时刻,OS的资 ...