RecyclerView之使用FlexboxLayoutManager

前言

演示使用FlexboxLayoutManager給RecyclerView使用,關(guān)于FlexBoxLaytout的介紹可以參考FlexboxLayout的認(rèn)識(shí)與使用

第一步:item

演示一個(gè)普通的圖片,這里圖片選擇的是wrap_content,所以需要準(zhǔn)備幾張尺寸不同的照片!

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/iamgeView_flex_box_test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:layout_margin="1dp"
    android:contentDescription="@string/app_name"
    />

第二步 :ViewHolder

public class PictureViewHolder extends RecyclerView.ViewHolder {

    public PictureViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.iamgeView_flex_box_test);
    }

    public void bindTo(@DrawableRes int drawable){
        imageView.setImageResource(drawable);
        ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
   
    }
    private ImageView imageView ;

}

第三步:適配器

為了演示采用的是普通的圖片,圖片來(lái)自官方Demo

public class PictureAdapter extends RecyclerView.Adapter<PictureViewHolder> {

    private int [] imgJiHe =
            {R.drawable.aslkdjf
            ,R.drawable.cat_2
            ,R.drawable.cat_2
            ,R.drawable.cat_3
            ,R.drawable.cat_4
            ,R.drawable.cat_5
            ,R.drawable.cat_6
            ,R.drawable.cat_7
            ,R.drawable.cat_8
            ,R.drawable.cat_9
            ,R.drawable.cat_10
            ,R.drawable.cat_11
            ,R.drawable.cat_12
            ,R.drawable.cat_13
            ,R.drawable.cat_14
            ,R.drawable.cat_15
            ,R.drawable.cat_16
            ,R.drawable.cat_17
            ,R.drawable.cat_18
            ,R.drawable.cat_19};



    @NonNull
    @Override
    public PictureViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_cat_test,viewGroup,false);

        return new PictureViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PictureViewHolder pictureViewHolder, int i) {
          int postiont = i%imgJiHe.length;
          pictureViewHolder.bindTo(imgJiHe[postiont]);
    }

    @Override
    public int getItemCount() {
        return imgJiHe.length * 4;
    }
}

第四步,設(shè)置給ReyclerView

       mRecyclerView = findViewById(R.id.recyclerView_test);
        //設(shè)置LayoutManager
        FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(this);
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);
  //確定主軸與換行方式,這樣的方式類(lèi)似于一般的ListView的樣式
        mRecyclerView.setLayoutManager(flexboxLayoutManager);

        PictureAdapter pictureAdapter = new PictureAdapter();
        mRecyclerView.setAdapter(pictureAdapter);

關(guān)于上面屬性為啥這樣設(shè)置可以參考文章開(kāi)頭,去了解一個(gè)FlexBoxLayout的使用

運(yùn)行結(jié)果

測(cè)試結(jié)果

發(fā)現(xiàn)確實(shí)出現(xiàn)了FlexBoxLayout的特性,自動(dòng)換行的特性?。?!

修改

修改Adapter

public class PictureViewHolder extends RecyclerView.ViewHolder {

    public PictureViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.iamgeView_flex_box_test);
    }

    public void bindTo(@DrawableRes int drawable){
        imageView.setImageResource(drawable);
        ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
        if (layoutParams instanceof FlexboxLayoutManager.LayoutParams){
               ((FlexboxLayoutManager.LayoutParams) layoutParams).setFlexGrow(1f);
         //加上這句話(huà),保證主軸上的子view均分主軸剩余空間
       //完全可以給網(wǎng)絡(luò)數(shù)據(jù)添加type,在這里手動(dòng)設(shè)置同一主軸上子View占據(jù)的空間
        }
    }

    
    private ImageView imageView ;


}
222.jpg

可以看出這樣運(yùn)行的效果就比較不錯(cuò)了~
實(shí)現(xiàn)這個(gè)效果的關(guān)鍵是寬度wrap ,選擇不同寬度的子View,這樣才能最大程度的體現(xiàn)FlexBoxLayout的特性?。?!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基本用法 想要使用這個(gè)控件,首先在項(xiàng)目的build.gradle中添加相應(yīng)的依賴(lài)庫(kù)才行打開(kāi)app/build.gr...
    何懼l閱讀 1,611評(píng)論 0 0
  • 一、概述 ItemTouchHelper在RecyclerView的整個(gè)體系中,負(fù)責(zé)監(jiān)聽(tīng)I(yíng)tem的手勢(shì)操作,我們通...
    澤毛閱讀 3,596評(píng)論 2 25
  • Day1: 在代碼中通過(guò)R.string.hello_world可以獲得該字符串的引用; 在XML中通過(guò)@stri...
    冰凝雪國(guó)閱讀 1,651評(píng)論 0 5
  • 也許是因?yàn)槲疑類(lèi)?ài)了一個(gè)人 思念總在夜里洗刷靈魂 才總讓我這般 人不像人 也許是因?yàn)槲疑類(lèi)?ài)了一個(gè)人 幻想總在每個(gè)荒蕪...
    文星星閱讀 138評(píng)論 0 0
  • 轉(zhuǎn)眼,又一周過(guò)去了,期中考試也離我們?cè)絹?lái)越近了。 一周開(kāi)始,我們學(xué)校就開(kāi)始了足球賽,可惜的是第一輪就被刷了下來(lái),主...
    機(jī)械181陳閱讀 180評(píng)論 0 1

友情鏈接更多精彩內(nèi)容