RecyclerView 瀑布流布局


最后一個布局樣式是瀑布流的布局,其實和網格布局幾乎一樣的,網格布局是規規矩矩的,而瀑布流就是有長有短的那種,有錯位和落差感,有時候太規矩的不好看,有一點錯位顯得更加美觀。

? ? 瀑布流的?RecyclerView Item 布局文件要注意了,不能寫固定的一個高度,否則就沒有效果了。比如,我們得這樣改:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="4dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">

<ImageView
android:id="@+id/img_recy_item_3_pic"
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/tv_recy_item_3_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_recy_item_3_pic"
android:layout_centerInParent="true"
android:layout_marginTop="8dp"
android:textSize="16sp" />

</RelativeLayout>
</android.support.v7.widget.CardView>
? ? 注意上面的代碼,cardview 的高度不能固定,以及下面的 textview 高度也都不能固定值,都要寫為 wrap_content,適配器就不需要修改了,要改的地方就是數據格式還有?RecyclerView 的布局管理樣式。

我們添加數據要改為這樣,名稱有長的有短的,才能形成長短不一的瀑布流的形式。

private void addStaggeredData() {
Map<String, Object> map = null;
Random random = new Random();
String[] str = {
"瀑布流\n",
"瀑布流\n瀑布流\n",
"瀑布流\n瀑布流\n瀑布流\n",
"瀑布流\n瀑布流\n瀑布流\n瀑布流\n",
};

for (int i = 0; i < 30; i++) {
int n = random.nextInt(pics.length);
map = new HashMap<>();
map.put("pic", pics[n]);
map.put("name", names[n] + "\n" + str[random.nextInt(str.length)]);
staggeredData.add(map);
}
}
---------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM