RecycleView

目录

  • 一.RecycleView是什么
  • 二.玩玩RecycleView
    • 在build.gradle中配置
    • 在布局中添加
    • 添加一个实体类,模拟后台数据
    • 自定义Adapter,并设置条目点击监听
    • 通过LayoutManager设置现实效果
    • 设置分割线及添加头部和底部数据
    • 添加交互动画
    • 分页加载

一.RecycleView是什么

RecycleView是2014年谷歌大会推出的Android5.0Lollipop的一个架构,提供一个高度解耦,灵活性和高效率的体验,通过设置它提供不同的LayoutManager、ItemDecoration、ItemAnimator 可实现更加丰富多样的效果。但是RecyclerView也有缺点和 让人头疼的地方:设置列表的分割线时需要自定义,另外列表的点击事件需要自己去实现。

二.玩玩RecycleView

在build.gradle中配置

implementation ‘com.android.support:recyclerview-v7:28.0.0’

在布局中添加

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

添加一个实体类,模拟后台数据

public class Movie {

    public Movie(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
}

自定义Adapter,并设置条目点击监听

package com.seven.material.recycleview;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

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

/**
 * Time:2019/12/13
 * 

* Author:wangzhou *

* Description: */ public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MyHolder> { private Context context; private List<Movie> movies = new ArrayList<>(); private LayoutInflater layoutInflater; private List<Integer> heights; public boolean isStagged() { return stagged; } public void setStagged(boolean stagged) { this.stagged = stagged; } private boolean stagged; public MovieAdapter(List<Movie> movies, Context context) { this.movies = movies; this.context = context; layoutInflater = LayoutInflater.from(context); heights = new ArrayList<Integer>(); for (int i = 0; i < movies.size(); i++) { heights.add((int) Math.max(200,Math.random()*550)); } } @NonNull @Override public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { return new MyHolder(layoutInflater.inflate(R.layout.item_recy, viewGroup, false)); } @Override public void onBindViewHolder(@NonNull MyHolder myHolder, int position) { myHolder.textView.setText(movies.get(position).getName()); myHolder.itemView.setBackgroundColor(Color.rgb(100, (int) (Math.random() * 255), (int) (Math.random() * 255))); if(isStagged()){ ViewGroup.LayoutParams params = myHolder.itemView.getLayoutParams(); params.height = heights.get(position); myHolder.itemView.setLayoutParams(params); } myHolder.itemView.setOnClickListener(new MyClickListener(position)); } @Override public int getItemCount() { return movies == null ? 0 : movies.size(); } public void addItem(int i) { movies.add(i, new Movie("movie" + i)); notifyItemInserted(i); } public void removeItem(int i) { movies.remove(i); notifyItemRemoved(i); } class MyHolder extends RecyclerView.ViewHolder { TextView textView; public MyHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.txt); } } private ItemClickListener onItemClickListener; public void setItemClickListener(ItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public interface ItemClickListener { void onItemClick(View view, int position); } class MyClickListener implements View.OnClickListener { private int position; public MyClickListener(int position) { this.position = position; } @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(v, position); Toast.makeText(context, "movie" + position + "被点击了!", Toast.LENGTH_SHORT).show(); } } } }

通过LayoutManager设置现实效果

设置线性管理器

   RecyclerView.LayoutManager linearLayoutManager =
                        new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
                movieAdapter.setStagged(true);
                recyclerView.setLayoutManager(linearLayoutManager);

设置Grid管理器

    RecyclerView.LayoutManager gridLayoutManager =
                        new GridLayoutManager(MainActivity.this, 3, LinearLayoutManager.VERTICAL, false);
                movieAdapter.setStagged(true);
                recyclerView.setLayoutManager(gridLayoutManager);

设置瀑布流

     RecyclerView.LayoutManager staggedLayoutManager = new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL);
                movieAdapter.setStagged(true);
                recyclerView.setLayoutManager(staggedLayoutManager);

设置分割线及添加头部和底部数据

添加交互动画

分页加载

你可能感兴趣的:(#,MaterialDesign)