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

有使用RecycleView開發(fā)的人都知道RecycleVIew的功能很豐富,幾乎能完全替代ListVIew仆嗦,不過在使用過程中有時會有點麻煩,例如我只想弄個垂直的列表或者水平的列表布局垃僚,然后在代碼中寫好RecycleView之后,還得要繼續(xù)在代碼中寫一下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"
就是水平樣式。

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

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末掠河,一起剝皮案震驚了整個濱河市亮元,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌唠摹,老刑警劉巖爆捞,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異勾拉,居然都是意外死亡煮甥,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進店門藕赞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來成肘,“玉大人,你說我怎么就攤上這事斧蜕∷簦” “怎么了?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵批销,是天一觀的道長洒闸。 經(jīng)常有香客問我,道長风钻,這世上最難降的妖魔是什么顷蟀? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮骡技,結果婚禮上鸣个,老公的妹妹穿的比我還像新娘羞反。我一直安慰自己,他們只是感情好囤萤,可當我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布昼窗。 她就那樣靜靜地躺著,像睡著了一般涛舍。 火紅的嫁衣襯著肌膚如雪澄惊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天富雅,我揣著相機與錄音掸驱,去河邊找鬼。 笑死没佑,一個胖子當著我的面吹牛毕贼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蛤奢,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼鬼癣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了啤贩?” 一聲冷哼從身側(cè)響起待秃,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎痹屹,沒想到半個月后章郁,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡志衍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年驱犹,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片足画。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖佃牛,靈堂內(nèi)的尸體忽然破棺而出淹辞,到底是詐尸還是另有隱情,我是刑警寧澤俘侠,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布象缀,位于F島的核電站,受9級特大地震影響爷速,放射性物質(zhì)發(fā)生泄漏央星。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一惫东、第九天 我趴在偏房一處隱蔽的房頂上張望莉给。 院中可真熱鬧毙石,春花似錦、人聲如沸颓遏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽叁幢。三九已至滤灯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間曼玩,已是汗流浹背鳞骤。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留黍判,地道東北人豫尽。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像样悟,于是被迫代替她去往敵國和親拂募。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容