android中RecyclerView控件實現瀑布流布局


本文是在之前文章的基礎上做的修改:android中RecyclerView控件的使用

1、修改列表項news_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="400dp">

    <ImageView
        android:id="@+id/newsPic"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:scaleType="fitCenter"
        android:src="@drawable/o1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/newsTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="TextView"
        app:layout_constraintTop_toBottomOf="@+id/newsPic" />
</android.support.constraint.ConstraintLayout>

 

2、修改NewsAdapter.java類,注意紅色的為修改內容,這里主要是修改了新聞圖片的高度,改成了隨機高度,讓每一項的高度不一樣:

package com.example.chenrui.app1;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.chenrui.common.News;

import java.util.List;
import java.util.Random;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

    private List<News> newsList;

    public NewsAdapter(List<News> newsList) {
        this.newsList = newsList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item1,viewGroup,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        News news = newsList.get(i);
        viewHolder.newsImage.setImageResource(news.getPic());

 ViewGroup.LayoutParams params = viewHolder.newsImage.getLayoutParams();
        params.height = params.height + new Random().nextInt(300); viewHolder.newsImage.setLayoutParams(params);

        viewHolder.newsTitle.setText(news.getTitle());
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView newsImage;
        TextView newsTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            newsImage = itemView.findViewById(R.id.newsPic);
            newsTitle = itemView.findViewById(R.id.newsTitle);
        }
    }
}

 

3、修改MainActivity.java類,注意紅色修改的內容,其他內容都沒有變:

package com.example.chenrui.app1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.example.chenrui.common.News;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

 List<News> newsList = new ArrayList();
        newsList.add(new News("新聞標題1",R.drawable.img01));
        newsList.add(new News("新聞標題2",R.drawable.img02));
        newsList.add(new News("新聞標題3",R.drawable.img01));
        newsList.add(new News("新聞標題4",R.drawable.img02));
        newsList.add(new News("新聞標題5",R.drawable.img01));
        newsList.add(new News("新聞標題6",R.drawable.img02));
        newsList.add(new News("新聞標題7",R.drawable.img01));
        newsList.add(new News("新聞標題8",R.drawable.img02));
        newsList.add(new News("新聞標題9",R.drawable.img01));
        newsList.add(new News("新聞標題10",R.drawable.img02));
        NewsAdapter newsAdapter = new NewsAdapter(newsList);

        RecyclerView view = findViewById(R.id.list1);
 StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL); view.setLayoutManager(layoutManager);
        view.setAdapter(newsAdapter);
    }
}

 

最終顯示效果:

 


免責聲明!

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



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