Android v7使用 RecyclerView ¬----之一简单实现

先上效果图,但愿大伙能用的上

分享代码:http://yunpan.cn/c69F9FiLTxHJC  访问密码 bf9c


 

RecyclerView是一种新的视图组件,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListViewGridView控件的继承者,在最新的support-V7版本中提供支持。

在开发RecyclerView时充分考虑了扩展性,因此用它可以创建想到的任何种类的的布局。但在使用上也稍微有些不便。这就是Android——要完成一件事情总不是那么容易。

整体上看RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManagerItemDecoration , 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动画效果
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵手生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值