cardview属性_RecyclerView

cardview属性_RecyclerViewCardView是用于实现卡片式布局效果的重要控件,实际上也是一个frameLayout,只是额外提供了圆角和阴影,看上去有立体效果。效果如下:<?xmlversion=”1.0″encoding=”utf-8″?><androidx.cardview.widget.CardViewxmlns:android=”http://schemas.android.com/apk/res/android”xmlns:app=”http:…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

1. CardView   

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

效果如下:

cardview属性_RecyclerView

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.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="5dp"
    app:cardCornerRadius="4dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

        <TextView
            android: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<>();

    @Override
    protected 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.RecyclerView
        android: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;
    }

    @Override
    public 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;
    }

    @Override
    public 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);
    }

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

}

item:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.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="5dp"
    app:cardCornerRadius="4dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

        <TextView
            android: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如下:

cardview属性_RecyclerView

<?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.Toolbar
            android: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.RecyclerView
            android: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.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android: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.RecyclerView
            android: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.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android: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.RecyclerView
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

cardview属性_RecyclerView

可以使用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.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android: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.RecyclerView
            android: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将所有的布局包裹一下:

cardview属性_RecyclerView

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

点击下载

 

android互助群:

cardview属性_RecyclerView

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

 

 

 

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/183077.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js字符串转date类型_java字符串转double

    js字符串转date类型_java字符串转double//字符串转日期时间格式getDate(strDate){vardate=eval(‘newDate(‘+strDate.replace(/\d+(?=-[^-]+$)/,function(a){returnparseInt(a,10)-1;}).match(/\d+/g)+’)’);returndate;},

    2022年10月4日
    0
  • vue文件上传和下载_vue上传文件组件

    vue文件上传和下载_vue上传文件组件Controller层上传@RequestMapping(“/uplaod”)@ResponseBodypublicRespBeanadd(@RequestParam(“file”)MultipartFilefile){//TODO处理上传的数据StringfileName=file.getOriginalFilename();StringcontentType=file.getContentType();lon

    2022年8月15日
    6
  • cadence快捷键大全(work bertrand russell)

    一、File相关Ctrl+Shift+N:新建一个窗口,即新打开一个ericCtrl+N:新建一个窗口,即编辑代码的窗口Ctrl+O:打开文件Ctrl+Shift+S:另存为Ctrl+Q:关闭eric二、Edit相关Ctrl+Z:撤回Ctrl+Shift+Z:撤回上次的撤回(你懂得,斜眼笑)Ctrl+Y:恢复到上次保存的状态Alt+Shift+C:清楚当前代…

    2022年4月16日
    87
  • Cubieboard 架设Git服务器

    Cubieboard 架设Git服务器如果你现在用的是Cubieboard或者树莓派卡片式电脑,可以查看本文之前,学习前面的四个教程,它可能会对你非常有帮助。如果你是普通的Linux用户或者LinuxVPS、Linux独立服务器等,可以直接跳过查看本文。教程一Cubieboard安装Linux系统教程二CubieboardLinux服务器配置教程三CubieboardLinux服务器安装L…

    2022年7月22日
    5
  • opencv 视频实时处理_opencv 控制摄像头

    opencv 视频实时处理_opencv 控制摄像头最近研究了通过OpenCV采集摄像头数据,并同时将视频流数据推送到RTSP和RTMP。RTSP服务采用开源的LIVE555(需要自己修改和实现部分代码)。RTMP服务采用开源CRtmpServer。

    2022年10月21日
    0
  • shell脚本–sed的用法[通俗易懂]

    shell脚本–sed的用法[通俗易懂]sed在处理文本时是逐行读取文件内容,读到匹配的行就根据指令做操作,不匹配就跳过。sed是Linux下一款功能强大的非交互流式文本编辑器,可以对文本文件进行增、删、改、查等操作,支持按行、按字段、按正则匹配文本内容,灵活方便,特别适合于大文件的编辑。本文主要介绍sed的一些基本用法,并通过shell脚本演示sed的使用实例。1.sed的使用方法,调用sed命令的语法有两种:一.在命令…

    2022年7月26日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号