有使用RecycleView开发的人都知道RecycleVIew的功能很丰富,几乎能完全替代ListVIew,不过在使用过程中有时会有点麻烦,例如我只想弄个垂直的列表或者水平的列表布局,然后在代码中写好RecycleView之后,还得要继续在代码中写一下LayoutManage的样式,这样每次在使用RecycleView的界面上会有重复的布局样式代码。
为了解决这个问题,我就写了个自定义的RecycleView,可以直接在布局的XML中设定布局样式,废话不多说直接上代码
Java的代码如下。
public class CustomRecyclerView extends RecyclerView {
private int layoutStyle;
private int spanCount;
private int divideLine;
public CustomRecyclerView(Context context) {
super(context);
}
public CustomRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomRecyclerView);
layoutStyle = typedArray.getInt(R.styleable.CustomRecyclerView_layout_style,0);
divideLine = typedArray.getInt(R.styleable.CustomRecyclerView_divide_line_style,0);
spanCount = typedArray.getInteger(R.styleable.CustomRecyclerView_spanCount, 2);
typedArray.recycle();
setLayoutStyle(layoutStyle);
setDivideLineStyle(divideLine);
}
private void setDivideLineStyle(int divideLine) {
if (divideLine != 0){
int orientation ;
if (divideLine == 1){
orientation = LinearLayoutManager.VERTICAL;
}else {
orientation = LinearLayoutManager.HORIZONTAL;
}
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(), orientation);
addItemDecoration(dividerItemDecoration);
}
}
public void setLayoutStyle(int layoutStyle){
LayoutManager layoutManager;
switch (layoutStyle){
case 0:
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
break;
case 2:
layoutManager = new GridLayoutManager(getContext(),spanCount, GridLayoutManager.VERTICAL, false);
break;
case 3:
layoutManager = new GridLayoutManager(getContext(),spanCount, GridLayoutManager.HORIZONTAL, false);
break;
case 1:
default: //默认的就是列表模式
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
break;
}
setLayoutManager(layoutManager);
}
}
然后在你的values文件中的attr.xml添加如下的属性:
<declare-styleable name="CustomRecyclerView">
<attr name="spanCount" format="integer"></attr>
<attr name="divide_line_style" format="enum">
<enum name="none" value="0"></enum>
<enum name="horizontal" value="1"></enum>
<enum name="vertical" value="2"></enum>
</attr>
<attr name="layout_style" format="enum">
<enum name="horizontal" value="0"></enum>
<enum name="vertical" value="1"></enum>
<enum name="grid_vertical" value="2"></enum>
<enum name="grid_horizontal" value="3"></enum>
<enum name="other" value="9"></enum>
</attr>
</declare-styleable>
最后只需要在你的布局中使用上文的RecycleView布局然后设置好属性即可。
<com.app.demo.CustomRecyclerView
android:id="@+id/demo_RecycleView"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/audio_root_bottom_divider"
android:layout_below="@id/audio_root_top_title_layout"
app:layout_style="vertical"
/>
注意:上文中的
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_style="vertical"
就是设置当前的排列样式,
app:layout_style="horizontal"
就是水平样式。
所以只需将代码复制粘贴到您的工程中可直接使用,这样减少的代码重复,方便使用。
网友评论