
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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)