hammer崔的程序世界

我的生涯一片无悔,我想起那个午夜在灯泡下的抠代码,那是我逝去的青春!


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

  • 搜索
close

angularJs学习1:初识angular

发表于 2016-03-02   |   分类于 angulasJs   |  

前言

angular是google搞出来的js框架,后端程序如果想做前段,可以考虑尝试下。因为其按照mvc的架构来做框架,并引入了mvvm的机制,就是view动态数据绑定

应用场景

比较适合单页面,更符合app的设计思路,当然,商城页面也可以写,如苏宁易购,主要是有后端开发经验,更容易理解

什么事单页面场景?

如何学习

  1. angular学习站,模仿w3school

  2. 伯乐在线的一篇文章

配合

学习这货主要是为了配合ionic的开发

要点

  1. 只要看到ng-而不是id,说明用的是angular绑定,而不是jquery的dom查找
  2. angular基本类似于javascript,但是可以直接在html写,angular没有for循环,==条件判断,及异常,但是有自己的实现机制
    比如:
    ```

循环对象:



  • undefined, undefined



```

  1. 除了这些内置的指令ng-app,ng-init,ng-model,ng-repeat外,还可以自定义指令,但是在命名的时候用驼峰法,使用的时候用-间隔。

angularJs学习1:初始angular

发表于 2016-03-02   |   分类于 angulasJs   |  

hexo gihthub创建blog

发表于 2016-03-02   |   分类于 github   |  

安装教程

hexo的安装

参考教程1 (http://blog.csdn.net/poem_of_sunshine/article/details/29369785/)
参考教程2 http://www.jianshu.com/p/089fbfe71eef

主题的安装

hexo博客的主题如何选择,来自于知乎社区的讨论
我自己使用的是jacman主题,推荐一个博客,里面有详细的jacman主题配置指南

常用部件安装

参考博客,详细讲解搜索等等部件的安装

1 站内搜索

百度站内搜索,使用教程,但是一直搞不定,暂时放弃了。

微搜索,使用教程

switfype搜索,使用教程

如何提高搜索引擎的收录

1 生成站点地图,参考blog,貌似没啥效果

2 让GitHub Pages博客支持百度搜索引擎收录,这个方案太麻烦了,放弃了

3 在知乎上有更好的实现方案,目前尚未实践,这个方案更详细的实施步骤

常用命令行

参考地址 https://segmentfault.com/a/1190000002632530

hexo n “博客名” == hexo new “我的博客” #新建文章

hexo p== hexo publish

hexo g == hexo generate#生成

hexo s == hexo server #启动服务预览

hexo d == hexo deploy#部署

Bug集锦

1 输入 0.0.0.0:4000没反应

根据知乎的答案,显示是端口被占用了,修改端口号就行了

1
hexo server -p 新端口号(比如11111)

修改ip地址,位于\node_modules\hexo-server\index.js

1
2
3
4
hexo.config.server = assign({
log: false,
ip: '127.0.0.1'
}, hexo.config.server)

这样修改完成后

1
2
INFO  Start processing
INFO Hexo is running at http://127.0.0.1:11111/. Press Ctrl+C to stop.

2 找不到git部署

1
ERROR Deployer not found: git

解决方法

1
npm install hexo-deployer-git --save

部署类型设置git
hexo 3.0 部署类型不再是github,_config.yml 中修改

1
2
3
4
deploy:
type: git
repository: git@***.github.com:***/***.github.io.git
branch: master

3 hexo s命令不起作用

1
npm install hexo-server --save

多pc端的同步问题

这个问题是不是大家也思考过,推荐一个bolg。

讲的太好了,我就不细讲了,我采用的第二种实践,在oschina新建一个工程用来存储blog工程。

android最佳实践4:4.4以下沉浸式体验框架SystemBarTint

发表于 2015-12-15   |   分类于 android   |  

前言

android也实现了类似于ios的状态栏透明,变色效果,称为沉浸式体验。但是,4.4及以上才能用!!所以强烈推荐这个SystemBarTint框架,能实现同样的效果,而且最低兼容API10.

注意

当不采用MD设计规范时,采用本方案最合适。若使用MD方案了,请最好使用toolbar取代actionbar.具体本方案的兼容性,还需要继续测试。

4.4 以下版本的方案

引入依赖

1
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

如何使用

1 Activity的onCreate方法添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//当api》=19时
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}

// 创建状态栏的管理实例
SystemBarTintManager tintManager = new SystemBarTintManager(this);
// 激活状态栏设置
tintManager.setStatusBarTintEnabled(true);
// 激活导航栏设置,当使用actionbar的时候开启
tintManager.setNavigationBarTintEnabled(true);
// 设置一个颜色给系统栏
tintManager.setTintColor(Color.parseColor("#99000FF"));
// 设置状态栏需颜色或背景图
tintManager.setStatusBarTintResource(R.color.statusbar_bg);
// 设置一个样式背景给导航栏,当使用actionbar的时候使用
tintManager.setNavigationBarTintResource(R.drawable.my_tint);

}

@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}

2 activity的xml配置文件

记得新增属性

1
2


3 Theme的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- 我的activity主题,应用于所有的 activity-->
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- activity切换动画-->
<item name="android:windowAnimationStyle">@style/activityAnimation</item>
<!-- 不显示Title Bar -->
<item name="android:windowNoTitle">true</item>
<!-- 避免控件都会顶到状态栏上 -->
<item name="android:fitsSystemWindows">true</item>
<!-- API 14 theme 的自定义 can go here. -->
<item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>

<!--activity统一的切换动画 -->
<style name="activityAnimation" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/push_left_in</item>
<item name="android:activityOpenExitAnimation">@anim/push_left_out</item>
<item name="android:activityCloseEnterAnimation">@anim/back_right_in</item>
<item name="android:activityCloseExitAnimation">@anim/back_right_out</item>
</style>

<!--API 14主题的自定义 -->
<style name="ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/orange</item>
</style>

4.4以上版本的方案(不适用第三方框架)

如何实现,请参考blog 安卓4.4以上修改状态栏颜色

android最佳实践3:retrofit框架

发表于 2015-12-05   |   分类于 android   |  

retrofit,rxjava编写,符合restful规范的网络请求框架

它的底层由okhttp实现,okhttp目前是最好的网络请求框架之一了,google的部分官方项目也在用。
完全rxjava的语法,响应式编程。
restful的接口规范。
总之,使用起来很方便,大大提高生产效率,nice!!。

ps:我们采用的是retrofit2.0

引入依赖

compile 'com.squareup.retrofit2:retrofit:2.0.0'

  • 最低java7 android2.3

如何使用

参考资料

retrofit github地址

泡在网上的日子写的 Retrofit 2.0:有史以来最大的改进

用 Retrofit 2 简化 HTTP 请求

rerofit官方api讲解

android最佳实践2:rxjava框架

发表于 2015-12-03   |   分类于 android   |  

rxjava框架,感受响应式编程的魔力

引入依赖

编辑app的build.gradle文件,新增

1
2
3
4
5
6
dependencies {
...
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
...
}

如何使用

请先学习参考资料

参考资料

抛物线的给 Android 开发者的 RxJava 详解

大头鬼Bruce深入浅出RxJava(一:基础篇)

android最佳实践1:retrolambda框架

发表于 2015-12-01   |   分类于 android   |  

retrolambad框架,让android也能兼容java8l的ambda表达式

我们都知道,java在复杂的内部类,内联函数实现的时候,会产生很多的回掉,代码阅读性会变得很差,经常会遇到迷之缩进。java8开始引入了ladmbad表达式,鼓掌!!但是,你妹,android只支持最高jdk7,坑爹呢。不要紧,伟大的歪果仁搞出了兼容性框架,gradle-retrolambda,戳戳看。

2016年新更新:google的亲儿子android studio2.1以及支持java8了,并用了新的编译器jack compile替代老旧的dvm了,再次鼓掌,目前还是预览版,尝鲜的小白鼠走起。

##如何使用retrolambda

1 下载jdk8,并配置环境变量

2 修改工程的build.gradle

1
2
3
4
5
6
7
8
9
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
+ classpath 'me.tatarka:gradle-retrolambda:3.2.4'
}
}

3 修改module,比如app的buidle.gradle

1
2
3
4
5
6
7
8
9
10
11
12

android {
...
//打包用java8
+ compileOptions {
+ sourceCompatibility JavaVersion.VERSION_1_8
+ targetCompatibility JavaVersion.VERSION_1_8
+ }
...
}

+ apply plugin: 'me.tatarka.retrolambda'

4 测试

参考资料

Android 上的 Java 8:使用 retrolambda

Android开发最佳实践

android Material Desigin控件学习:综述

发表于 2015-11-28   |   分类于 android   |  

android Material Desigin控件学习:综述

com.android.support:design是android的material design设计风格的兼容库

都是兼容库,区别是这个库多了个Design。 Android Support Library 22.1只是支持了一些基本控件的材料设计化,但是这个库更多的是对一些特效的实现,其实跟github的很多=库有关系,只不是官方把这些库给标准化了
参看教程地址 泡神的博客

如何使用

1
2
3
compile 'com.android.support:appcompat-v7:23.1.1'

compile 'com.android.support:design:23.1.1'

新的Material Design风格主要体现在一些新控件的使用

1 DrawerLayout

注意事项 DrawerLayout只能有两个子视图,顺序第一个当前视图,顺序第二个是弹出视图,弹出视图的laytout_gravity=”left”,否则都是错误的

2 Navigation View 抽屉导航

其实就是在drawLayout滑出来的控件上新增内容,比旧版本更省时间了

3 ToolBar

可以取代actionbar控件

4 输入框控件的悬浮标签

5 悬浮操作按钮floating action button

6 协作滚动控件CoordinatorLayout

appBarLayout,collapsing toolbar ,toolbar都要放到这个协同层里,就实现了协同滚动

7 Snackbar


为一个操作提供轻量级的,快速的反馈是使用snackbar的最好时机。snackbar显示在屏幕的底部,包含了文字信息与一个可选的操作按钮。在指定时间结束之后自动消失。另外,用户还可以在超时之前将它滑动删除。

8 选项卡TabLayout

9 可伸缩折叠的Toolbar (Collapsing Toolbar)

背景大图能根据手势滚动而收缩,可用于对toolbar的补充

1
app:contentScrim="?attr/colorPrimary",CollapsingToolbarLayout

这个属性是设置折叠后Toolbar的颜色.

1
app:layout_scrollFlags="scroll|exitUntilCollapsed",

这是两个Flag控制滚动时候CollapsingToolbarLayout的表现.

  • Scroll, 表示向下滚动列表时候,CollapsingToolbarLayout会滚出屏幕并且消失(原文解释:this flag should be set for all views that want to scroll off the screen - for views that do not use this flag, they’ll remain pinned to the top of the screen)

  • exitUntilCollapsed, 表示这个layout会一直滚动离开屏幕范围,直到它收折成它的最小高度.(原文解释:this flag causes the view to scroll off until it is ‘collapsed’ (its minHeight) before exiting)
    app:layout_collapseMode=”parallax”,这是控制滚出屏幕范围的效果的
    1) parallax,表示滚动过程中,会一直保持可见区域在正中间.
    2) pin,表示不会被滚出屏幕范围.

具体学习demo

预览图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!--以后actionbar的布局是AppBarLayout,因为appBar越来越复杂了-->
<android.support.design.widget.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- toolbar内容-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"
/>

<!-- tablayout内容-->
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#ef5350"
app:tabSelectedTextColor="#1976d2"
app:tabTextColor="#90caf9"
/>

</android.support.design.widget.AppBarLayout>

<!--增强型listview -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="0.1dp"
app:fabSize="normal"
/>
</android.support.design.widget.CoordinatorLayout>

java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* toolbar的初始化,tool新增
*/
public void initToolBar(){
//使用toolBar取代actionBar,以及toolBar的使用
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
drawerToggle = new ActionBarDrawerToggle(LessonThreeActivity.this,drawerLayout,R.string.drawer_open_content,R.string.drawer_close_content);
//设置标题选项卡
TabLayout tabLayout= (TabLayout)findViewById(R.id.tabLayout);
for (int i=0;i<8;i++)
tabLayout.addTab(tabLayout.newTab().setText("选项卡:"+i));

tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
//设置RecyclerView
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayout.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
RecyclerView.Adapter mAdapter = new MyRecyclerViewAdapter(this);
mRecyclerView.setAdapter(mAdapter);
}

private class MyViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public MyViewHolder(View itemView)
{
super(itemView);
textView = (TextView)itemView.findViewById(android.R.id.text1);
}
}

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyViewHolder>{
private LayoutInflater layoutInflater;
public MyRecyclerViewAdapter(Activity activity)
{
super();
layoutInflater = activity.getLayoutInflater();
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = layoutInflater.inflate(android.R.layout.simple_list_item_1, null);
MyViewHolder holder = new MyViewHolder(v);
return holder;

}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText("数据position:" + position);
}

@Override
public int getItemCount() {
return 40;
}
}

android最佳实践5:EventBus

发表于 2015-10-05   |   分类于 android   |  

学习EventBus的使用

介绍EventBus

为什么要使用EventBus呢?

当我们的Fragment过多的时候,组件之间传递信息会变得异常繁琐,单例不是很好的解决方案,通常我们会使用观察者模式来解决这个问题。EventBus就是一个很不错的观察者模式框架。

EventBus实现了什么?

主要用于事件的发布和订阅。EventBus定义:是一个发布 / 订阅的事件总线。
包含4个成分

  • Publisher:发布者
  • Subscriber:订阅者
  • Event:事件,Event可以是任何类型的对象
  • 总线。

那么这四者的关系是什么呢?很明显:订阅者订阅事件到总线,发送者发布事件。

订阅者Subscriber

在EventBus中,使用约定来指定事件订阅者以简化使用。即所有事件订阅都都是以onEvent开头的函数,具体来说

  • onEvent 它和ThreadModel中的PostThread对应,这个也是默认的类型,当使用这种类型时,回调函数和发起事件的函数会在同一个线程中执行,所以事件处理时间不应太长
  • onEventMainThread 它和ThreadModel中的MainThread对应,当使用这种类型时,回调函数会在主线程中执行,这个在Android中非常有用,因为在Android中禁止在子线程中更新UI。处理时间也不能太长。
  • onEventBackgroundThread 当使用这种类型时,如果事件发起函数在主线程中执行,那么回调函数另启动一个子线程,如果事件发起函数在子线程执行,那么回调函数就在这个子线程执行
  • onEventBusAsync 当使用这种类型时,不管事件发起函数在哪里执行,都会另起一个线程去执行回调,但最好限制线程的数目。

发布者Publisher

可以在任意线程任意位置发送事件,直接调用EventBus的post(Object)方法,可以自己实例化EventBus对象,但一般使用默认的单例就好了:EventBus.getDefault().post(Object),根据post函数参数的类型,会自动调用订阅相应类型事件的函数。

使用EventBus

引入依赖

编辑app的build.gradle

1
compile 'org.greenrobot:eventbus:3.0.0'

简单使用步骤

  1. 定义事件类型

    1
    2
    public class MyEvents {
    }
  2. 在订阅者类里,新建事件处理方法

    1
    2
    3
    4
    @Subscribe
    public void onEventMainThread(MyEvent events){

    }

千万别忘记写@Subscribe注解了,详情见EventBus 注册错误no public methods with the @Subscribe annotation

  1. 注册订阅者,在构造函数或onCreate(Bundle savedInstanceState)

    1
    EventBus.getDefault().register(this);
  2. 取消订阅者,在onDestory()方法

    1
    EventBus.getDefault().unregister(this);
  3. 发送事件,可以在任意方法,线程

1
EventBus.getDefault().post(new MyEvent());

参考文献

AngelDevil的快速Android开发系列通信篇之EventBus
Android EventBus实战 没听过你就out了
Android中开源库EventBus使用详解

1…67
hammercui

hammercui

69 日志
13 分类
19 标签
RSS
github
Links
  • logicool的技术专栏
  • xiekw2010的专栏
  • 江清清的技术专栏
© 2017 hammercui
由 Hexo 强力驱动
主题 - NexT.Pisces