卡片式布局

本文介绍了如何在Android应用中使用CardView来创建卡片式布局,并将其结合到RecyclerView中。首先,讨论了CardView作为FrameLayout的增强版,提供圆角和阴影效果。接着,详细阐述了如何在RecyclerView的子布局中使用CardView,以及在activity_main.xml中添加RecyclerView和自定义FruitAdapter。最后,提到了使用AppBarLayout来管理Toolbar和RecyclerView的滚动行为,通过指定特定的布局行为实现更流畅的交互体验。
摘要由CSDN通过智能技术生成

CardView

CardView其实也是一个FrameLayout,但是它额外提供了圆角和阴影等效果,所以看起来会有立体的感觉。

在RecyclerView填充CardView

子布局设置为CardView

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    app:cardCornerRadius="5dp"
    android:layout_margin="5dp"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="centerCrop"
            />
        <TextView
            android:id="@+id/image_name"
            android:layout_width="wrap_content"
            android:layout_gravity="left"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            />
    </LinearLayout>

</androidx.cardview.widget.CardView>

在activity_main.xml中添加RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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="match_parent"
    android:orientation="vertical"
    android:id="@+id/drawer_layout"
    >
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>/
        </LinearLayout>
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/fab"
            android:layout_gravity="bottom|end"
            android:src="@drawable/ic_done"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation"
        android:layout_gravity="start"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:headerLayout="@layout/nav_header"
        android:elevation="8dp"
        app:menu="@menu/nav_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>

在MainActivity中自定义适配器(FruitAdapter)

class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
    List<Fruit> FruitList;
    ViewHolder viewHolder;
    Context context;
    FruitAdapter(List<Fruit> fruits){
        FruitList=fruits;
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        ImageView imageView;
        TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView=(CardView)itemView;
            imageView=itemView.findViewById(R.id.image_view);
            textView=itemView.findViewById(R.id.image_name);
        }
    }
    @NonNull
    @Override
    public FruitAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        if(context==null){
            context=parent.getContext();
        }
        View view=LayoutInflater.from(context).inflate(R.layout.fruit_item,parent,false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull FruitAdapter.ViewHolder holder, int position) {
        Fruit fruit=FruitList.get(position);
        Glide.with(context).load(fruit.getId()).into(holder.imageView);
        holder.textView.setText(fruit.getName());
    }
    @Override
    public int getItemCount() {
        return FruitList.size();
    }
}

为ReclyclerView设置适配器和布局。

布局采用网格布局(更能体现网格布局)。

FruitAdapter  fruitAdapter=new FruitAdapter(fruitList);
        recyclerView.setAdapter(fruitAdapter);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
        recyclerView.setLayoutManager(gridLayoutManager);

AppBarLayout

在之前的布局中,为了让Toolbar和RecyclerView不重合在一起,用了一个垂直方向线性布局将他们做了布局,其实用AppBarLayout就可以实现这个效果,这实际是一个垂直方向的LinearLayout,在内部做了很多滚动事件的封装。
这里修改xml,将Toolbar放在AppBarLayout中,同时为RecyclerView添加一个布局行为。

     <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        </com.google.android.material.appbar.AppBarLayout>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

这里其实还没有体现出具体的效果,可以在AppBarLayout中的子控件(这里是Toolbar)指定具体的行为。

            <androidx.appcompat.widget.Toolbar
                app:layout_scrollFlags="scroll|enterAlways"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

这里scroll会让Toolbar跟随ReclyclerView一起向上,enterAlways会跟随ReclyclerView一起向下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值