步骤
- 在xml中对需要操作的组件添加id标识。常见标识如下:
- 输入组件,如input_phone
- 按钮组件,如btn_login(工序3只处理这一类组件)
- 信息组件,如show_title
- 容器组件,如contain_title
-
透过ButterKnife Zelezny将 id 映射为 java 组件、将btn映射为点击方法。
-
用butterfork代替butterknife。
- 用
butterfork.Bind;
替换butterknife.Bind
- 用
butterfork.OnClick
替换butterknife.OnClick
- 用
B.id.
替换R.id.
- 删除ButterKnife的相关代码。如:
ButterKnife.bind(this, rootView);
、ButterKnife.unbind(this);
。
- 添加按钮组件的点击事件。
@OnClick(B.id.btn_forget_pass)
public void forgetPass(){
DevUtil.showInfo(getActivity(), "跳转忘记密码页面");
}
@OnClick(B.id.btn_login)
public void login(){
DevUtil.showInfo(getActivity(), "登录");
}
@OnClick(B.id.btn_register)
public void register(){
DevUtil.showInfo(getActivity(), "跳转注册页面");
}
- 添加页面跳转代码
@OnClick(B.id.btn_login)
public void login(){
DevUtil.showInfo(getActivity(), "模拟登录");
IntentUtil.gotoActivity(getActivity(), MainActivity.class);
}
@OnClick(B.id.btn_register)
public void register(){
IntentUtil.gotoActivity(getActivity(), RegisterActivity.class);
}
常用代码片段
一、启动页LaunchFragment
public class LaunchFragment extends BaseFragment {
private long mLaunchViewDisplayTimeLength = 1000L; //启动页显示的时间
@Override
public int getLayoutId() {
return R.layout.fragment_launch;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = super.onCreateView(inflater, container, savedInstanceState);
configLaunchView();
return rootView;
}
/**
* 配置启动界面
*/
public void configLaunchView() {
SimpleSafeTask<Void> waitLaunchViewDismissedTask = new SimpleSafeTask<Void>(
getActivity()) {
@Override
protected Void doInBackgroundSafely() throws Exception {
Thread.sleep(mLaunchViewDisplayTimeLength);
return null;
}
@Override
protected void onPostExecuteSafely(Void resultInfo, Exception e) {
super.onPostExecuteSafely(resultInfo, e);
if(DataManager.isLogined()){
IntentUtil.gotoActivity(getActivity(), MainActivity.class);
}
else{
IntentUtil.gotoActivity(getActivity(), LoginActivity.class);
}
getActivity().finish();
}
};
waitLaunchViewDismissedTask.execute();
}
}
二、列表数据对应的页面
public class NearbyFragment extends AbsListViewNetFragment<TestModel> {
@Bind(B.id.tv_title)
TextView mTvTitle;
@Bind(B.id.net_list)
GridView mGvNearby;
@Override
public int getLayoutId() {
return R.layout.fragment_nearby;
}
@Override
protected ListNetResultInfo<TestModel> onDoInBackgroundSafely(int i, int i1) {
return TestModel.getListNetResultInfo(100);
}
@Override
public int getItemTextViewResourceId() {
return R.id.tv_test;
}
@Override
public int getItemLayoutId() {
return R.layout.item_nearby_detail;
}
@Override
public View bindView(int i, View view, ViewGroup viewGroup) {
return view;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = super.onCreateView(inflater, container, savedInstanceState);
mGvNearby.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// TODO: 16/8/5 跳转详情列表
DevUtil.showInfo(getActivity(), "item 点击");
}
});
return rootView;
}
@OnClick(B.id.btn_goback)
public void exit() {
super.exit();
}
}
网友评论