1. CardView

CardView是用于实现卡片式布局效果的重要控件,实际上也是一个frameLayout,只是额外提供了圆角和阴影,看上去有立体效果。

效果如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardViewxmlns: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="5dp"app:cardCornerRadius="4dp"><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/fruit_image"android:layout_width="match_parent"android:layout_height="100dp"android:scaleType="centerCrop" /><TextViewandroid:id="@+id/fruit_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_margin="5dp"android:textSize="16sp" /></LinearLayout></androidx.cardview.widget.CardView>

MainActivity

package com.example.mycardview;import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;import android.os.Bundle;import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class MainActivity extends AppCompatActivity {private Fruit[] fruits = {new Fruit("Apple", R.drawable.apple), new Fruit("Banana", R.drawable.banana),new Fruit("Orange", R.drawable.orange), new Fruit("Watermelon", R.drawable.watermelon),new Fruit("Pear", R.drawable.pear), new Fruit("Grape", R.drawable.grape),new Fruit("Pineapple", R.drawable.pineapple), new Fruit("Strawberry", R.drawable.strawberry),new Fruit("Cherry", R.drawable.cherry), new Fruit("Mango", R.drawable.mango)};private List<Fruit> fruitList = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initFruits();RecyclerView rl = findViewById(R.id.rl);GridLayoutManager layoutManager = new GridLayoutManager(this, 2);rl.setLayoutManager(layoutManager);FruitAdapter adapter = new FruitAdapter(fruitList);rl.setAdapter(adapter);}private void initFruits() {fruitList.clear();for (int i = 0; i < 50; i++) {Random random = new Random();int index = random.nextInt(fruits.length);fruitList.add(fruits[index]);}}
}

MainActivity  .xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rl"android:layout_width="match_parent"android:layout_height="match_parent"/></androidx.constraintlayout.widget.ConstraintLayout>

adapter:

package com.example.mycardview;import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;import com.bumptech.glide.Glide;import java.util.List;public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {private static final String TAG = "FruitAdapter";private Context mContext;private List<Fruit> mFruitList;static class ViewHolder extends RecyclerView.ViewHolder {CardView cardView;ImageView fruitImage;TextView fruitName;public ViewHolder(View view) {super(view);cardView = (CardView) view;fruitImage = (ImageView) view.findViewById(R.id.fruit_image);fruitName = (TextView) view.findViewById(R.id.fruit_name);}}public FruitAdapter(List<Fruit> fruitList) {mFruitList = fruitList;}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {if (mContext == null) {mContext = parent.getContext();}View view = LayoutInflater.from(mContext).inflate(R.layout.fruit_item, parent, false);final ViewHolder holder = new ViewHolder(view);return holder;}@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {Fruit fruit = mFruitList.get(position);holder.fruitName.setText(fruit.getName());Glide.with(mContext).load(fruit.getImageId()).into(holder.fruitImage);}@Overridepublic int getItemCount() {return mFruitList.size();}}

item:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardViewxmlns: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="5dp"app:cardCornerRadius="4dp"><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/fruit_image"android:layout_width="match_parent"android:layout_height="100dp"android:scaleType="centerCrop" /><TextViewandroid:id="@+id/fruit_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_margin="5dp"android:textSize="16sp" /></LinearLayout></androidx.cardview.widget.CardView>
Fruit package com.example.mycardview;
public class Fruit {private String name;private int imageId;public Fruit(String name, int imageId) {this.name = name;this.imageId = imageId;}public String getName() {return name;}public int getImageId() {return imageId;}}

2.AppBarLayout

当toolbar和RecyclerView一起共用的时候,RV遮挡了toolbar如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.appcompat.widget.Toolbarandroid:id="@+id/toobar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"android:popupTheme="@style/ThemeOverlay.AppCompat.Light"android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"app:layout_scrollFlags="scroll|enterAlways|snap" /><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rl"android:layout_width="match_parent"android:layout_height="match_parent"/></androidx.constraintlayout.widget.ConstraintLayout>

这个时候可以使用AppBarLayout,他实际是一个垂直的linearLayout,修改代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.google.android.material.appbar.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><androidx.appcompat.widget.Toolbarandroid:id="@+id/toobar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"android:popupTheme="@style/ThemeOverlay.AppCompat.Light"android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"app:layout_scrollFlags="scroll|enterAlways|snap" /></com.google.android.material.appbar.AppBarLayout>
<!--        &lt;!&ndash;        scroll:RV 向上滚动的时候ToolBar会跟着一起向上滚动并实现隐藏&ndash;&gt;-->
<!--        &lt;!&ndash;        enterAlways:当RV像下滚动的时候 toolbar会跟着一起向下滑动&ndash;&gt;-->
<!--        &lt;!&ndash;        snap:当还没有完全显示、隐藏的时候,会根据当前滚动的距离,自动显示隐藏&ndash;&gt;--><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rl"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior" /></androidx.constraintlayout.widget.ConstraintLayout>

AppBarLayout包裹Toolbar

<com.google.android.material.appbar.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><androidx.appcompat.widget.Toolbarandroid:id="@+id/toobar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"android:popupTheme="@style/ThemeOverlay.AppCompat.Light"android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"app:layout_scrollFlags="scroll|enterAlways|snap" /></com.google.android.material.appbar.AppBarLayout>

app:layout_behavior="@string/appbar_scrolling_view_behavior"

<androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rl"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior" />

效果如下,但是toolbar遮挡了部分rv

可以使用CoordinatorLayout解决:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.coordinatorlayout.widget.CoordinatorLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><com.google.android.material.appbar.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><androidx.appcompat.widget.Toolbarandroid:id="@+id/toobar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"android:popupTheme="@style/ThemeOverlay.AppCompat.Light"android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"app:layout_scrollFlags="scroll|enterAlways|snap" /></com.google.android.material.appbar.AppBarLayout>
<!--        &lt;!&ndash;        scroll:RV 向上滚动的时候ToolBar会跟着一起向上滚动并实现隐藏&ndash;&gt;-->
<!--        &lt;!&ndash;        enterAlways:当RV像下滚动的时候 toolbar会跟着一起向下滑动&ndash;&gt;-->
<!--        &lt;!&ndash;        snap:当还没有完全显示、隐藏的时候,会根据当前滚动的距离,自动显示隐藏&ndash;&gt;--><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rl"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior" /></androidx.coordinatorlayout.widget.CoordinatorLayout></androidx.constraintlayout.widget.ConstraintLayout>

使用CoordinatorLayout将所有的布局包裹一下:

转发标明出处:https://blog.csdn.net/qq_35698774/article/details/108112074

点击下载

android互助群:

感谢:郭霖的《第一行代码 第二版》

CardView的基本使用相关推荐

  1. Android5.0之CardView的使用

    CardView也是一个非常炫酷的控件,一般我们将CardView配合RecyclerView来使用,当然,CardView也可以配合ListView来使用,都是可以的.OK,我们先来看一张CardV ...

  2. 简约才是王道? CardView 的使用

    发现个好看的东东 CardView.他在support v7包中~~ 顾名思义就是卡片view.能够设置阴影,圆角,等等.. 样子是这种: 或者你还能够放到listview里 是这种: http:// ...

  3. Android CardView 开发过程中要注意的细节

    原文出处:http://www.codeceo.com/article/android-cardview.html 2014 年,随着 Google 推出了全新的设计语言 Material Desig ...

  4. CardView的那点事儿

    概述 官方说明和文档 CardView官方API 创建列表与卡片 类继承关系: java.lang.Object ↳ android.view.View ↳ android.view.ViewGrou ...

  5. RecyclerView+CardView

    RecyclerView+CardView 简单效果图 build.gradle apply plugin: 'com.android.application'android {compileSdkV ...

  6. Android Material Design之在RecyclerView中嵌套CardView实现

    前言: 第一眼就爱上了Android的Material Design风格.以前倒对Android的界面风格不那么喜欢,扁平化的界面设计真是好看. 其实,这个嵌套操作在实现上并没有什么难点.可是,我还在 ...

  7. Android中CardView的简单使用

    Android 5.0的一个新增加的控件CardView. 环境配置 在Android Studio中进行使用,我们需要只需要在Gradle中添加CardView包的依赖即可进行使用. [html]  ...

  8. Android CardView卡片布局 标签: 控件

    CardView介绍 CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果:CardView被包装为一种布局,并且经常在ListV ...

  9. Android之 RecyclerView,CardView 详解和相对应的上拉刷新下拉加载

    为什么80%的码农都做不了架构师?>>>    随着 Google 推出了全新的设计语言 Material Design,还迎来了新的 Android 支持库 v7,其中就包含了 M ...

  10. Android项目笔记【项目管理统计图app】:使用github上的cardslib开源项目实现CardView(1)...

    因为项目中用到第三级菜单,我们原有的界面框架已经不适用于该项目,Android L出了新的cardview设计,爬了下github发现有些高手已经把card整合为更方便调用的类库了,我这个项目就准备试 ...

最新文章

  1. gtkorphan清理孤立软件包
  2. python可变数量参数的平均值_Python Pandas:计算可变行数的滚动均值(移动平均值)...
  3. VC字体安装相关方法总结
  4. 数据scale过程用model更方便,可以保存到本地
  5. IOS高级编程之二:IOS的数据存储与IO
  6. 图像处理与图像识别笔记(六)图像增强3
  7. 按下回车键自动切换焦点到下个控件
  8. 【深度学习】PyCorrector中文文本纠错实战
  9. python正交表结果生成
  10. 计算机地图制图的点状符号制作,计算机地图制图地图符号库系统建立.doc
  11. ipscan(ip端口扫描工具) 2.21 中文绿色版 局域网ip端口扫描神器
  12. 大秦帝国部:黑色裂变(上卷)读后感
  13. Android BLE GATT CONN LMP TIMEOUT 0x22
  14. 微信小程序之——实现一行滑动显示很多文字-scroll-view
  15. 微信图文中出现了腾讯视频时,教你弄到不能直接获取的视频原始地址的方法~
  16. Three.js坐标系与变换矩阵快速入门
  17. 【日语】secret base
  18. Spark中RDD的sortBy排序的5种实现方法
  19. button渐变色 ios_UIButton 背景色渐变动画
  20. jQuery 如何通过 ID 选择器 获取动态ID

热门文章

  1. 武汉大学计算机研究生导师,武汉大学计算机学院导师简介-丁立新
  2. 迷你迅雷 vs. QQ旋风
  3. 冒险岛079单机搭建流程
  4. 联想笔记本windows10,点击蓝牙显示无法连接
  5. 知识点二十五:启发式搜索算法——A*算法
  6. 华为防火墙ssl xxx配置
  7. RocketMq中MessageQueue的分配
  8. 词法分析flex 语法分析bison
  9. 交通流特征工程小技巧与思考
  10. 矩阵与拼接屏如何连接之方案详解