android——卡片式布局
一、CardView
<android.support.v7.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"> <TextView
android:id="@+id/overwatch_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:textSize="16sp"/> </android.support.v7.widget.CardView>
这是一个CardView的简单布局,app:cardCornerRadius这个属性指定了卡片圆角的弧度,另外还可以通过app:elevation指定卡片的高度,改变卡片的阴影效果。
要使用CardView需要添加相应的库,在app/build.gradle中:
compile 'com.android.support:recyclerview-v7:26.+'
compile 'com.android.support:cardview-v7:26.+'
compile 'com.github.bumptech.glide:glide:4.0.0'
第一行是RecyclerView需要的库,第二行就是CardView,第三行是一个Glide的库,Glide是一个超级强大的图片加载库,一行代码就能实现图片加载功能。
首先在主活动的布局中加入RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_height="match_parent"
android:layout_width="match_parent" />
然后为RecyclerView的子项指定一个自定义的布局,在layout目录下新建overwatch_item.xml:
<android.support.v7.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/overwatch_image"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"/> <TextView
android:id="@+id/overwatch_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:textSize="16sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
然后新建一个OverWatch类:
public class OverWatch {
    private String name;
    private int imageID;
    public OverWatch(String name, int imageID) {
        this.imageID = imageID;
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public int getImageID() {
        return imageID;
    }
}
这个类就是每个卡片的内容了。name代表名字,imageID代表图片资源ID。
然后就需要为RecyclerView准备一个适配器:
 public class OverWatchAdapter extends RecyclerView.Adapter<OverWatchAdapter.ViewHolder> {
     private Context mContext;
     private List<OverWatch> mOverWatch;
     static class ViewHolder extends RecyclerView.ViewHolder{
         CardView cardView;
         ImageView overwatchImage;
         TextView overwatchName;
         public ViewHolder(View view){
             super(view);
             cardView = (CardView) view;
             overwatchImage = (ImageView) view.findViewById(R.id.overwatch_image);
             overwatchName = (TextView) view.findViewById(R.id.overwatch_name);
         }
     }
     public OverWatchAdapter(List<OverWatch> OverWatchList){
         mOverWatch = OverWatchList;
     }
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         if(mContext == null){
             mContext = parent.getContext();
         }
         View view = LayoutInflater.from(mContext).inflate(R.layout.overwatch_item,parent,false);
         return  new ViewHolder(view);
     }
     public void onBindViewHolder(ViewHolder viewHolder, int position) {
         OverWatch overWatch = mOverWatch.get(position);
         viewHolder.overwatchName.setText(overWatch.getName());
         Glide.with(mContext).load(overWatch.getImageID()).into(viewHolder.overwatchImage);
     }
     public int getItemCount() {
         return mOverWatch.size();
     }
 }
第35行就是Glide加载图片的方法,首先是调用Glide.with()方法传入一个Context参数,然后调用load()方法去加载图片,参数可以是URI,也可以使一个本地路径,或者是一个资源ID,然后再调用into()方法将图片设置到某一个ImageView中去。
然后就是主活动的java代码:
 public class MainActivity extends AppCompatActivity {
     private DrawerLayout mDrawerLayout;
     private OverWatch[] overWatches = {new OverWatch("猎空",R.drawable.img_1),new OverWatch("猎空",R.drawable.img_2),
             new OverWatch("猎空",R.drawable.img_3),new OverWatch("猎空",R.drawable.img_5),
             new OverWatch("猎空",R.drawable.img_6),new OverWatch("猎空",R.drawable.img_7),
             new OverWatch("猎空",R.drawable.img_8),new OverWatch("猎空",R.drawable.img_9),
             new OverWatch("猎空",R.drawable.img_10),new OverWatch("猎空",R.drawable.img_11)};
     private List<OverWatch> overWatchList = new ArrayList<>();
     private OverWatchAdapter overWatchAdapter;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
  ...
         //图片加载
         initOverWatch();
         RecyclerView reccyclerView = (RecyclerView) findViewById(R.id.recycler_view);
         GridLayoutManager layoutManager = new GridLayoutManager(MainActivity.this,2);
         reccyclerView.setLayoutManager(layoutManager);
         overWatchAdapter = new OverWatchAdapter(overWatchList);
         reccyclerView.setAdapter(overWatchAdapter);
     }
 ...
     }
 ...
     //存放图片
     private  void initOverWatch(){
         overWatchList.clear();
         for (int i = 0; i < 50; i++){
             Random random = new Random();
             int index = random.nextInt(overWatches.length);
             overWatchList.add(overWatches[index]);
         }
     }
 }
运行程序:

二、AppBarLayout
刚刚的RecyclerView把Toolbar挡住了.从布局xml中看:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.v7.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"/> <android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_height="match_parent"
android:layout_width="match_parent" /> <android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fab"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_done"
app:elevation="8dp" /> </android.support.design.widget.CoordinatorLayout>
Toolbar、RecyclerView、FloatingActionButton都是放置在CoordinatorLayout中的,因为CoordinatorLayout就是一个加强版的FrameLayout,从上到下的布局会逐渐覆盖,所以我们可以把Toolbar的布局代码下移试试:

但是这样Toolbar又把RecyclerView的一部分覆盖住了,所以再试试在CoordinatorLayout中嵌套一个LinearLayout:
<android.support.design.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"> <android.support.v7.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"/> <android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout> <android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fab"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_done"
app:elevation="8dp" />
</android.support.design.widget.CoordinatorLayout>
然后就完成了:

然后其实这里想讲的是AppBarLayout,其实AppBarLayout就是垂直方向上的LinearLayout不过是在其内部做了很多滚动事件的封装,所以可以用AppBarLayout避免这个遮挡
先把Toolbar嵌套到AppBarLayout中,然后给RecyclerView指定一个布局行为:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <android.support.v7.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"/> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/> <android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fab"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_done"
app:elevation="8dp" />
</android.support.design.widget.CoordinatorLayout>
效果和使用LinearLayout一样。但是AppBarLayout做了一些滚动事件的封装。比如这样改:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <android.support.v7.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"
app:layout_scrollFlags="scroll|enterAlways|snap"/> </android.support.design.widget.AppBarLayout>
app:layout_scrollFlags这个属性的值指定为scroll就是,当RecyclerView向上滚动的时候,Toolbar会随着滚动而隐藏,enterAlways是指向下滚动的时候,snap指根据滚动的距离,自动选择隐藏还是显示。
三、下拉刷新
使用的是SwipeRefreshLayout然某个控件实现下拉刷新:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swip_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"> <android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_height="match_parent"
android:layout_width="match_parent" /> </android.support.v4.widget.SwipeRefreshLayout>
将想要实现下拉刷新的某控件嵌套在SwipeRefreshLayout就行了,这里要注意把app:layout_behavior="@string/appbar_scrolling_view_behavior"这个属性移到SwipeRefreshLayout。
然后是JAVA代码:
public class MainActivity extends AppCompatActivity {
...
    private SwipeRefreshLayout swipeRefresh;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
...
        //下拉刷新
        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swip_refresh);
        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshOverWatch();
            }
        });
    }
 ...
    //刷新图片
    private  void refreshOverWatch(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initOverWatch();
                        overWatchAdapter.notifyDataSetChanged();
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }
}
具体步骤就是:
1、获取SwipeRefreshLayout的实例。
2、设置监听器。
3、编写刷新的使用的方法
在这里要注意的有,setColorSchemeResources()这个方法设置的是刷新时进度条的颜色,Thread.sleep(1000)先让线程沉睡一秒,让,然后改变数据,接着调用overWatchAdapter.notifyDataSetChanged()方法通知数据发生了变化,swipeRefresh.setRefreshing(false)用于表示刷新事件结束,并隐藏进度条。
android——卡片式布局的更多相关文章
- CollapsingToolbarLayoutDemo【可折叠式标题栏,顺便带有CardView卡片式布局】
		版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 CollapsingToolBarLayout是一个作用于ToolBar基础之上的布局,它也是由Design Support库提供的 ... 
- CardView卡片式布局
		CardView适用于实现卡片式布局效果的重要控件,由appcompat-v7库提供,实际上CardView也是一个FrameLayout,只是额外提供了圆角和阴影效果,看上去有立体的感觉.一般Car ... 
- [Android] Android 卡片式控件CardView的优雅使用
		[Android] Android 卡片式控件CardView的优雅使用 CardView是在安卓5.0提出的卡片式控件 其具体用法如下: 1.在app/build.gradle 文件中添加 comp ... 
- Android零基础入门第71节:CardView简单实现卡片式布局
		还记得我们一共学过了多少UI控件了吗?都掌握的怎么样啊. 安卓中一些常用控件学习得差不多了,今天再来学习一个新的控件CardView,在实际开发中也有非常高的地位. 一.CardView简介 Card ... 
- Android 性能优化---布局优化
		Android 性能优化---布局优化 Android 布局绘制原理 布局加载过程 setContentView() --> inflate() -- > getLayout()(I/O操 ... 
- 树莓派Odroid等卡片式电脑上搭建NAS教程系列6-miniDLNA
		目录: 1. 树莓派Odroid等卡片式电脑上搭建NAS教程系列1-Ubuntu系统安装 2. 树莓派Odroid等卡片式电脑上搭建NAS教程系列2-SSH连接访问 3. 树莓派Odroid等卡片式电 ... 
- Android菜鸟成长记7 -- Android的五大布局
		Android五大布局,相信android的都了解过,今天我根据自己的学习整理一下五大布局,主要介绍的是线性布局(LiearLayout),因为,其他的布局使用率不是很高. Android的五大布局 ... 
- 无废话Android之常见adb指令、电话拨号器、点击事件的4种写法、短信发送器、Android 中各种布局(1)
		1.Android是什么 手机设备的软件栈,包括一个完整的操作系统.中间件.关键的应用程序,底层是linux内核,安全管理.内存管理.进程管理.电源管理.硬件驱动 2.Dalvik VM 和 JVM ... 
- 14.Android之Layout布局学习
		Android布局主要有5种,接下来学习总结下. 1) 最常见的线性布局 LinearLayout 线性布局是Android布局中最简单的布局,也是最常用,最实用的布局. android:orient ... 
随机推荐
- Shiro中@RequiresAuthentication等等注解介绍
			使用前请先开启Shiro的controller层注解,如果已经设置请下滑绕过 要在spring-mvc.xml中写. <!--下面的用于开启shiro的权限注解--> <bean c ... 
- Spring Cloud Alibaba | Nacos服务注册与发现
			目录 Spring Cloud Alibaba | Nacos服务注册与发现 1. 服务提供者 1.1 pom.xml项目依赖 1.2 配置文件application.yml 1.3 启动类Produ ... 
- PYTHONIOENCODING = UTF-8 引发的血案
			血案: 我就是想在Jenkins上运行一段自动化python代码,就是最简单的本地控制台输出, 我就不懂了它为什么一直是去找 cp1252.py 编码???目前, 确定 pycharm 运行脚本很OK ... 
- python 3.5学习笔记(第四章)
			本章内容: 一..装饰器 二.生成器 三.迭代器 四.python中的内置方法 五.json & pickle 的数据序列化及反序列化 六.程序目录结构规范 七.补充内容 一.装饰器: 1.概 ... 
- c语言进阶3-有参函数
			一. 有参函数的定义 有参函数的定义格式如下: 类型标识符 函数名(形式参数表列) { 语句: } 如 void fun(int a,int b) { printf(“a+b=%d”,a ... 
- 安装解压版MySQL5.76及以上版本 出现服务正在启动-服务无法启动的问题
			最近重装了系统,去MySQL官网下载了最新的MySQL5.7.9,我选择的是解压版,安装之后启动服务的时候,提示服务无法启动,在网上找了很多教程,弄了很久都没有弄好,后来还是决定去英文官网找找答案, ... 
- python课堂整理2
			一.字节和编码 1个字节是8位二进制 utf-8 表示一个中文要用3个字节 gbk 为中国汉字发明,2个字节可表示汉字 所以 utf-8 可以读gbk编码,而gbk去读utf-8 的内容会乱码 uni ... 
- springcloud-provider-consumer-register
			作者:纯洁的微笑出处:http://www.ityouknow.com/ 版权归作者所有,转载请注明出处 上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注 ... 
- 基于SpringBoot从零构建博客网站 - 新增创建、修改、删除专栏功能
			守望博客是支持创建专栏的功能,即可以将一系列相关的文章归档到专栏中,方便用户管理和查阅文章.这里主要讲解专栏的创建.修改和删除功能,至于专栏还涉及其它的功能,例如关注专栏等后续会穿插着介绍. 1.创建 ... 
- phpStudy 升级 MySQL 到 5.7.21
			1.备份原来的MySQL 我的路径是D:\phpStudy2018\PHPTutorial\MySQL\bin 修改文件名为MySQL-backup 2.下载新的MySQL 5.7.21 网址:htt ... 
