zoukankan      html  css  js  c++  java
  • 5-AVI--Fragment简单封装

    零、前言

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

    9414344-538740a3256561ab.png
    Fragment封装.png

    一、代码实现

    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.欢迎关注我的微信公众号,最新精彩文章,及时送达:
    9414344-c474349cd3bd4b82.jpg
    公众号.jpg
  • 相关阅读:
    vue 对图片进行拖拽到另一个位置
    vue自定义拖动指令
    使用pm2启动nodejs+express+mysql管理系统步骤
    重新学习html和css
    vue监听页面大小变化重新刷新布局
    Docker可视化管理工具DockerUI ,Portainer ,Shipyard对比(转)
    js删除html标记 去掉所有html标记 百度文库内容copy
    安卓模拟器连接端口一览表
    springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题(转)
    常用软件测试工具的对比
  • 原文地址:https://www.cnblogs.com/toly-top/p/9781919.html
Copyright © 2011-2022 走看看