欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android开发 RecyclerView 瀑布列表布局

程序员文章站 2022-03-13 17:05:11
...

1.写一个内容的自定义小布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">
    <ImageView
        android:id="@+id/waterfall_Image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ace"/>
    <TextView
        android:id="@+id/waterfall_Name_TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里显示名称"
        android:textSize="15sp"
        android:textColor="@color/colorBlack"
        android:layout_gravity="center_horizontal"/>
    <TextView
        android:id="@+id/waterfall_contents_TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里显示内容"
        android:textSize="10sp"
        android:textColor="@color/colorblue"/>

</LinearLayout>



布局预览:

android开发 RecyclerView 瀑布列表布局

写一个内容适配器class:

package com.example.lenovo.myrecyclerview.RecyclerViewToolkit;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.lenovo.myrecyclerview.R;

import java.util.List;

/**
 * Created by lenovo on 2018/5/2.
 */

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
    private List<ListData> mDataList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageAvatar;
        TextView nameText;
        TextView contentsText;
        public ViewHolder(View itemView) {
            super(itemView);
            //注意这里可能需要import com.example.lenovo.myrecyclerview.R; 才能使用R.id
            imageAvatar = (ImageView)itemView.findViewById(R.id.waterfall_Image);
            nameText =(TextView) itemView.findViewById(R.id.waterfall_Name_TextView);
            contentsText = (TextView)itemView.findViewById(R.id.waterfall_contents_TextView);
        }
    }
    public  ListAdapter(List<ListData> listDatas){
        mDataList = listDatas;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.waterfall_list_view,
                parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ListData listData = mDataList.get(position);
        holder.imageAvatar.setImageResource(listData.getImageView());
        holder.nameText.setText(listData.getNameText());
        holder.contentsText.setText(listData.getContentsText());
    }

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


写一个RecyclerView 瀑布列表布局活动class:



package com.example.lenovo.myrecyclerview;

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.lenovo.myrecyclerview.RecyclerViewToolkit.ListAdapter;
import com.example.lenovo.myrecyclerview.RecyclerViewToolkit.ListData;

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

public class RecyclerViewActivity extends AppCompatActivity {
    private List<ListData> listDatas = new ArrayList<>(); //创建数据list

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        addingData(); //导入数据
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.RecyclerView);
        //在此处修改布局排列方向
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        ListAdapter listAdapter = new ListAdapter(listDatas); //创建适配器,并且导入数据list
        recyclerView.setAdapter(listAdapter);//布局导入适配器
    }
    //添加数据
    public void addingData(){
        ListData ace = new ListData(R.drawable.ace,"ace", "波特卡斯·D·艾斯:" +
                "烧烧果实的前任能力者,绰号“火拳”,实力强大。");
        listDatas.add(ace);
//以下省略.....

    }

}

实现效果图:

android开发 RecyclerView 瀑布列表布局

相关标签: android