DeYi 技术博客

总结&记录


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

Happy New Year 2019

发表于 2019-01-01

Cheer Up

server down

git command reference

发表于 2018-03-15 | 分类于 工程管理 , git

Android Studio File Templates for RecyclerView Adapter

发表于 2018-03-10 | 分类于 编程 , android , android studio , file templates

参考资料

LiveTemplate for RecyclerView Adapter

操作步骤

1
Settings | Editor | File and Code Templates

添加模板文件,命名为 RecyclerView Adapter

模板代码

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
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;

#parse("File Header.java")
public class ${NAME} extends
RecyclerView.Adapter<${NAME}.ItemViewHolder> {

private Context context;
private List<${ITEM_TYPE}> dataSet;
private View.OnClickListener onItemClickListener;

${NAME}(Context context, View.OnClickListener onItemClickListener) {
this.context = context;
this.onItemClickListener = onItemClickListener;
}

${ITEM_TYPE} getItemData(int position) {
return dataSet.get(position);
}

void setDataSet(List<${ITEM_TYPE}> dataSet) {
this.dataSet = dataSet;
notifyDataSetChanged();
}

@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ItemViewHolder(LayoutInflater.from(context), parent);
}

@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
${ITEM_TYPE} item = dataSet.get(position);
holder.bind(item);
}

@Override
public int getItemCount() {
return dataSet == null ? 0 : dataSet.size();
}

class ItemViewHolder extends RecyclerView.ViewHolder {
ItemViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.${item_layout}, parent, false));
}

void bind(${ITEM_TYPE} model) {
itemView.setOnClickListener(onItemClickListener);
}
}
}

Gradle 常用代码片段

发表于 2018-03-10 | 分类于 编程 , android , android studio , gradle

abiFilters

NdkOptions - abiFilters

1
2
3
4
5
6
7
8
9
10
11
android {
// Similar to other properties in the defaultConfig block, you can override
// these properties for each product flavor in your build configuration.
defaultConfig {
ndk {
// Tells Gradle to build outputs for the following ABIs and package
// them into your APK.
abiFilters 'x86', 'x86_64', 'armeabi'
}
}
}

另外一种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
productFlavors {  
flavor1 {
ndk {
abiFilters "armeabi-v7a"
}
}
flavor2 {
ndk {
abiFilters "armeabi-v7a"
abiFilters "x86"
abiFilters "armeabi"
abiFilters "arm64-v8a"
abiFilters "x86_64"
}
}
}

Android Studio 自定义向导模板

发表于 2018-03-07 | 分类于 编程 , android , android studio , file templates

资料

jetbrains官方文档

Demo

创建 RxMvp 文件向导

使用步骤

  1. 在指定的包上右击,弹出菜单里,批到新建向导
    android studio file templates
  2. 输入新建类名的前缀
    android studio file templates
  3. 查看向导生成的类文件
    android studio file templates

创建步骤

在 android studio 安装目录找到文件夹:
{Android Studio installation dir}\plugins\android\lib\templates\other

依次创建以下文件:

  1. template.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <template
    format="4"
    revision="1"
    name="Rx MPV"
    description="Creates new RxMVP classes - Presenter, Model, Bean and Contract">

    <category value="Other"/>

    <parameter
    id="prefix"
    name="Prefix of Class Name"
    type="string"
    constraints="class|unique|nonempty"
    default="Mvp"
    help="The prefix of class name"/>

    <globals file="globals.xml.ftl" />
    <execute file="recipe.xml.ftl" />
    </template>
  2. recipe.xml.ftl

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <?xml version="1.0"?>
    <recipe>
    <instantiate
    from="src/app_package/Contract.java.ftl"
    to="${escapeXmlAttribute(srcOut)}/${prefix}Contract.java" />
    <instantiate
    from="src/app_package/Presenter.java.ftl"
    to="${escapeXmlAttribute(srcOut)}/${prefix}Presenter.java" />
    <instantiate
    from="src/app_package/Model.java.ftl"
    to="${escapeXmlAttribute(srcOut)}/${prefix}Model.java" />
    <instantiate
    from="src/app_package/Bean.java.ftl"
    to="${escapeXmlAttribute(srcOut)}/${prefix}Bean.java" />
    <open file="${srcOut}/${prefix}Contract.java"/>
    </recipe>
  3. globals.xml.ftl

    1
    2
    3
    4
    5
    <?xml version="1.0"?>
    <globals>
    <global id="resOut" value="${resDir}" />
    <global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}" />
    </globals>
  4. root/src/app_package/Contract.java.ftl

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package ${packageName};

    public interface ${prefix}Contract{
    interface Model {
    }

    interface View {
    }

    interface Presenter {
    }
    }
  5. root/src/app_package/Model.java.ftl

    1
    2
    3
    4
    package ${packageName};

    public class ${prefix}Model implements ${prefix}Contract.Model {
    }
  6. root/src/app_package/Presenster.java.ftl

    ${packageName};
    1
    2
    3
    4
    5
    6
    7

    import work.wangxiang.android.common.PresenterBase;

    public class ${prefix}Presenter
    extends PresenterBase<${prefix}Contract.Model, ${prefix}Contract.View>
    implements ${prefix}Contract.Presenter {
    }
  7. root/src/app_package/Bean.java.ftl

    1
    2
    3
    4
    package ${packageName};

    public class ${prefix}Bean {
    }

创建完成后,重新启动 android studio,就可使用新建向导了。

遇到的问题

  1. template.xml 不要加 .ftl 后缀
  2. app_package 目录名中,是下划线,不是中划线

MediaPlayer.preparAsync-no-call-back

发表于 2018-02-08 | 分类于 编程 , android , android sdk

com.meituan.android.walle-example

发表于 2017-12-01 | 分类于 编程 , android , 3rdParty , com.meituan.android.walle

打渠道包示例

美团打包

project build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
classpath 'com.meituan.android.walle:plugin:1.1.5'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

app/build.gradle

1
2
3
4
5
6
7
8
9
compile 'com.meituan.android.walle:library:1.1.5'  依赖库
walle {
// 指定渠道包的输出路径
apkOutputFolder = new File("${project.buildDir}/outputs/channels");
// 定制渠道包的APK的文件名称
apkFileNameFormat = 'youle_v${versionName}_${channel}_${buildType}_${buildTime}.apk';
// 渠道配置文件
configFile = new File("${project.getProjectDir()}/config.json")
}

app/config.json

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
{
"channelInfoList": [
{
"channel": "caiyi",
"alias": "采邑",
"extraInfo": {
"aliChannel": "701172"
}
},
{
"channel": "yingyongbao",
"alias": "应用宝",
"extraInfo": {
"aliChannel": "10005890"
}
},
{
"channel": "_360",
"alias": "360",
"extraInfo": {
"aliChannel": "10005884"
}
},
{
"channel": "baidu",
"alias": "百度",
"extraInfo": {
"aliChannel": "10005882"
}
},
{
"channel": "xiaomi",
"alias": "小米",
"extraInfo": {
"aliChannel": "700159"
}
},
{
"channel": "huawei",
"alias": "华为",
"extraInfo": {
"aliChannel": "10002089"
}
},
{
"channel": "samsung",
"alias": "三星",
"extraInfo": {
"aliChannel": "600153"
}
},
{
"channel": "lenovo",
"alias": "联想",
"extraInfo": {
"aliChannel": "203200"
}
},
{
"channel": "meizu",
"alias": "魅族",
"extraInfo": {
"aliChannel": "702003"
}
},
{
"channel": "oppo",
"alias": "oppo",
"extraInfo": {
"aliChannel": "263200"
}
},
{
"channel": "vivo",
"alias": "vivo",
"extraInfo": {
"aliChannel": "10003993"
}
},
{
"channel": "leshi",
"alias": "乐视",
"extraInfo": {
"aliChannel": "10002912"
}
},
{
"channel": "alipp",
"alias": "阿里PP",
"extraInfo": {
"aliChannel": "10005894"
}
},
{
"channel": "yy001",
"alias": "yy001",
"extraInfo": {
"aliChannel": "10006813"
}
},
{
"channel": "yy002",
"alias": "yy002",
"extraInfo": {
"aliChannel": "10006814"
}
},
{
"channel": "yy003",
"alias": "yy003",
"extraInfo": {
"aliChannel": "10006815"
}
},
{
"channel": "yy004",
"alias": "yy004",
"extraInfo": {
"aliChannel": "10006816"
}
},
{
"channel": "yy005",
"alias": "yy005",
"extraInfo": {
"aliChannel": "10006817"
}
},
{
"channel": "yy006",
"alias": "yy006",
"extraInfo": {
"aliChannel": "10006818"
}
},
{
"channel": "yy007",
"alias": "yy007",
"extraInfo": {
"aliChannel": "10006819"
}
},
{
"channel": "yy008",
"alias": "yy008",
"extraInfo": {
"aliChannel": "10006820"
}
},
{
"channel": "yy009",
"alias": "yy009",
"extraInfo": {
"aliChannel": "10006821"
}
},
{
"channel": "yy010",
"alias": "yy010",
"extraInfo": {
"aliChannel": "10006822"
}
},
{
"channel": "yy011",
"alias": "yy011",
"extraInfo": {
"aliChannel": "10006823"
}
},
{
"channel": "yy012",
"alias": "yy012",
"extraInfo": {
"aliChannel": "10006824"
}
},
{
"channel": "yy013",
"alias": "yy013",
"extraInfo": {
"aliChannel": "10006825"
}
},
{
"channel": "yy014",
"alias": "yy014",
"extraInfo": {
"aliChannel": "10006826"
}
},
{
"channel": "yy015",
"alias": "yy015",
"extraInfo": {
"aliChannel": "10006827"
}
},
{
"channel": "julangdsp",
"alias": "julangdsp",
"extraInfo": {
"aliChannel": "10006796"
}
},
{
"channel": "100001",
"alias": "100001",
"extraInfo": {
"aliChannel": "10006874"
}
},
{
"channel": "100002",
"alias": "100002",
"extraInfo": {
"aliChannel": "10006875"
}
},
{
"channel": "100003",
"alias": "100003",
"extraInfo": {
"aliChannel": "10006876"
}
},
{
"channel": "100004",
"alias": "100004",
"extraInfo": {
"aliChannel": "10006877"
}
},
{
"channel": "100005",
"alias": "100005",
"extraInfo": {
"aliChannel": "10006878"
}
},
{
"channel": "100006",
"alias": "100006",
"extraInfo": {
"aliChannel": "10006879"
}
},
{
"channel": "100007",
"alias": "100007",
"extraInfo": {
"aliChannel": "10006880"
}
},
{
"channel": "100008",
"alias": "100008",
"extraInfo": {
"aliChannel": "10006881"
}
},
{
"channel": "100009",
"alias": "100009",
"extraInfo": {
"aliChannel": "10006882"
}
},
{
"channel": "100010",
"alias": "100010",
"extraInfo": {
"aliChannel": "10006883"
}
}
]
}

RecyclerView Position 概念

发表于 2017-12-01 | 分类于 编程 , android , android sdk , android.support.v7.widget , RecyclerView

官方解释

RecyclerView 官方文档

Positions in RecyclerView:
RecyclerView introduces an additional level of abstraction between the RecyclerView.Adapter and RecyclerView.LayoutManager to be able to detect data set changes in batches during a layout calculation(计算). This saves LayoutManager from tracking adapter changes to calculate animations. It also helps with performance because all view bindings happen at the same time and unnecessary bindings are avoided.

For this reason, there are two types of position related methods in RecyclerView:

  1. layout position: Position of an item in the latest layout calculation. This is the position from the LayoutManager’s perspective.
  2. adapter position: Position of an item in the adapter. This is the position from the Adapter’s perspective.

These two positions are the same except the time between dispatching adapter.notify* events and calculating the updated layout.

Methods that return or receive LayoutPosition use position as of the latest layout calculation (e.g. getLayoutPosition(), findViewHolderForLayoutPosition(int)). These positions include all changes until the last layout calculation. You can rely on these positions to be consistent with what user is currently seeing on the screen. For example, if you have a list of items on the screen and user asks for the 5th element, you should use these methods as they’ll match what user is seeing.

The other set of position related methods are in the form of AdapterPosition. (e.g. getAdapterPosition(), findViewHolderForAdapterPosition(int)) You should use these methods when you need to work with up-to-date adapter positions even if they may not have been reflected to layout yet. For example, if you want to access the item in the adapter on a ViewHolder click, you should use getAdapterPosition(). Beware that these methods may not be able to calculate adapter positions if notifyDataSetChanged() has been called and new layout has not yet been calculated. For this reasons, you should carefully handle NO_POSITION or null results from these methods.

When writing a RecyclerView.LayoutManager you almost always want to use layout positions whereas when writing an RecyclerView.Adapter, you probably want to use adapter positions

问题一: lint-error-do-not-treat-position-as-fixed-only-use-immediately

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

D:\work\videoshare_android.git\app\src\main\java\com\caiyi\youle\camera\VideoEffectListAdapter.java

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
EffectData data = dataSets.get(position);
if (data.getCoverPic() == null) {
holder.idIndexItemImage.setImageResource(data.getImageID());
} else {
Glide.with(context)
.load(data.getCoverPic())
.placeholder(R.drawable.ic_filter1)
.into(holder.idIndexItemImage);
}
holder.idIndexItemText.setText(data.getText());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(holder, position);
select(position);
}
});

if (data.isSelected()) {
holder.itemView.setSelected(true);
} else {
holder.itemView.setSelected(false);
}
}

Do not treat position as fixed; only use immediately and call `holder.getAdapterPosition()` to look it up later

lint-error-do-not-treat-position-as-fixed-only-use-immediately

recyclerview-adapter-lint-error-do-not-treat-position-as-fixed

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
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
EffectData data = dataSets.get(position);
if (data.getCoverPic() == null) {
holder.idIndexItemImage.setImageResource(data.getImageID());
} else {
Glide.with(context)
.load(data.getCoverPic())
.placeholder(R.drawable.ic_filter1)
.into(holder.idIndexItemImage);
}
holder.idIndexItemText.setText(data.getText());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getAdapterPosition();
mOnItemClickListener.onItemClick(holder, pos);
select(pos);
}
});

if (data.isSelected()) {
holder.itemView.setSelected(true);
} else {
holder.itemView.setSelected(false);
}
}

json-fomat-tools

发表于 2017-11-07 | 分类于 编程 , json

在线工具

JSON TOOLS

  • jsonviewer 格式化
  • jsonvalidator
  • online-json-editor

android.support.v7.widget.RecyclerView

发表于 2017-10-20 | 分类于 编程 , android , android sdk , android.support.v7.widget , RecyclerView

RecyclerView 官方文档

RecyclerView 的用途

A flexible view for providing a limited window into a large data set.

继承关系 及 派生类

1
2
3
4
5
6
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.support.v7.widget.RecyclerView
派生类
HorizontalGridView, VerticalGridView

基本概念

  • Adapter:A subclass of RecyclerView.Adapter responsible for providing views that represent items in a data set.
  • Position:The position of a data item within an Adapter.
  • Index: The index of an attached child view as used in a call to getChildAt(int). Contrast with(与...截然不同的) Position.
  • Binding: The process of preparing a child view to display data corresponding to a position within the adapter.
  • Recycle (view): A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction.
  • Scrap(废料,小片,残余物) (view): A child view that has entered into a temporarily detached state during layout. Scrap views may be reused without becoming fully detached from the parent RecyclerView, either unmodified if no rebinding is required or modified by the adapter if the view was considered dirty.
  • Dirty (view): A child view that must be rebound by the adapter before being displayed.

    position 概念解释

    Positions in RecyclerView:
    RecyclerView introduces an additional level of abstraction between the RecyclerView.Adapter and RecyclerView.LayoutManager to be able to detect data set changes in batches during a layout calculation(计算). This saves LayoutManager from tracking adapter changes to calculate animations. It also helps with performance because all view bindings happen at the same time and unnecessary bindings are avoided.

For this reason, there are two types of position related methods in RecyclerView:

  1. layout position: Position of an item in the latest layout calculation. This is the position from the LayoutManager’s perspective.
  2. adapter position: Position of an item in the adapter. This is the position from the Adapter’s perspective.

These two positions are the same except the time between dispatching adapter.notify* events and calculating the updated layout.

Methods that return or receive LayoutPosition use position as of the latest layout calculation (e.g. getLayoutPosition(), findViewHolderForLayoutPosition(int)). These positions include all changes until the last layout calculation. You can rely on these positions to be consistent with what user is currently seeing on the screen. For example, if you have a list of items on the screen and user asks for the 5th element, you should use these methods as they’ll match what user is seeing.

The other set of position related methods are in the form of AdapterPosition. (e.g. getAdapterPosition(), findViewHolderForAdapterPosition(int)) You should use these methods when you need to work with up-to-date adapter positions even if they may not have been reflected to layout yet. For example, if you want to access the item in the adapter on a ViewHolder click, you should use getAdapterPosition(). Beware that these methods may not be able to calculate adapter positions if notifyDataSetChanged() has been called and new layout has not yet been calculated. For this reasons, you should carefully handle NO_POSITION or null results from these methods.

When writing a RecyclerView.LayoutManager you almost always want to use layout positions whereas when writing an RecyclerView.Adapter, you probably want to use adapter positions.

使用中的问题

1
2
3
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter
这两个方法,必须都调用了,才能显示。
12…6

wxiang

音视频、Android、C/C++

54 日志
37 分类
43 标签
© 2019 wxiang
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.2