Android App开发中使用RecyclerView实现Gallery画廊的实例

2019-12-10 18:27:32于海丽
易采站长站为您分析Android App开发中使用RecyclerView实现Gallery画廊的实例,比普通的ListView实现的效果更为强大,需要的朋友可以参考下  

什么是RecyclerView
        RecyclerView是Android 5.0 materials design中的组件之一,相应的还有CardView、Palette等。看名字我们就能看出一点端倪,没错,它主要的特点就是复用。我们知道,Listview中的Adapter中可以实现ViewHolder的复用。RecyclerView提供了一个耦合度更低的方式来复用ViewHolder,并且可以轻松的实现ListView、GridView以及瀑布流的效果。

RecyclerView使用的基本思路
        首先我们要gradle的依赖库中添加  compile 'com.android.support:recyclerview-v7:21.+'  。如果是eclipse直接导入android-support-v7-recyclerview.jar就可以了。

/**
* 设置Adapter
*/
mRecyclerView.setAdapter(mListAdapter);
/**
* 设置布局管理器
*/
mRecyclerView.setLayoutManager(linearLayoutManager);
/**
* 设置item分割线
*/
mRecyclerView.addItemDecoration(itemDecoration);
/**
* 设置item动画
*/
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        使用RecyclerView,基本上要上面四步。相比ListView只需设置Adapter而言,RecyclerView的使用看起来似乎要复杂一些。但是它的可定制性更高了,你可以自己定制自己的分割线样式或者是item的的动画。

实现Gallery效果
RecyclerView在这里可以被看作为ListView的升级版本,下买呢首先介绍RecyclerView的用法,然后经行一定的分析;最后自定义一下RecyclerView实现我们需要的相册效果。
1、RecyclerView的基本用法
首先主Activity的布局文件:

<RelativeLayout xmlns:android="http://www.easck.com/apk/res/android" 
  xmlns:tools="http://www.easck.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" > 
 
  <android.support.v7.widget.RecyclerView 
    android:id="@+id/id_recyclerview_horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="120dp" 
    android:layout_centerVertical="true" 
    android:background="#FF0000" 
    android:scrollbars="none" /> 
 
</RelativeLayout>