今天在做 Heaven 項(xiàng)目時(shí)遇到的一個(gè)問題钠四,在網(wǎng)上搜了一下,參考文章很少只提供了下面這個(gè)在 ViewHolder 中重新設(shè)置 Item 的寬高的方法跪楞,一般在 Item 的根布局中會(huì)設(shè)置marginTop 或者 marginBottom缀去,因此不能解決這個(gè)問題,就寫下來給大家一個(gè)解決方案甸祭。
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View itemView) {
super(itemView);
}
public void setVisibility(boolean visible) {
RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) itemView.getLayoutParams();
if (visible) {
param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
param.width = LinearLayout.LayoutParams.MATCH_PARENT;
itemView.setVisibility(View.VISIBLE);
} else {
itemView.setVisibility(View.GONE);
param.height = 0;
param.width = 0;
}
itemView.setLayoutParams(param);
}
}
如果 Item 根布局中有使用 marginTop 或者 layout_marginBottom缕碎,還是會(huì)存在間隔;
如果要使用 marginTop 或者 layout_marginBottom, 需要修改為 paddingTop / paddingBottom池户。
Heaven 項(xiàng)目是將開源項(xiàng)目 CloudReader 從 Retrofit + RxJava + MVVM + databinding 模式改成 MVP+Dagger2+Retrofit+RxJava 架構(gòu)的項(xiàng)目
??Heaven 項(xiàng)目已上傳至 github:https://github.com/chengHeaven/Heaven
??該項(xiàng)目的 MVP 模式與其他的 MVP 模式有些不一樣的地方咏雌,稍后幾天會(huì)將該項(xiàng)目詳細(xì)思路寫下來,歡迎大家關(guān)注我校焦,以便能及時(shí)看到文章赊抖。
??如果覺得這篇文章對(duì)你有幫助,希望各位能夠點(diǎn)一個(gè)關(guān)注或者喜歡寨典。
關(guān)于我:
- github : chengHeaven
- 簡(jiǎn)書: 月色丶 Heaven
- 郵箱: chen464994292@163.com / heavencheng1916@gmail.com