Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Blankj/master
pull newest to merge
- Loading branch information
Showing
11 changed files
with
626 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# These are supported funding model platforms | ||
|
||
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] | ||
patreon: # Replace with a single Patreon username | ||
open_collective: # Replace with a single Open Collective username | ||
ko_fi: # Replace with a single Ko-fi username | ||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel | ||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry | ||
liberapay: # Replace with a single Liberapay username | ||
issuehunt: # Replace with a single IssueHunt username | ||
otechie: # Replace with a single Otechie username | ||
custom: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/donate.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
* 面试模块:<!-- Android --> | ||
* 面试题目:<!-- RecyclerView 性能优化 --> | ||
* 作者:<!-- [Blankj](https://github.com/Blankj) --> | ||
* 参考答案: | ||
|
||
<!-- 答案写入下方 --> | ||
|
||
|
||
<!-- P.S:如果要提交 pr,务必确保和我当前文章格式一致,文章中不要参有和文章无关的链接 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# EditText 搜索优化 | ||
|
||
## 描述 | ||
|
||
设计一个 EditText 的文本监听器,停止输入 1s 后,如果文本发生变化则触发监听器。 | ||
|
||
例子: | ||
文本内容是 111,111 -> 1111 -> 11111,连续输入都小于 1s,在输完后的 1s 触发监听器为 11111; | ||
文本内容是 111,111 -> 1111 -> 111,连续输入都小于 1s,在输完后的 1s 不触发监听器; | ||
|
||
类似微信的客户端搜索,不同的是微信在 111 -> 1111 -> 111 是会触发改变的。 | ||
|
||
<!-- more --> | ||
|
||
效果如下所示,注意观察 title 文本的改变: | ||
|
||
![demo.gif](http://ww1.sinaimg.cn/large/b75b8776ly1g6prj25wgsg206k0dwaod.gif) | ||
|
||
## 参考答案 | ||
代码很简单,结合注释参考即可,小功能从构思到编码到完成到优化,其实也是要花不少时间的,希望可以帮到你们哈。 | ||
```java | ||
public class SearchEditText extends EditText { | ||
|
||
private static final long LIMIT = 1000; | ||
|
||
private OnTextChangedListener mListener; | ||
private String mStartText = "";// 记录开始输入前的文本内容 | ||
private Runnable mAction = new Runnable() { | ||
@Override | ||
public void run() { | ||
if (mListener != null) { | ||
// 判断最终和开始前是否一致 | ||
if (!StringUtils.equals(mStartText, getText().toString())) { | ||
mStartText = getText().toString();// 更新 mStartText | ||
mListener.onTextChanged(mStartText); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
public SearchEditText(Context context) { | ||
super(context); | ||
} | ||
|
||
public SearchEditText(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
/** | ||
* 在 LIMIT 时间内连续输入不触发文本变化 | ||
*/ | ||
public void setOnTextChangedListener(OnTextChangedListener listener) { | ||
mListener = listener; | ||
} | ||
|
||
@Override | ||
protected void onTextChanged(final CharSequence text, int start, int lengthBefore, int lengthAfter) { | ||
super.onTextChanged(text, start, lengthBefore, lengthAfter); | ||
// 移除上一次的回调 | ||
removeCallbacks(mAction); | ||
postDelayed(mAction, LIMIT); | ||
} | ||
|
||
@Override | ||
protected void onDetachedFromWindow() { | ||
super.onDetachedFromWindow(); | ||
removeCallbacks(mAction); | ||
} | ||
|
||
public interface OnTextChangedListener { | ||
void onTextChanged(String text); | ||
} | ||
} | ||
``` | ||
|
||
|
||
|
||
## 结语 | ||
|
||
我正在打造一个帮助 Android 开发者们拿到更好 offer 的面试库————**[安卓 offer 收割基](https://github.com/Blankj/AndroidOfferKiller)**,欢迎 star,觉得不错的可以持续关注,有兴趣的可以一起加入进来和我一同打造。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# 单例模式 | ||
|
||
## 单例模式的特点: | ||
|
||
1. 单例类只能有一个实例; | ||
2. 单例类必须自己创建自己的唯一实例; | ||
3. 单例类必须给所有其他对象提供这一实例。 | ||
|
||
|
||
## 懒汉类 | ||
|
||
```java | ||
// 懒汉式单例类,在第一次调用的时候实例化自己 | ||
public class Singleton { | ||
private Singleton() { | ||
} | ||
|
||
private volatile static Singleton singleton = null; // 声明成 volatile | ||
|
||
// 静态工厂方法 | ||
public static Singleton getInstance() { | ||
if (singleton == null) { | ||
synchronized (Singleton.class) { | ||
if (singleton == null) { | ||
singleton = new Singleton(); | ||
} | ||
} | ||
} | ||
return singleton; | ||
} | ||
} | ||
``` | ||
|
||
|
||
## 饿汉类 | ||
|
||
``` java | ||
// 饿汉式单例类,在类初始化时,已经自行实例化 | ||
public class Singleton { | ||
private Singleton() { | ||
} | ||
|
||
private static final Singleton single = new Singleton(); | ||
|
||
// 静态工厂方法 | ||
public static Singleton getInstance() { | ||
return single; | ||
} | ||
} | ||
``` | ||
|
||
但当单例类当实现了 `Serializable` 接口后,反序列化时单例会被破坏,此时需要重写 `readResolve`,才能保证其反序列化依旧是单例,如下所示: | ||
|
||
``` java | ||
private Object readResolve() throws ObjectStreamException { | ||
return single; | ||
} | ||
``` | ||
|
||
|
||
## 内部静态类 | ||
|
||
```java | ||
public class Singleton { | ||
private static class LazyHolder { | ||
private static final Singleton INSTANCE = new Singleton(); | ||
} | ||
|
||
private Singleton() { | ||
} | ||
|
||
public static Singleton getInstance() { | ||
return LazyHolder.INSTANCE; | ||
} | ||
} | ||
``` | ||
|
||
|
||
## 枚举类 | ||
|
||
``` java | ||
public enum Singleton { | ||
INSTANCE; | ||
|
||
public void whateverMethod() { | ||
} | ||
} | ||
``` | ||
|
||
枚举类是 Effective Java 作者 Josh Bloch 提倡的方式,它不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象,可谓是很坚强的壁垒,不过在实际工作中,我很少看见有人这么写过。 | ||
|
||
|
||
## 结论 | ||
|
||
工作中我基本都是用内部静态类,具有懒加载和线程安全的效果,如果涉及到反序列化创建对象时我会试着使用枚举的方式来实现单例,如果每次书写嫌麻烦的话,可以在 Android Studio 中设置 `Preferences -> Editor -> Live Templates` 来快速创建出自己需要的单例。 | ||
|
||
|
||
## 结语 | ||
|
||
我正在打造一个帮助 Android 开发者们拿到更好 offer 的面试库————**[安卓 offer 收割基](https://github.com/Blankj/AndroidOfferKiller)**,欢迎 star,觉得不错的可以持续关注,有兴趣的可以一起加入进来和我一同打造。 |
Oops, something went wrong.