前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >5-AVI--Fragment简单封装

5-AVI--Fragment简单封装

作者头像
张风捷特烈
发布2018-09-29 11:13:20
6090
发布2018-09-29 11:13:20
举报
零、前言

[1].每次写Fragment要加载布局,为布局设置内容,挺麻烦的,搞个基类简单封装一下吧 [2].一般封装基类使用模板方法设计模式,基类中做一些常用的不变东西,需要拐点弯的逻辑就弄个抽象方法延迟到子类 [3].textView设置文字,ImageView设置图片两个经常用的方法也提供一下

Fragment封装.png

一、代码实现
1.使用:EVAFragment继承
代码语言:javascript
复制
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
代码语言:javascript
复制
getFragmentManager().beginTransaction().add(R.id.fl_title, new EVAFragment()).commit();
3.封装的基类
代码语言:javascript
复制
/**
 * 作者:张风捷特烈<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
代码语言:javascript
复制
<?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
代码语言:javascript
复制
<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>

本文由张风捷特烈原创,转载请注明 更多安卓技术欢迎访问:https://www.jianshu.com/c/004f3fe34c94 张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com 你的喜欢与支持将是我最大的动力

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.08.29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 零、前言
  • 一、代码实现
    • 1.使用:EVAFragment继承
      • 2.Activity
        • 3.封装的基类
        • 附录、布局文件:
          • activity.xml
            • fragment_title.xml
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档