码迷,mamicode.com
首页 > 其他好文 > 详细

开源框架:fragmentargs 的优势和使用方法

时间:2015-01-31 13:00:51      阅读:1327      评论:0      收藏:0      [点我收藏+]

标签:fragmentargs   fragment   bundle   

/**转帖注明出处*/

 FragmentArgs开源库的地址:https://github.com/sockeqwe/fragmentargs

    FragmentArgs是一个轻量级的开源库,他用来为你的Frgament生成准确java代码,这个库使用简单。下边是使用FragmentArgs的示例代码:

import com.hannesdorfmann.fragmentargs.FragmentArgs;
import com.hannesdorfmann.fragmentargs.annotation.Arg;
 
public class MyFragment extends Fragment {
 
@Arg
int id;
 
@Arg
String title;
 
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FragmentArgs.inject(this); // read @Arg fields
}
 
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
 
Toast.makeText(getActivity(), "Hello " + title,
Toast.LENGTH_SHORT).show();
}
}
 大家会明显的发现使用FragmentArgs后我们少写了很多代码,这就是FrgamentArgs的强大之处。FragmentArgs会为你的Fragment中的注解属性生成样板代码。在你的Activity中你将使用FragmentArgs生成的Builder类代替new MyFragment(int id,String title)或静态的MyFragment.newInstance(int id,String title)方法。示例代码如下:
public class MyActivity extends Activity {
 
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
 
int id = 123;
String title = "test";
 
// Using the generated Builder
Fragment fragment =
new MyFragmentBuilder(id, title)
.build();
 
// Fragment Transaction
getFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
 
}
你可能注意到Fragment的onCreate()方法里边的FragmentArgs.inject(this)语句。当调用该语句的时候FragmentArgs将会为你的Fragment生成相应的代码。你可能会问那我是不是必须要在我的每一个Fragment的onCreate方法里边都要调用这个方法呀?你并不需要在你的每一个Fragment的onCreate方法里边都调用这个语句,FragmentsArgs.inject(this)语句支持继承,你只需要写一个Fragment基类,在基类的onCreate方法里调用FragmentArgs.inject(this)语句,然后让其它的Fragment继承这个基类就行了。就像下边的代码这样
public class BaseFragment extends Fragment {
 
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FragmentArgs.inject(this); // 读取 @Arg 属性
}
}
 
public class MyFragment extends BaseFragment {
 
@Arg
String title;
 
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
 
Toast.makeText(getActivity(), "Hello " + title,
Toast.LENGTH_SHORT).show();
}
 
}

   首先我们从网址https://github.com/sockeqwe/fragmentargs处下载代码,解压后的目录如下所示


           这里的annotation和processor文件夹是两个项目,processor项目依赖于annotation项目。当你点开annotation目录和processor目录的时候你会发现他们并不是Eclipse项目。我们需要将这里的annotation和processor合并到一个项目里边并生成.jar文件。首先我们先新建一个java项目(注意是java项目不是android项目)

然后我们将annotation目录下的代码拷到刚新建的fragmentargs项目的src目录下(注意拷的时候从com目录开始拷)


 

然后我们在将processor目录下的代码拷到fragmentargs目录下(注意也是从com目录开始拷)


除此之外我们还需要将路径processor\src\main\resources里边的文件夹META-INF拷到fragmentargs项目的根目录下(即META-INFsrc是同级目录)


下边我们该生成.jar文件夹了。



 

点击finish后可能会弹出。。不要管他点击ok

现在.jar文件就生成成功了。

 

      现在我们来新建一个android项目,并将生成的.jar文件拷到新建的android项目的libs目录下:


 

同时需要额外设置的选项有,

1)           工程属性—>java Compiler Annotation Processing 勾选Enable project specific settingsEnable annotation prcessing

2)           如果需要生成源文件,则可以设置目标文件的存储路径。

3)           Factory Path中指定annotation processor所在的jar包。



经过上边的一系列设置之后,你就可以使用FragmentArgs库了。但是谷歌提供给开发者的Eclipse默认是没有带Annotation processing的。

 

解决方法: 

  需要配置一个插件:

技术分享

就这些了,用起来确实不fragment加bundle方便。

开源框架:fragmentargs 的优势和使用方法

标签:fragmentargs   fragment   bundle   

原文地址:http://blog.csdn.net/kern_/article/details/43319241

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!