可直接在布局XML中設置LayoutManager樣式的RecycleView

有使用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"
就是水平樣式。

所以只需將代碼復制粘貼到您的工程中可直接使用,這樣減少的代碼重復,方便使用。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容