GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Tagged to v2.2.0-beta1 · javandcpp/MultiType@67e1128

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README-zh-cn.md
  • README.md
  • library
    • build.gradle
    • gradle.properties
  • sample
    • build.gradle
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README-zh-cn.md‎

Copy file name to clipboardExpand all lines: README-zh-cn.md
+16-41Lines changed: 16 additions & 41 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ GitHub: [https://github.com/drakeet/MultiType](https://github.com/drakeet/MultiT
1919

2020
```groovy
2121
dependencies {
22-
compile 'me.drakeet.multitype:multitype:2.1.1'
22+
compile 'me.drakeet.multitype:multitype:2.2.0-beta1'
2323
}
2424
```
2525

@@ -72,33 +72,25 @@ public class TextItemViewProvider
7272
}
7373
```
7474

75-
#### Step 3. 好了,你不必再创建新的类文件了,只要往你 `Application` 中进行注册类型,同时在 `Activity` 中加入 `RecyclerView` 和 `List<TypeItem>` 即可,示例:
75+
#### Step 3. 好了,你不必再创建新的类文件了,在 `Activity` 中加入 `RecyclerView` 和 `List<Item>` 并注册你都类型即可,示例:
7676

7777

7878
```java
79-
public class App extends Application {
80-
81-
@Override public void onCreate() {
82-
super.onCreate();
83-
MultiTypePool.register(TextItem.class, new TextItemViewProvider());
84-
MultiTypePool.register(ImageItem.class, new ImageItemViewProvider());
85-
MultiTypePool.register(RichItem.class, new RichItemViewProvider());
86-
MultiTypePool.register(Category.class, new CategoryItemViewProvider());
87-
MultiTypePool.register(PostRowItem.class, new PostRowItemViewProvider());
88-
MultiTypePool.register(PostList.class, new HorizontalItemViewProvider());
89-
}
90-
}
91-
```
79+
public class NormalActivity extends AppCompatActivity {
9280

93-
```java
94-
public class MainActivity extends AppCompatActivity {
81+
private MultiTypeAdapter adapter;
82+
private Items items;
9583

9684
@Override protected void onCreate(Bundle savedInstanceState) {
9785
super.onCreate(savedInstanceState);
9886
setContentView(R.layout.activity_main);
9987
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
10088

101-
Items items = new Items();
89+
items = new Items();
90+
adapter = new MultiTypeAdapter(items);
91+
adapter.applyGlobalMultiTypePool();
92+
adapter.register(RichItem.class, new RichItemViewProvider());
93+
10294
TextItem textItem = new TextItem("world");
10395
ImageItem imageItem = new ImageItem(R.mipmap.ic_launcher);
10496
RichItem richItem = new RichItem("小艾大人赛高", R.mipmap.avatar);
@@ -109,7 +101,7 @@ public class MainActivity extends AppCompatActivity {
109101
items.add(richItem);
110102
}
111103

112-
recyclerView.setAdapter(new MultiTypeAdapter(items));
104+
recyclerView.setAdapter(adapter);
113105
}
114106
}
115107
```
@@ -118,29 +110,12 @@ public class MainActivity extends AppCompatActivity {
118110

119111
你可以阅读源码项目中的 `sample` 模块获得更多信息和示例,当完整的示例代码运行起来,它是这样子的:
120112

121-
<img src="art/screenshot-normal.png" width=270 height=486/> <img src="art/screenshot-bilibili.png" width=270 height=486/>
122-
123-
## 性能测试
124-
125-
找了一个小米 2s 来对 `MultiType` 进行测试,注入 9999 个 `ItemContent` class 和 `ItemViewProvider` 对象,`ItemContent` 包含 12 个随机 String,`ItemViewProvider.TestViewHolder` 包含 12 个 `TextView` 对象,并将我们使用的 Type 排到第 10000 位以后(检索严格模式)。
126-
127-
测试结果表明,性能极好。初始化注册 10000 个类型,只要 10 毫秒左右!而且内存占用也极低,因为类型 class 和 provider 对象都是非常非常轻薄的对象,后者虽然是以传统实例注册(其实 class 也是实例),但 provider 层面不持有任何对象,它只提供生产方法;另外,尽管 target index 在 10000 位以后,但丝毫不会影响列表滑动流畅性,因为计算个 10000 次,对于我们的手机 CPU,简直比我们人类 1 + 1 还简单的事情。这更近坚定了我使用全局类型池的设计。
128-
129-
![](http://ww2.sinaimg.cn/large/86e2ff85gw1f6sembftdjj20rq03utbp.jpg)
130-
131-
那么问题来了,即使是淘宝,有超过 10000 个 item types 吗?我们真的需要局部类型池吗?答案我想是显然的。
132-
133-
## Q&A (English Version later)
134-
135-
**Q: 为什么使用静态或者全局类型池?(Why we need static and single TypePool?)**
113+
<img src="art/screenshot-normal.png" width=270 height=486/> <img src="art/screenshot-bilibili.png" width=270 height=486/> <img src="art/screenshot-multigrid.png" width=270 height=486/>
136114

137-
A: 我不反对局部或临时类型池的设计,你可以 fork 这个项目自行实现,它们对于内存更加友好(但也只是微小优势而已),但在我看来,全局类型池在多方面更好:
138-
- 它能够**显式**连接 Type 和它的 Item View,能够在同一地方统一 register,这将避免分散,带来很好的直观性和可管理性;
139-
- 一个应用不会有超级大量的类型定义,类型 class 和 provider 对象都是非常轻薄的对象,直接静态存于内存,并不会导致内存泄漏和大的内存占用问题,几乎可以忽略;
140-
- 至于要不要支持 optional 的局部类型池参数,我也是不喜欢支持的,前面说了,这是没必要的,而且若是可选(optional)也会使用户疑惑:“到底要还是不要?”
115+
And it has been used in [drakeet/TimeMachine](http://github.com/drakeet/TimeMachine), you could check the `Message extends TypeItem` to learn **how to custom TypeItem** and it is recommended to read the `MessageViewProvider`, they are all great guide:
141116

142-
因此我喜欢和坚持使用全局静态类型池,它不会带来什么问题,而且好处诸多,有人给我提交了使用反射的方法来自动获取类型连接,为了避免性能话题,我不喜欢反射,而且将类型连接变得复杂和不可见性未必是好事。我一直坚持的原则是:写简单的代码,写可读的代码,实现复杂的需求(你们看我的代码是不是感觉很自然而然而且可读性十分好?)
117+
[<img src="http://ww3.sinaimg.cn/large/86e2ff85gw1f55jnr2zjij20bx0bx0v3.jpg" width=256 height=256/>](http://github.com/drakeet/TimeMachine)
143118

144-
## 题外话
119+
## Change logs & Releases
145120

146-
这个类库成品看来是挺精巧的,或者说轻巧,但一个人从无到有把它设计和创造出来,还是费了很多思考和多次推翻重构,其中有些点看起来可能自然而然,但是它在开发过程中可能都是一个小坎,如果没有找到合适的结构或设计,整体可能就不能搭建起来,使用也可能没那么简单和灵活。所以,要是有人感兴趣,之后可以分享一下开发过程中遇到的问题和思考,还是很有意思的 : )
121+
https://github.com/drakeet/MultiType/releases
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+11-46Lines changed: 11 additions & 46 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ In your `build.gradle`:
2222

2323
```groovy
2424
dependencies {
25-
compile 'me.drakeet.multitype:multitype:2.1.1'
25+
compile 'me.drakeet.multitype:multitype:2.2.0-beta1'
2626
}
2727
```
2828

@@ -73,33 +73,24 @@ public class TextItemViewProvider
7373
}
7474
```
7575

76-
#### Step 3. You do not need to create another new class. Just `register` in your `Application` and add a `RecyclerView` and `List<TypeItem>` to your `Activity`, for example:
76+
#### Step 3. You do not need to create another new class. Just `register` your types and add a `RecyclerView` and `List<Item>` to your `Activity`, for example:
7777

7878
```java
79-
public class App extends Application {
80-
81-
@Override public void onCreate() {
82-
super.onCreate();
83-
MultiTypePool.register(TextItem.class, new TextItemViewProvider());
84-
MultiTypePool.register(ImageItem.class, new ImageItemViewProvider());
85-
MultiTypePool.register(RichItem.class, new RichItemViewProvider());
86-
MultiTypePool.register(Category.class, new CategoryItemViewProvider());
87-
MultiTypePool.register(PostRowItem.class, new PostRowItemViewProvider());
88-
MultiTypePool.register(PostList.class, new HorizontalItemViewProvider());
89-
}
90-
}
91-
```
79+
public class NormalActivity extends AppCompatActivity {
9280

93-
```java
94-
public class MainActivity extends AppCompatActivity {
81+
private MultiTypeAdapter adapter;
82+
private Items items;
9583

9684
@Override protected void onCreate(Bundle savedInstanceState) {
9785
super.onCreate(savedInstanceState);
9886
setContentView(R.layout.activity_main);
9987
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
10088

101-
/* Or you could use List<Item> */
102-
Items items = new Items();
89+
items = new Items();
90+
adapter = new MultiTypeAdapter(items);
91+
adapter.applyGlobalMultiTypePool();
92+
adapter.register(RichItem.class, new RichItemViewProvider());
93+
10394
TextItem textItem = new TextItem("world");
10495
ImageItem imageItem = new ImageItem(R.mipmap.ic_launcher);
10596
RichItem richItem = new RichItem("小艾大人赛高", R.mipmap.avatar);
@@ -110,7 +101,7 @@ public class MainActivity extends AppCompatActivity {
110101
items.add(richItem);
111102
}
112103

113-
recyclerView.setAdapter(new MultiTypeAdapter(items));
104+
recyclerView.setAdapter(adapter);
114105
}
115106
}
116107
```
@@ -127,36 +118,10 @@ And it has been used in [drakeet/TimeMachine](http://github.com/drakeet/TimeMach
127118

128119
[<img src="http://ww3.sinaimg.cn/large/86e2ff85gw1f55jnr2zjij20bx0bx0v3.jpg" width=256 height=256/>](http://github.com/drakeet/TimeMachine)
129120

130-
131-
## Performance testing
132-
133-
I found a Xiaomi 2s (Android 5.1.1, made in 2012) mobile phone for **testing the performance of global static MultiTypePool(#15)** today. I registered 9999 `ItemContent` classes & `ItemViewProvider` instances in the Application beginning. The `ItemContent` contains 12 random `String`s and the `ItemViewProvider.TestViewHolder` contains 12 `TextView`s, and I put my target type after 10000 index for test Adapter onCreateViewHolder's performance.
134-
135-
The results of this testing showed that:
136-
137-
![image](https://cloud.githubusercontent.com/assets/5214214/17643171/086e29b4-6194-11e6-9a08-d0ae19541346.png)
138-
139-
**The initialization of registering 10000 types just spend 10ms! And the memory usage is also very low** because `ItemViewProvider` instances do not hold any other class instance actually. And the `RecyclerView` which contains MultiType also perform perfectly and smoothly.
140-
141-
So, is there a application reach 10000 types? Do we really need a local type pool? The answer is obvious.
142-
143-
144121
## Change logs & Releases
145122

146123
https://github.com/drakeet/MultiType/releases
147124

148-
149-
## Q&A (English Version later)
150-
151-
**Q: 为什么使用静态或者全局类型池?(Why we need static and single TypePool?)**
152-
153-
A: 我不反对局部或临时类型池的设计,你可以 fork 这个项目自行实现,它们对于内存更加友好(但也只是微小优势而已),但在我看来,全局类型池在多方面更好:
154-
- 它能够**显式**连接 Type 和它的 Item View,能够在同一地方统一 register,这将避免分散,带来很好的直观性和可管理性;
155-
- 一个应用不会有超级大量的类型定义,类型 class 和 provider 对象都是非常轻薄的对象,直接静态存于内存,并不会导致内存泄漏和大的内存占用问题,几乎可以忽略;
156-
- 至于要不要支持 optional 的局部类型池参数,我也是不喜欢支持的,前面说了,这是没必要的,而且若是可选(optional)也会使用户疑惑:“到底要还是不要?”
157-
158-
因此我喜欢和坚持使用全局静态类型池,它不会带来什么问题,而且好处诸多,有人给我提交了使用反射的方法来自动获取类型连接,为了避免性能话题,我不喜欢反射,而且将类型连接变得复杂和不可见性未必是好事。我一直坚持的原则是:写简单的代码,写可读的代码,实现复杂的需求(你们看我的代码是不是感觉很自然而然而且可读性十分好?)
159-
160125
License
161126
-------
162127

Collapse file

‎library/build.gradle‎

Copy file name to clipboardExpand all lines: library/build.gradle
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ android {
2525
defaultConfig {
2626
minSdkVersion 9
2727
targetSdkVersion 24
28-
versionCode 22
29-
versionName "2.1.1"
28+
versionCode 30
29+
versionName "2.2.0-beta1"
3030
}
3131
buildTypes {
3232
release {
@@ -38,6 +38,6 @@ android {
3838

3939
dependencies {
4040
testCompile 'junit:junit:4.12'
41-
compile 'com.android.support:support-annotations:24.2.0'
42-
compile 'com.android.support:recyclerview-v7:24.2.0'
41+
compile 'com.android.support:support-annotations:24.2.1'
42+
compile 'com.android.support:recyclerview-v7:24.2.1'
4343
}
Collapse file

‎library/gradle.properties‎

Copy file name to clipboardExpand all lines: library/gradle.properties
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ POM_NAME=MultiType
3737
POM_ARTIFACT_ID=multitype
3838
POM_PACKAGING=aar
3939

40-
VERSION_NAME=2.1.1
41-
VERSION_CODE=22
40+
VERSION_NAME=2.2.0-beta1
41+
VERSION_CODE=30
4242
GROUP=me.drakeet.multitype
4343

4444
POM_DESCRIPTION=An Android library to retrofit multiple item view types
Collapse file

‎sample/build.gradle‎

Copy file name to clipboardExpand all lines: sample/build.gradle
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ android {
2323
applicationId "me.drakeet.multitype.sample"
2424
minSdkVersion 14
2525
targetSdkVersion 24
26-
versionCode 22
27-
versionName "2.1.1"
26+
versionCode 30
27+
versionName "2.2.0-beta1"
2828
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
2929
}
3030
buildTypes {
@@ -41,8 +41,8 @@ dependencies {
4141
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
4242
exclude group: 'com.android.support', module: 'support-annotations'
4343
})
44-
compile 'com.android.support:appcompat-v7:24.2.0'
45-
compile 'com.android.support:recyclerview-v7:24.2.0'
44+
compile 'com.android.support:appcompat-v7:24.2.1'
45+
compile 'com.android.support:recyclerview-v7:24.2.1'
4646
testCompile 'junit:junit:4.12'
4747
compile 'com.google.code.gson:gson:2.7'
4848
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.