RecyclerView的基本用法
RecyclerView 是一个增强版的ListView,不仅可以实现和ListView同样的效果,还优化了ListView中存在的各种不足之处
ResyslerView 能够实现横向滚动,这是ListView所不能实现的
目前官方更加推荐使用RecyclerView.
1.实现垂直方向的滚动
在 dependencies 中添加库的引用
[html] view plain copy
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.1'
}
添加布局文件:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
创建RecyclerView 适配器 BookBaseAdapter ,这个类继承 RecyclerView.Adapter 并将泛型指定为 BookBaseAdapter.ViewHolder
其中ViewHolder是我们在 BookBaseAdapter 中定义的一个内部类:代码如下:
[html] view plain copy
public class BookBaseAdapter extends RecyclerView.Adapter<BookBaseAdapter.ViewHolder>{
private List<Book> mBookList;
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView bookImage;
TextView bookname;
public ViewHolder(View view) {
super(view);
bookImage = (ImageView) view.findViewById(R.id.book_iamge);
bookname = (TextView) view.findViewById(R.id.book_name);
}
}
public BookBaseAdapter(List<Book> mBookList) {
this.mBookList = mBookList;
}
[html] view plain copy
<span style="white-space:pre;"> </span>//加载item 的布局 创建ViewHolder实例
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.book,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
[html] view plain copy
<span style="white-space:pre;"> </span>//对RecyclerView子项数据进行赋值
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Book book = mBookList.get(position);
holder.bookname.setText(book.getName());
holder.bookImage.setImageResource(book.getImageId());
}
[html] view plain copy
<span style="white-space:pre;"> </span>//返回子项个数
@Override
public int getItemCount() {
return mBookList.size();
}
}
MainActivity调用:
[html] view plain copy
public class MainActivity extends AppCompatActivity {
private List<Book> mlsit = new ArrayList<Book>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化List数据
initBook();
//初始化RecyclerView
RecyclerView recyslerview = (RecyclerView) findViewById(R.id.recycler_view);
//创建LinearLayoutManager 对象 这里使用 <span style="font-family:'Source Code Pro';">LinearLayoutManager 是线性布局的意思</span>
LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
//设置RecyclerView 布局
recyslerview.setLayoutManager(layoutmanager);
//设置Adapter
BookBaseAdapter adapter = new BookBaseAdapter(mlsit);
recyslerview.setAdapter(adapter);
}
private void initBook(){
for (int i = 0; i < 10; i++) {
Book book01 = new Book("Book"+i,R.drawable.icon01);
mlsit.add(book01);
Book book02 = new Book("Book"+i,R.drawable.icon02);
mlsit.add(book02);
Book book03 = new Book("Book"+i,R.drawable.icon03);
mlsit.add(book03);
}
}
}
main_layout布局:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
item布局:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/book_iamge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/book_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
此处省略Book对象的相关源码。如上就可以实现和ListView一样的效果。
2.实现横向滚动
对垂直布局中的代码做小修改:
onCreat方法中添加setOrientation()方法来设置布局的排列方向
[html] view plain copy
<span style="background-color:rgb(255,255,255);"> <span> </span>layoutmanager.setOrientation(LinearLayoutManager.HORIZONTAL);</span>
[html] view plain copy
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化List数据
initBook();
//初始化RecyclerView
RecyclerView recyslerview = (RecyclerView) findViewById(R.id.recycler_view);
//创建LinearLayoutManager 对象
LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
layoutmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
//设置RecyclerView 布局
recyslerview.setLayoutManager(layoutmanager);
//设置Adapter
BookBaseAdapter adapter = new BookBaseAdapter(mlsit);
recyslerview.setAdapter(adapter);
}
修改一下item的布局:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/book_iamge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/book_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
运行程序就可以发现我们实现了横向的滚动效果
3.瀑布流布局
RecyclerView除了LinearLayoutManager 之外,还提供了GridlayoutManager和StaggeredGridlayoutManager这两种内置的布局排列方式
GridlayoutManager可以用于实现网格布局
StaggeredGridlayoutManager可以用于实现瀑布流布局,
这里我们来实现一下炫酷的瀑布流布局:
修改item.xml的布局
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/book_iamge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_gravity="left"/>
</LinearLayout>
onCreat方法:
[html] view plain copy
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化List数据
initBook();
//初始化RecyclerView
RecyclerView recyslerview = (RecyclerView) findViewById(R.id.recycler_view);
//创建LinearLayoutManager 对象
/*
* 第一个参数表示布局的列数
* 第二个参数表示布局的方向,这里我们传入StaggeredGridLayoutManager.VERTICAL,表示布局纵向排列
*/
StaggeredGridLayoutManager layoutmanager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
//设置RecyclerView 布局
recyslerview.setLayoutManager(layoutmanager);
//设置Adapter
BookBaseAdapter adapter = new BookBaseAdapter(mlsit);
recyslerview.setAdapter(adapter);
}
仅仅修改一行代码,就可以成功的实现瀑布流的布局效果
点击监听事件这里引用 http://blog.csdn.net/dmk877/article/details/50816933 的方法
给RecyclerView的Item添加点击事件
Item的点击事件RecyclerView监听事件处理在ListView使用的时候,该控件给我们提供一个onItemClickListener监听器,这样当我们点击Item的时候,会回调相关的方法,以便我们方便处理Item点击事件。对于RecyclerView来讲,非常可惜的是,该控件没有给我们提供这样的内置监听器方法,不过我们可以进行改造实现,可以这样实现Item的点击事件的监听,在我们的adapter中增加这两个方法
public interface OnItemClickListener{
void onClick( int position);
void onLongClick( int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
this. mOnItemClickListener=onItemClickListener;
}
然后onBindViewHolder方法要做如下更改
[java] view plain copy
在CODE上查看代码片派生到我的代码片
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder. tv.setText( mDatas.get(position));
if( mOnItemClickListener!= null){
holder. itemView.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onClick(position);
}
});
holder. itemView.setOnLongClickListener( new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mOnItemClickListener.onLongClick(position);
return false;
}
});
}
}<span style="color:#333333;">
</span>
在MainAcitivity中增加
[java] view plain copy
在CODE上查看代码片派生到我的代码片
recycleAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onLongClick(int position) {
Toast.makeText(MainActivity.this,"onLongClick事件 您点击了第:"+position+"个Item",0).show();
}
@Override
public void onClick(int position) {
Toast.makeText(MainActivity.this,"onClick事件 您点击了第:"+position+"个Item",0).show();
}
});
参考文档:http://blog.csdn.net/dmk877/article/details/50816933
RecyclerView的基本用法的更多相关文章
- Android控件RecyclerView的基本用法
Android控件RecyclerView的基本用法 转 https://www.jianshu.com/p/e71a4b73098f github: https://github.com/Cym ...
- Android RecyclerView Adapter 新式用法之SortedListAdapterCallback
引言 前几天在同事的提醒下发现V7中有了一个新的工具类SortedListAdapterCallback,配合RecyclerView Adapter和SortedList一起使用更加方便的管理我们在 ...
- RecyclerView的基础用法
为了让RecyclerView可以在所有的Android版本中都能使用,Android开发团队将RecyclerView定义在support.v7包当中.在使用该控件时需要打开当前Modile的bui ...
- Android RecyclerView SearchView基本用法1
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/82 背景: 做了很多年的app开发,貌似没见过没有搜索功能 ...
- RecyclerView解密篇(二)
在上一篇(RecyclerView解密篇(一))文章中简单的介绍了RecyclerView的基本用法,接下来要来讲讲RecyclerView的更多用法,要实现不同的功能效果,大部分都还是在于Recyc ...
- 一篇博客理解Recyclerview的使用
从Android 5.0开始,谷歌公司推出了RecylerView控件,当看到RecylerView这个新控件的时候,大部分人会首先发出一个疑问,recylerview是什么?为什么会有recyler ...
- Android RecyclerView使用详解(二)
在上一篇(RecyclerView使用详解(一))文章中简单的介绍了RecyclerView的基本用法,接下来要来讲讲RecyclerView的更多用法,要实现不同的功能效果,大部分都还是在于Recy ...
- RecyclerView 实现gallery画廊效果
1.RecyclerView的基本用法 首先主Activity的布局文件: [html] view plaincopy <RelativeLayout xmlns:android="h ...
- <Android基础>(三) UI开发 Part 3 RecyclerView
RecyclerView 1)RecyclerView的基本用法 2)横向滚动和瀑布流滚动 3)注册点击事件 3.6 强大的滚动控件 RecyclerView ListView缺点: 1.不使用技巧优 ...
随机推荐
- Flask--修改默认的static文件夹的方法
修改的flask默认的static文件夹只需要在创建Flask实例的时候,把static_folder和static_url_path参数设置为空字符串即可. app = Flask(__name__ ...
- asp.net 页面缓存、数据缓存
页面缓存,webform框架的aspx页面,服务器引擎生成的页面可以被缓存.
- 准确率(Precision),召回率(Recall)以及综合评价指标(F1-Measure)
准确率和召回率是数据挖掘中预测,互联网中得搜索引擎等经常涉及的两个概念和指标. 准确率:又称“精度”,“正确率” 召回率:又称“查全率” 以检索为例,可以把搜索情况用下图表示: 相关 不相关 检索 ...
- 【最小费用最大流】N. April Fools' Problem (medium)
http://codeforces.com/contest/802/problem/N [题解] 方法一: #include<bits/stdc++.h> using namespace ...
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...
- 【HDOJ5640】King's Cake(数论)
题意: 思路: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...
- Codeforces 559A(计算几何)
A. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- noip 2015 day1
T1 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1 ...
- uva 10559
记忆话搜索 DP 看了网上题解 状态方程真是巧妙 orz #include <cstdio> #include <cstdlib> #include <cmath> ...
- Codeforces 954 D Fight Against Traffic
Discription Little town Nsk consists of n junctions connected by m bidirectional roads. Each road co ...