美文网首页孚睿科技·ANDROID知识总结
[Android流水化开发]工序3:如何增加点击事件,将各个页面

[Android流水化开发]工序3:如何增加点击事件,将各个页面

作者: 沉思的Panda | 来源:发表于2016-08-04 15:46 被阅读135次

    步骤

    1. 在xml中对需要操作的组件添加id标识。常见标识如下:
    • 输入组件,如input_phone
    • 按钮组件,如btn_login(工序3只处理这一类组件)
    • 信息组件,如show_title
    • 容器组件,如contain_title
    1. 透过ButterKnife Zelezny将 id 映射为 java 组件、将btn映射为点击方法。

    2. 用butterfork代替butterknife。

    • butterfork.Bind; 替换 butterknife.Bind
    • butterfork.OnClick 替换 butterknife.OnClick
    • B.id.替换R.id.
    • 删除ButterKnife的相关代码。如:ButterKnife.bind(this, rootView);ButterKnife.unbind(this);
    1. 添加按钮组件的点击事件。
    @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(), "跳转注册页面");
    }
    
    1. 添加页面跳转代码
    @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();
        }
    }
    

    【Android流水化开发】相关文章

    1. 理念·Android端开发效率如何提升4488倍?
    2. 工序1:初始化项目框架
    3. 工序2:绘制xml
    4. 工序3:如何增加点击事件,将各个页面连接在一起?
    5. 工序4:封装数据加载、上传接口,并显示、跳转

    相关文章

      网友评论

      本文标题:[Android流水化开发]工序3:如何增加点击事件,将各个页面

      本文链接:https://www.haomeiwen.com/subject/gaicsttx.html