时间戳的总结

  • 时间戳的理解

    • 时间戳是指当天的总秒数到1970年1月1日08时 0分0秒之间的秒数差。
  • 格式

    • EEEE表示星期几、E表示周几
    • HH表示导0(01:10),H表示不导(1:10) 推荐使用H

2016/1/13 16:20:08

LayoutInflater的inflate方法分析

inflater.inflate(待加载布局,给该布局嵌套一个父布局,是否加载到父布局);

View view = inflater.inflate(R.layout.button_layout, main_layout);
View view = inflater.inflate(R.layout.button_layout, main_layout,true);
//加载一个布局到父布局,不填写参数、默认为true 上面两句代码效果一样

View view = inflater.inflate(R.layout.button_layout, main_layout,false);
//不加载到父布局,但是使用父布局的属性。若要加载需要通过父布局addView的方法加载

View view = inflater.inflate(R.layout.button_layout, null,true);
父布局调用addView后,view的宽高不会显示自己的属性,而是用父布局的属性。

//父布局为null,后面的参数无效

自定义View

分析Adatper里的一句代码

@Override
public View getView(int i , View view , ViewGroup parent)
{
    view = inflater.inflate(R.layout.list_item, parent);
}
  • view 是指list_item里面的根标签
  • parent 是指外部使用该Item的控件,即ListView
  • 由于adapter默认就是要将View加入到ViewGroup里,所以设置第三个参数是无效的。

  • Gradle

    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.android.support:support-v4:22.2.1'
    
  • 设置Item的高度

    在list_item布局文件中的根标签设置minheight
    

Read More

2015/11/19 15:43:46

Broadcast的理解

  • 系统广播接收器
  • 利用intent-filter过滤action
  • 可用作整个应用监听某状态的变化

示例

  • 服务器下发新消息时,通知MainActivity更改UI
  1. 继承BroadcastReceiver
  2. 定义过滤的Action
  3. 添加回调接口
  4. 构造函数初始化接口
  5. 重写onReceive方法(调用接口的方法)
  6. 实现接口、动态注册接口(LocalBroadcastManager类)
  7. 在获取到服务器数据的地方发送广播
  8. 在ondestory里取消广播
Intent intent = new Intent();
intent.putExtra(....);
intent.setAction(Receiver定义的Action);
sendBroadcast(intent);

  • 注册(在MainFest注册一下即可)

  • 使用

    private MsgUpdateReceiver msgReceiver
    @Override
    public void onCreate(Bundle savedInstanceState) {
    msgReceiver = new MsgUpdateReceiver(this); //实例化
    IntentFilter filter = new IntentFilter(MsgUpdateReceiver.MSG_UPDATE);//设置过滤器
    //利用LocalBroadcastManager调用其注册方法
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(msgReceiver, filter);
    
    }
    

2015/12/8 17:00:41

Fragment里注册广播

  • 不能在OnCreateView里面注册,要在onCreate方法里面注册
  • 记得在ondestory方法里注销广播

2015/11/18 15:03:43

回调函数Callback的理解

  1. 一段程序代码块中,可设定一部分代码,该代码块在未来才实现。
  2. 回调函数分成3部分

    • 回调接口 —— 提供数据的人编写
    • 提供数据的人

      • 预留接口给他人实现(定义接口、创建接口引用)
      • 提供注册接口的方法(注册接口)
      • 在给数据的地方调用接口的方法(通过接口引用调用方法)
* 获取返回数据的人

    - (实现回调接口) implements Callback
    -  注册回调函数

Read More