android常用封装 --- BaseFragment

public abstract class Basefragment extends Fragment{
    public FragmentActivity mActivity;
    /**
     * 此方法可以得到上下文对象
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = initView(inflater,container);
        initFindViewById(view);
        return view;
    }


    /*
         * 当Activity初始化之后可以在这里进行一些数据的初始化操作 
         */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initEvent();
    }
    /**
     * 子类可以复写此方法初始化事件
     */
    protected abstract void initEvent();
    /**
     * 子类在此方法中实现数据的初始化
     */
    protected abstract void initData();
    /**
     * 初始化控件
     */
    protected abstract void initFindViewById(View view);
    /**
     * 子类实现此抽象方法返回View进行展示
     *
     * @return
     */
    protected abstract View initView(LayoutInflater inflater, ViewGroup container);
}
public class f1 extends Basefragment {
    //获取数据的接口
    String url = "http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=20160411091603";
    private ViewPager vp;
    List list=new ArrayList<>();
    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
           getlist();
           vp.setAdapter(new Myadaper(getChildFragmentManager()));
    }

    private void getlist() {
        Childf1 c1=new Childf1();
        Childf2 c2=new Childf2();
        Childf3 c3=new Childf3();
        Childf4 c4=new Childf4();
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
    }

    @Override
    protected void initFindViewById(View view) {
        vp = view.findViewById(R.id.vp);
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.aa,container,false);
        return view;
    }

    class Myadaper extends FragmentPagerAdapter{

        public Myadaper(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}


你可能感兴趣的:(android常用封装 --- BaseFragment)