5-AVI--Fragment简单封装

作者: e4e52c116681 | 来源:发表于2018-08-29 14:45 被阅读14次

    零、前言

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

    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>
    
    

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

    相关文章

      网友评论

        本文标题:5-AVI--Fragment简单封装

        本文链接:https://www.haomeiwen.com/subject/nmtkwftx.html