RecyclerView和ConstraintLayout的介绍与使用方式

一、RecyclerView

1、RecyclerView的介绍

RecyclerView是support:recyclerview-v7包中提供的一个类。它具有ListView、GridView、Gallery这些类的功能。
那么为什么有了上面这些类后,Google还推出RecyclerView这个类呢?它到底有什么样的优势呢?
相比于ListView、GridView和Gallery这些类,RecyclerView已经将ViewHolder制定了一套标准,所有我们自定义的ViewHolder必须继承RecyclerView.ViewHolder,然后需要在构造方法中初始化Item中要用到的控件。
同时通过设置不同的LayoutManager,以及结合ItemDecoration 、ItemAnimator、ItemTouchHelper,可以实现非常炫酷的效果,这些功能是ListView等控件难以企及的。
说了那么多,那么RecyclerView到底应该怎么用呢?下面举个例子说明如何使用RecyclerView来实现ListView的功能。

2、RecyclerView的使用方法

首先在使用RecyclerView前,必须要在app的gradle中依赖RecyclerView,如下所示:

compile 'com.android.support:recyclerview-v7:23.4.0'

然后我们需要在布局文件中引用RecyclerView,如下所示:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/> 

然后,我们需要在Activity中获取到该RecyclerView实例,然后设置LayoutManager和adapter:

//通过findViewById拿到RecyclerView实例
mRecyclerView =   findViewById(R.id.recycler_view);
//设置RecyclerView管理器
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//初始化适配器
mAdapter = new MyRecyclerViewAdapter(list); 
//设置适配器
mRecyclerView.setAdapter(mAdapter);

上面自定义了MyRecyclerViewAdapter,我们看它是如何实现的:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
    private List<String> list;
    
    public MyRecyclerViewAdapter(List<String> list) {
        this.list = list;
    }
 
    @Override
    public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_layout, parent, false);
        MyRecyclerViewAdapter.ViewHolder viewHolder = new MyRecyclerViewAdapter.ViewHolder(view);
        return viewHolder;
    }
 
    @Override
    public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
        holder.mText.setText(list.get(position));
    }
 
    @Override
    public int getItemCount() {
        return list.size();
    }
 
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mText;
        ViewHolder(View itemView) {
            super(itemView);
            mText = itemView.findViewById(R.id. item_text_view);
        }
    }
}

自定义的MyRecyclerViewAdapter必须实现RecyclerView.Adapter,它是一个泛型类,其中传入的是一个自定义的ViewHolder,这个ViewHolder必须继承RecyclerView.ViewHolder,构造函数传入的参数是ItemView对象,并在此初始化所需要用到的控件。
最后是item_view_layout布局文件的实现:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
 
    <TextView
        android:id="@+id/item_text_view"
        android:layout_width="match_content"
        android:layout_height="wrap_content"
        android:gravity="center"               
        android:text="Item"/>
</LinearLayout>

以上就是RecyclerView实现ListView的全部代码,其实和ListView最大的不同点就是在于Adapter简化了很多,ListView中需要实现的getView方法中的繁重的逻辑全部都内聚到了RecyclerView的内部逻辑中,开发者只需要关注itemView的功能即可。

3、参考文章

自从用了RecyclerView,腰再也不痛了,手也不酸了

二、ConstraintLayout

1、ConstraintLayout的介绍

ConstraintLayout在Android Studio 2.2就已经出现了,在传统的Android界面开发中,都是通过xml来完成的,而且也是可以通过可视化的方式来完成的,但是不太方便。但是自从ConstaintLayout出现后,它和传统编写界面的方式恰恰相反,ConstraintLayout非常适合使用可视化的方式来编写界面,但并不太适合使用XML的方式来进行编写。
然后,ConstraintLayout还有另外一个优点,它可以有效的解决布局嵌套问题。我们平时编写界面,复杂的布局总会伴随着多层的嵌套,而嵌套越多,程序的性能也就越差。ConstraintLayout则是使用约束的方式来指定各个控件的位置和关系的,它有点类似于RelativeLayout,但远比RelativeLayout要更强大。

2、ConstraintLayout的使用方法

说了那么多,我们就来看一下ConstraintLayout是如何使用的。
首先,要使用ConstraintLayout,需要在app的gradle文件中添加ConstraintLayout的依赖,如下所示:

implementation 'com.android.support.constraint:constraint-layout:1.0.2'

我们先来看一个布局形式:



如果要实现这样的布局,我们使用传统的RelativeLayout,肯定是需要ViewGroup的嵌套才能完成的。
如果使用ConstraintLayout,完全是不需要嵌套的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/imgview"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorPrimary"/>
    <TextView
        android:id="@+id/feed_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="这是一段TextView这是一段TextView这是一段TextView"
        app:layout_constraintLeft_toRightOf="@+id/imgview"
        app:layout_constraintTop_toTopOf="@+id/imgview"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="20dp"/>

    <TextView
        android:id="@+id/time_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1小时前"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/imgview"
        android:layout_marginRight="10dp"/>
</android.support.constraint.ConstraintLayout>

上面的布局文件中,先看左边的ImageView,它有两个属性,分别是app:layout_constraintLeft_toLeftOf和app:layout_constraintTop_toTopOf。

app:layout_constraintLeft_toLeftOf的意思是:
当前控件左边的边需要和哪一个控件左边的边进行对齐。
app:layout_constraintLeft_toLeftOf=“parent"说明:
ImageView左边的边和父布局左边的边进行对齐。

同理,app:layout_constraintTop_toTopOf=”parent"说明ImageView的顶部和父布局对齐。
加上这两个约束后,就唯一确定了ImageView的位置。

我们再来看下面一个TextView,它有三个属性,分别是app:layout_constraintLeft_toRightOf、app:layout_constraintTop_toTopOf、app:layout_constraintRight_toRightOf
根据上面的分析,我们很清楚的就知道了该TextView的约束是这样的:

它左边的边和ImageView右边的边对齐。
它上面的边和ImageView上面的边对齐。
它右边的边和父布局右边的边对齐。

类似的属性还有:

  • layout_constraintRight_toLeftOf
  • layout_constraintRight_toRightOf
  • layout_constraintTop_toTopOf
  • layout_constraintTop_toBottomOf
  • layout_constraintBottom_toTopOf
  • layout_constraintBottom_toBottomOf
  • layout_constraintBaseline_toBaselineOf

3、另一个例子


如果上面是用RelativeLayout来写的话,那么代码是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/btn1"
        android:layout_alignParentRight="true"
        android:text="button2"/>

</RelativeLayout>

如果将上面的RelativeLayout转换为ConstraintLayout,那么代码是这样的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/btn1"
        app:layout_constraintRight_toRightOf="parent"
        android:text="button2"/>

</android.support.constraint.ConstraintLayout>

我们将android:layout_toRightOf="@+id/btn1"换成了app:layout_constraintLeft_toRightOf="@+id/btn1",将android:layout_alignParentRight="true"换成了app:layout_constraintRight_toRightOf="parent"
但是出来的效果却不是很理想:


bt2并没有占满bt1之外的屏幕宽度。这是为什么呢?

其实控件有自己设置的宽度时,例如warp_content、固定值,我们为控件添加的都是约束的“Constraint”,这个约束有点像橡皮筋一样会拉这个控件,但是并不会改变控件的尺寸。

在上面的例子中,当btn2的宽度较小时,我们为其左侧设置了一个约束(btn1右侧),右侧设置了一个约束(parent右侧对齐),当两个约束同时生效的时候(你可以认为两边都是相同的一个拉力),这样btn2会居中。

当btn2设置的宽度特别大的时候,依然是这两个力,那么会发生什么?这样就会造成左侧和右侧超出的距离一样大。

那么怎样才能使得btn2实现上面的效果呢?
我们可以将btn2的宽度设置为0dp,因为在ConstraintLayout中0代表:MATCH_CONSTRAINT。顾名思义,这个常量就是和约束保持一样尺寸的意思。
下面是正确的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/btn1"
        app:layout_constraintRight_toRightOf="parent"
        android:text="button2"/>

</android.support.constraint.ConstraintLayout>

4、增加一个banner

现在我们往第一个布局中的最上面增加一个banner,并且设置其宽高比为16:6。如下图所示:



代码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/imgview"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/banner"
        android:background="@color/colorPrimary"/>
    <TextView
        android:id="@+id/feed_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="这是一段TextView这是一段TextView这是一段TextView"
        app:layout_constraintLeft_toRightOf="@+id/imgview"
        app:layout_constraintTop_toTopOf="@+id/imgview"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="20dp"/>

    <TextView
        android:id="@+id/time_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1小时前"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/imgview"
        android:layout_marginRight="10dp"/>

    <TextView
        android:id="@+id/banner"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        app:layout_constraintDimensionRatio="16:6"
        android:gravity="center"
        android:text="banner"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/imgview"/>
</android.support.constraint.ConstraintLayout>

在这里,我们使用了app:layout_constraintDimensionRatio这个属性,这个属性默认的意思是表示宽高比,我们设置了16:6,也就是说在宽度已经固定为约束的尺寸(屏幕宽度)时,高度会根据这个比例进行调整。
其实app:layout_constraintDimensionRatio这个属性还有另外两种写法:

app:layout_constraintDimensionRatio="W,16:6"
app:layout_constraintDimensionRatio="H,16:6"

其中app:layout_constraintDimensionRatio="H,16:6"和默认是一样的,表示宽高比。因为H的意思是使用高(H表示高度)作为后面16:6中的分母。
所以app:layout_constraintDimensionRatio="W,16:6"就很好理解了,其实就是使用宽(W表示宽度)作为分母。

5、增加几个Tab

我们现在需要在底部增加3个tab,并让它们实现均分。这有点像LinearLayout和weight的意思。我们先看一下效果图是怎样的:



代码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/imgview"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/banner"
        android:background="@color/colorPrimary"/>
    <TextView
        android:id="@+id/feed_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="这是一段TextView这是一段TextView这是一段TextView"
        app:layout_constraintLeft_toRightOf="@+id/imgview"
        app:layout_constraintTop_toTopOf="@+id/imgview"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="20dp"/>

    <TextView
        android:id="@+id/time_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1小时前"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/imgview"
        android:layout_marginRight="10dp"/>

    <TextView
        android:id="@+id/banner"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        app:layout_constraintDimensionRatio="16:6"
        android:gravity="center"
        android:text="banner"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/imgview"/>

    <Button
        android:id="@+id/tab1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="tab1"
        app:layout_constraintRight_toLeftOf="@+id/tab2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <Button
        android:id="@+id/tab2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="tab2"
        app:layout_constraintLeft_toRightOf="@+id/tab1"
        app:layout_constraintRight_toLeftOf="@+id/tab3"
        app:layout_constraintBottom_toBottomOf="parent"/>


    <Button
        android:id="@+id/tab3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="tab3"
        app:layout_constraintLeft_toRightOf="@+id/tab2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

我们增加了3个tab,并且让它们之间相互依赖,即tab1的左边和parent的左边对齐,右边和tab2的左边对齐。tab2的左边和tab1的右边对齐,右边和tab3的左边对齐。tab3的左边和tab2的右边对齐,右边和parent的右边对齐。
这样就实现了3个tab均分整个屏幕的效果。
那么平分算是实现了,那么可以实现LinearLayout和weight给单独控件实现不同比例配置的效果吗?其实也是可以的。
ConstraintLayout提供了app:layout_constraintHorizontal_weight属性,它可以实现不同的tab占据不同的比例。
如果我们给上面的3个tab分别设置为2:1:1,那么就可以实现下面的效果:


其实app:layout_constraintHorizontal_weight属性还有更多的值可以设置,具体可以看hongyang大神的这篇文章:ConstraintLayout 完全解析 快来优化你的布局吧
当然,ConstraintLayout还有很多其他高级的玩法,大家感兴趣的可以上网搜一下,这里就不再详谈了。

6、参考文章

Android新特性介绍,ConstraintLayout完全解析
实战篇ConstraintLayout的崛起之路
ConstraintLayout 完全解析 快来优化你的布局吧

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容