Android之碎片Fragment
Fragment是个特别的存在,有点像报纸上的专栏,看起来只占据页面的一小块,但是这一小块有自己的生命周期,可以自行其是,仿佛独立王国,并且这一小块的特性无论在哪个页面,给一个位置就行,添加以后不影响宿主页面的其他区域,去除后也不影响宿主页面的其他区域。每个fragment都有自己的布局文件,依据其使用方式可分为静态注册和动态注册两种,静态注册是在布局文件中直接放置fragment节点,类似于一个普通控件,可被多个布局文件同时引用。静态注册一般用于某个通用的页面部件(如logo条,广告条等),每个活动页面均可直接引用该部件。
1.静态注册
下面是fragment布局文件的代码:
<?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="wrap_content"
android:orientation="horizontal"
android:background="#bbffbb">
<TextView
android:id="@+id/tv_adv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#000"
android:textSize="20sp"
android:gravity="center"
android:text="广告"/>
<ImageView
android:id="@+id/iv_adv"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="5"
android:src="@drawable/adv"
android:scaleType="fitCenter"/>
</LinearLayout>
下面是与上述文件对应的fragment代码,除了继承自fragment外,其他地方很像活动页代码:
package com.example.animator.highleveltools; import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; import java.util.zip.Inflater; public class BlankFragment extends Fragment implements View.OnClickListener{ private View mView;
private Context mContext; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContext = getActivity();//获取活动页上下文
//根据布局文件fragment_static.xml生成视图对象
mView = inflater.inflate(R.layout.fragment_layout,container,false);
TextView tv_adv = (TextView) mView.findViewById(R.id.tv_adv);
ImageView iv_adv = (ImageView) mView.findViewById(R.id.iv_adv);
tv_adv.setOnClickListener(this);
iv_adv.setOnClickListener(this);
return mView;//返回该碎片的视图对象
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
} @Override
public void onClick(View view) {
if(view.getId()==R.id.tv_adv){
Toast.makeText(mContext,"您点击了广告文本",Toast.LENGTH_LONG).show();
}else if(view.getId()==R.id.iv_adv){
Toast.makeText(mContext,"您点击了广告图片",Toast.LENGTH_LONG).show();
}
}
}
若想在页面布局文件中引用fragment,则可直接加入一个fragment节点,注意节点要增加name属性指定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"
android:orientation="vertical"
android:padding="5dp"
> <fragment
android:id="@+id/fragment_static"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.animator.highleveltools.BlankFragment"
tools:layout="@layout/fragment_layout" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:text="这是每个页面的具体内容"
android:textColor="#000"
android:textSize="17sp"/>
</LinearLayout>
运行效果如下:

2.动态注册
相比静态注册,动态注册用的更多,动态注册知道在代码中才动态添加fragment,动态生成的碎片就是给翻页视图用的。使用碎片适配器即可实现:
package adapter; import android.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.style.DynamicDrawableSpan; import java.util.ArrayList; import bean.DynamicFragment; /**
* Created by animator on 2020/1/29.
*/
public class MobilePagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<GoodsInfo> mGoodsList = new ArrayList<>();//声明一个商品队列 //碎片页适配器的构造函数,传入碎片管理器与商品信息队列
private MobilePagerAdapter(android.support.v4.app.FragmentManager fm , ArrayList<GoodsInfo> goodsList){
super(fm);
mGoodsList = goodsList;
} //获取指定位置的fragment
@Override
public Fragment getItem(int position) {
return DynamicFragment.newInstance(position,mGoodsList.get(position).pic,mGoodsList.get(position).desc);
} //获取碎片fragment的个数
@Override
public int getCount() {
return mGoodsList.size();
} //获得指定碎片页的标题文本
public CharSequence getPageTitle(int position){
return mGoodsList.get(position).name;
}
}
下面是动态注册的碎片代码:
package bean; import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; import com.example.animator.highleveltools.R; /**
* Created by animator on 2020/1/29.
*/
public class DynamicFragment extends Fragment {
protected View mView;//声明一个视图对象
protected Context mContext;//声明一个上下文对象
private int mPosition;//位置序号
private int mImageId;//图片的资源编号
private String mDesc;//商品的文字描述 //获取该碎片的一个实例
public static DynamicFragment newInstance(int position,int image_id,String desc){
DynamicFragment fragment = new DynamicFragment();//创建该碎片的一个实例
Bundle bundle = new Bundle();//创建一个新包裹
bundle.putInt("position",position);
bundle.putInt("image_id",image_id);
bundle.putString("desc",desc);
fragment.setArguments(bundle);//把包裹塞给碎片
return fragment;
} //创建碎片视图
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
mContext = getActivity();
if(getArguments()!=null){//如果碎片携带有包裹
mPosition = getArguments().getInt("position");
mImageId = getArguments().getInt("image_id");
mDesc = getArguments().getString("desc");
}
//根据布局文件fragment_dynamic.xml生成视图对象
mView = inflater.inflate(R.layout.fragment_dynamic,container,false);
ImageView iv_pic = mView.findViewById(R.id.iv_pic);
TextView tv_desc = (TextView) mView.findViewById(R.id.tv_desc);
iv_pic.setImageResource(mImageId);
tv_desc.setText(mDesc);
return mView;//返回该碎片的视图对象
}
}
下面是activity代码:
ArrayList<GoodsInfo> goodsList = GoodsInfo.getDefaultList();
//构建一个手机商品的碎片翻页适配器
MobilePagerAdapter adapter = new MobilePagerAdapter(getSupportFragmentManager(),goodsList);
//从布局视图中获取名叫vp_content的翻页视图
ViewPager vp_content = findViewById(R.id.vp_content);
//给vp_content设置手机商品的碎片适配器
vp_content.setAdapter(adapter);
//设置vp_content默认显示第一个页面
vp_content.setCurrentItem(0);
Android之碎片Fragment的更多相关文章
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml
Android开发:碎片Fragment完全解析 为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...
- Android学习——碎片Fragment的使用
一.碎片的简单用法(实现在一个活动中添加两个碎片,并让这两个碎片平分活动空间) 1.新建一个FragmentTest项目: 新建一个左侧碎片布局left_fragment.xml,代码如下:(只放置一 ...
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml(转)
注明:这个转的,见谅未能标明原始出处 我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activ ...
- 【Android】碎片Fragment
1.碎片可以让界面在平板上更好地展示. 2.碎片是一种可以嵌入到活动中的UI片段,它能让程序更加合理和充分地利用一个大屏幕的空间.有自己的生命周期,能包含布局. 3.新建碎片类继承Fragment,可 ...
- Android交流会-碎片Fragment,闲聊单位与尺寸
女孩:又周末了哦~ 男孩:那么今日来开个交流会,我们也学一学人家高大尚的大会,自己开一个,广州站,Android开发攻城狮交流会~ 1.Fragment概要: Android从3.0开始引入了Frag ...
- android 开发 碎片Fragment布局例子(用按键切换碎片布局)
实现思路: 1.写一个父类布局,里面写一个按键和一个帧布局(用于给Fragment布局后续替代) 2.写3个子布局,并且在写3个class继承Fragment布局 3.在MainActivity的cl ...
- Android利用碎片fragment实现底部标题栏(Github模板开源)
在安卓开发当中,一个十分重要的布局则是底部标题栏了,拥有了底部标题栏,我们就拥有了整个软件UI开发的框架,一般而言,整个软件的布局首先就是从底部标题栏开始构建,然后再开始其他模块的编写,组成一个完善的 ...
- android UI:Fragment碎片
碎片(Fragment) 嵌入与活动中的UI片段,为了合理的分配布局而存在,这是我的简单理解.多用于兼顾手机与平板的UI,也适用于灵活高级的UI制作. Demo 简单的按键切换两片不同的Demo 新建 ...
- 安卓Android碎片fragment实现静态加载
静态加载好后的界面如下,两个碎片分别位于一个活动的左边和右边: 左边和右边分别为一个碎片,这两个碎片正好将一整个活动布满.一个活动当中可以拥有多个碎片,碎片的含义就是可以在同一个UI界面下,将这个界面 ...
随机推荐
- [urllib]urlretrieve在python3
python3下面要使用:urllib.request.urlretrieve()这种形式的调用 from urllib.request import urlretrieve urlretrieve( ...
- 2020-02-20Linux学习日记,第二天
在内容开始前请教一下博客园的大佬.编辑器中没有看到格式刷,要怎么不连续的选中内容给予想要的格式,有看到的麻烦私信解答一下,谢谢! ----------------------------------- ...
- Linux服务器上搭建codis集群之——安装前环境准备
codis是redis的分布式集群模式,由豌豆荚开源,本文简单记录一下它的集群搭建方法. 首先介绍一下我的实验环境.三台配置相同的虚拟机, [root@test ~]# ip a|grep -w &q ...
- Invoking Descriptors - Python 描述符的用法建议
描述符用法建议, 内置的 property 类创建的是'覆盖型'(date descriptor), 实现了 __set__ 和 __get__. 特性 property 的 __set__ 方法 默 ...
- Bound Method and Unbound Method - 绑定方法 与 非绑定方法
Bound Method and Unbound Method 通常有两种方法对类的方法(instance.method)/属性(class.attribute)进行引用, 一种称做 Bound Me ...
- 四步搞定Zabbix 日志文件监控
Zabbix 日志文件监控 一.给运行Zabbix agent的用户授予要监控日志的读取权限. 1. 執行下面的命令,追加app的可讀權限: setfacl -m u:app:r-- /var/log ...
- C++ 类的赋值运算符'='重载
什么类需要重载赋值运算符 先来看一个普通类的直接赋值. #include <iostream> using namespace std; class person{ int age; pu ...
- Angular常用命令
一. Angular常用命令 1. ng new 文件夹名 (新建项目,选择y使用路由) 2. ng serve --open (默认浏览器运行项目) 3. ng serve --port 6060 ...
- 根据map中的某一key进行排序(快速排序实现)
/** * @author Gaoxl * 根据key值排序,要求key值能够转为Long类型(快速排序) * @param maps * @param key * @return */ public ...
- 使用Nginx对.NetCore站点进行反向代理
前言 之前的博客我已经在Linux上部署好了.NetCore站点且通过Supervisor对站点进行了进程守护,同时也安装好了Nginx.Nginx的用处非常大,还是简单说下,它最大的功能就是方便我们 ...