5-AVI--Fragment简单封装
零、前言
[1].每次写Fragment要加载布局,为布局设置内容,挺麻烦的,搞个基类简单封装一下吧
[2].一般封装基类使用模板方法设计模式
,基类中做一些常用的不变东西,需要拐点弯的逻辑就弄个抽象方法延迟到子类
[3].textView设置文字,ImageView设置图片两个经常用的方法也提供一下
一、代码实现
1.使用:EVAFragment继承
public class EVAFragment extends BaseFragment { @Override protected void render(View rootView) { setTextView(R.id.f_tv_title, "封装Fragment"). setImageView(R.id.f_iv_back, R.mipmap.ic_launcher); } @Override protected int setLayoutId() { return R.layout.fragment_title; } }
2.Activity
getFragmentManager().beginTransaction().add(R.id.fl_title, new EVAFragment()).commit();
3.封装的基类
/** * 作者:张风捷特烈<br/> * 时间:2018/8/29 0029:13:46<br/> * 邮箱:1981462002@qq.com<br/> * 说明:Fragment封装类 */ public abstract class BaseFragment extends Fragment { private View mRootView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //加载布局 mRootView = inflater.inflate(setLayoutId(), container, false); render(mRootView); return mRootView; } /** * 设置布局里的控件 * */ protected abstract void render(View rootView); /** * 设置布局id * @return 布局id */ protected abstract int setLayoutId(); /** * 找出对应的控件 * * @param id 控件id * @param <T> 控件类型 * @return 控件 */ protected <T extends View> T findViewById(int id) { return (T) mRootView.findViewById(id); } /** * 为textView设置文字 * * @param viewId TextView控件id * @param str 控件id * @return BaseFragment */ protected BaseFragment setTextView(int viewId, String str) { TextView textView = findViewById(viewId); textView.setText(str); return this; } /** * 通过id设置ImageView图片 * * @param viewId 条目内部控件的id * @param o 图片对象 * @return BaseFragment */ public BaseFragment setImageView(int viewId, Object o) { ImageView view = findViewById(viewId); if (o instanceof Integer) { view.setImageResource((Integer) o); } else if (o instanceof Bitmap) { view.setImageBitmap((Bitmap) o); } return this; } }
附录、布局文件:
activity.xml
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".activity.ActFragmentActivity"> <FrameLayout android:id="@+id/fl_title" android:layout_width="match_parent" android:layout_height="60dp"> </FrameLayout> </LinearLayout>
fragment_title.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="60dp" android:id="@+id/f_rl_root" android:background="#7383DF"> <ImageView android:id="@+id/f_iv_back" android:layout_width="50dp" android:layout_height="50dp" android:paddingLeft="15dp" android:layout_centerVertical="true" android:src="@drawable/back"/> <TextView android:id="@+id/f_tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="张风捷特烈" android:textColor="#fff" android:layout_centerInParent="true" android:textSize="26dp"/> </RelativeLayout>
后记、
1.声明:
[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力
2.连接传送门:
更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
3.联系我
QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328
4.欢迎关注我的微信公众号,最新精彩文章,及时送达:

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android 6.0 权限行为变更详解
版权声明:本文为sydMobile原创文章,转载请务必注明出处! https://blog.csdn.net/sydMobile/article/details/82182615 文章最早发布于我的微信公众号 Android开发者家园 中,欢迎大家扫描下面二维码关注微信公众获取更多知识内容。 本文为sydMobile原创文章,可以随意转载,但请务必注明出处! Android 6.0 权限行为变更 运行时权限说明 Android 6.0 引入了一种新的权限模式,使得用户可以在运行 APP 的时候对一些比较敏感的权限进行管理。这种新的模式可以让用户更好的了解和控制权限,同时为应用精简了安装和自动更新过程。 对于以 Android 6.0 或者更高版本为目标平台的应用,务必在运行的时候检查和请求权限(针对一些危险权限)否则,如果直接调用相关需要特殊权限的方法的话,会导致 APP 的崩溃。检查是否已经拥有权限了可以使用方法 checkSelfPerssion() 如果要请求权限使用方法 requestPermissions 方法。这两个方法都是在 API 23 后引入的,也就是说在之前的 AP...
- 下一篇
1-VVI-材料设计之-TabLayout上标签
[1].既然ViewPager和Fragment都总结完了,那就插一个材料设计中的TabLayout控件吧,这三者关系挺好 [2].TabLayout在上面就是曾经的ViewPager指示器,想当年都是自己封装来用,现在条件好了,安卓给了。 [3].引入包:implementation 'com.android.support:design:26.1.0' [4].在此之前,你应该知道ViewPager和Fragment的组合使用,如果不清楚,可以看一下:Fragment与ViewPager结合 TableLayout.gif 1.Activity的布局 <android.support.design.widget.TabLayout android:id="@+id/tl_tab" android:layout_width="match_parent" android:layout_height="@dimen/dp_48" android:background="@color/colorPrimary"> </android.support.design.widg...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- 设置Eclipse缩进为4个空格,增强代码规范
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Hadoop3单机部署,实现最简伪集群
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS8安装Docker,最新的服务器搭配容器使用