步驟
- 在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的相關(guān)代碼炎咖。如:
ButterKnife.bind(this, rootView);
、ButterKnife.unbind(this);
寒波。
- 添加按鈕組件的點擊事件乘盼。
@OnClick(B.id.btn_forget_pass)
public void forgetPass(){
DevUtil.showInfo(getActivity(), "跳轉(zhuǎn)忘記密碼頁面");
}
@OnClick(B.id.btn_login)
public void login(){
DevUtil.showInfo(getActivity(), "登錄");
}
@OnClick(B.id.btn_register)
public void register(){
DevUtil.showInfo(getActivity(), "跳轉(zhuǎn)注冊頁面");
}
- 添加頁面跳轉(zhuǎn)代碼
@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();
}
}
二影所、列表數(shù)據(jù)對應(yīng)的頁面
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 跳轉(zhuǎn)詳情列表
DevUtil.showInfo(getActivity(), "item 點擊");
}
});
return rootView;
}
@OnClick(B.id.btn_goback)
public void exit() {
super.exit();
}
}