发表于: 2018-10-02 23:17:57

0 627


一、今天完成的事情

    1.学习了如何Fragment中嵌套Fragment

       要点是获取FragmentManager的时候,调用的是getChildFragmentManager,而不是getSupportFragmentManager。

       在Activity中获取Fragment:

   getSupportFragmentManager().beginTransaction().add(R.id.fl_fragment, homeFragment).commit();

        在Fragment 中获取FragmentManager:

getChildFragmentManager().beginTransaction().add(R.id.fl_explore_fragment, findWorkerFragment).commit();

         


  2.在Fragment中使用findViewById的方法

     在Fragment中不能直接使用findViewById,如果在findViewById前面加上getView(),看上去没什么问题,但运行会发现空指针异常,因为在使用           onCreateView创建视图, inflater插入的布局,用getView()引用,不能识别是哪一个布局,所以就报了空指针。

    正确写法是:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInsatnceState) {
View square = inflater.inflate(R.layout.fragment_explore, container, false);

   findWorkerIV = (ImageView) square.findViewById(R.id.iv_findWorker);
   findEmployerIV = (ImageView) square.findViewById(R.id.iv_findEmployer);
   findWorkerTV = (TextView) square.findViewById(R.id.tv_findWorker);
   findEmployerTV = (TextView) square.findViewById(R.id.tv_findEmployer);
   findWorkerIV.setOnClickListener(this);
   findEmployerIV.setOnClickListener(this);

   initTopTab();

   return square;
}


      3.在Recycler中完成了简单的服务器端数据加载

         用的都是BmobSDK提供的一些现成的增删改查方法。

        


二、明天计划的事情

      1. 实现左右滑动切换Fragment;

      2.继续学习数据库的增删改查操作。


三、遇到的问题

       从服务端查询数据时,直接将所有得到数据一次性加载到了本地,数据量小的话倒是没什么,但如果数据量大了肯定会占用大量的资源,导致应用卡顿甚至崩溃,常见的APP都是一次加载部分数据,滑动ListView或RecyclerView时再加载一部分数据,并且还会在本地做一些缓存,下次加载相同的数据时就不用浪费流量和时间去从服务端加载了。所以我的这个加载数据的方法非常粗糙,需要改进的地方实在太多。

四、收获

    学会了UI布局方面的一些知识,主要是如何Fragment中嵌套Fragment以及如何在Fragment中加载控件、布局等。


返回列表 返回列表
评论

    分享到