Android v7使用 RecyclerView ¬----之一简单实现
来源:互联网 发布:读取acess数据分析统计 编辑:程序博客网 时间:2024/06/11 06:35
先上效果图,但愿大伙能用的上
分享代码:http://yunpan.cn/c69F9FiLTxHJC 访问密码 bf9c
RecyclerView是一种新的视图组件,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListView和GridView控件的继承者,在最新的support-V7版本中提供支持。
在开发RecyclerView时充分考虑了扩展性,因此用它可以创建想到的任何种类的的布局。但在使用上也稍微有些不便。这就是Android——要完成一件事情总不是那么容易。
整体上看RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现令人瞠目的效果。
先上一下效果图2
1:创建工程CzgRecylerView
2:项目中引入RecyclerView库
Build.gradle文件中加入
compile 'com.android.support:recyclerview-v7:23.3.0'
或图形化操作如下:
3:定义数据实体类 – CellData
package czg.com.czgrecyclerview.Bean; /** * Created by Administrator on 2016/8/7. */ public class CellData { public String title = "默认标题"; public String content = "默认内容说明"; public int icon = -1; public CellData(String title, String content) { this.title = title; this.content = content; } public CellData() { this.title = "默认标题"; this.content = "默认内容说明"; this.icon = -1; }}
4:定义自定义资源项目item_recycler_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:id="@+id/iv_Icon" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="标题" android:id="@+id/tv_title" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容" android:id="@+id/tv_content" /></LinearLayout>
4:自定义MyAdapter继承RecyclerView.Adapter
package czg.com.czgrecyclerview; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import czg.com.czgrecyclerview.Bean.CellData; /** * Created by Administrator on 2016/8/7. */ class MyAdapter extends RecyclerView.Adapter { //创建一个ViewHolder类继承 class ViewHolder_czg extends RecyclerView.ViewHolder { private View root; private TextView tv_title,tv_content; private ImageView im_Icon; public ViewHolder_czg(View root) { super(root); tv_content = (TextView) root.findViewById(R.id.tv_content); tv_title = (TextView) root.findViewById(R.id.tv_title); im_Icon = (ImageView) root.findViewById(R.id.iv_Icon); } public TextView getTv_content() { return tv_content; } public TextView getTv_title() { return tv_title; } public ImageView getIm_Icon() { return im_Icon; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { return new ViewHolder_czg(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_recycler_view,null )); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolder_czg vh_czg = (ViewHolder_czg) holder; //vh_czg.getTv().setText("这是RecyclerView的ViewHolder中的Text控件"+position); CellData cd = data[position]; vh_czg.getTv_title().setText(cd.title); vh_czg.getTv_content().setText(cd.content); vh_czg.getIm_Icon().setImageResource(R.mipmap.ic_launcher); } @Override public int getItemCount() { return data.length; } private CellData[] data =new CellData[]{ new CellData("标题一","内容一"),new CellData("标题2","内容2"),new CellData(), new CellData("标题3","内容3"),new CellData("标题4","内容4"),new CellData(), new CellData("标题5","内容5"),new CellData("标题6","内容6"),new CellData(), new CellData("标题7","内容7"),new CellData("标题8","内容8"),new CellData(), new CellData("标题9","内容9"),new CellData("标题10","内容10"),new CellData(), new CellData("标题11","内容11"),new CellData("标题12","内容12"),new CellData(), new CellData("标题13","内容13"),new CellData("标题14","内容14"),new CellData(), new CellData("标题15","内容15"),new CellData("标题16","内容16"),new CellData() };}
5:在MainActivity中实现RecyclerView的各类布局
package czg.com.czgrecyclerview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; public class MainActivity extends AppCompatActivity { private RecyclerView rv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); rv = new RecyclerView(this); // setContentView(R.layout.activity_main); setContentView(rv); //rv.setLayoutManager(new LinearLayoutManager(this)); //使用线性布局 //rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)); //使用线性布局,第3个参数表示翻转 //rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true)); //使用线性布局,第3个参数表示反转// rv.setLayoutManager(new GridLayoutManager(this,3)); //使用表格布局 3列 rv.setLayoutManager(new GridLayoutManager(this,4,LinearLayoutManager.HORIZONTAL,false)); //使用表格布局 水平方向4行,不翻转 //为RecyclerView 填充内容 ===创建一个Adapter rv.setAdapter(new MyAdapter()); } }
6:实现RecyclerView动画效果
0 0
- Android v7使用 RecyclerView ¬----之一简单实现
- Android V7 RecyclerView 使用完全解析
- Android-support-v7-RecyclerView的使用
- android.support.v7.widget.RecyclerView的使用
- android v7兼容包RecyclerView的使用
- Android RecyclerView简单使用
- android recyclerView简单使用
- android v7兼容包RecyclerView的使用(一)
- android v7兼容包RecyclerView的使用(二)
- Android简单的实现RecyclerView
- Android RecyclerView 的简单使用
- Android RecyclerView使用简单介绍
- android RecyclerView的简单使用
- 【Android】recyclerView-1-简单使用
- Android RecyclerView 的简单使用
- Android 笔记-RecyclerView简单使用
- 关于RecyclerView使用报错Didn't find class "android.support.v7.recyclerview.R$styleabl
- 使用RecyclerView出错: Error inflating class android.support.v7.widget.RecyclerView
- 常用类
- spark读取mysql数据库的驱动问题
- POJ 2251--Dungeon Master
- Android中.9图片快捷的制作
- socket python
- Android v7使用 RecyclerView ¬----之一简单实现
- 设计模式总结之Visitor Pattern(访问者模式)
- 只是一些笔记
- QPPS 工程
- 为什么中国的程序员总被称为码农?
- MSP430常见问题解答
- POJ 1011 Sticks(搜索+剪枝)
- 空指针(NULL)那些事?
- GSM/GPRS/EDGE简介