环境
studio中需要导入recyclerview的包
使用
范例1:存放数据
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = new RecyclerView(this);//1、创建一个recyclerview
setContentView(R.layout.activity_main);
recyclerView.setAdapter(new RecyclerView.Adapter() {//2.位recyclerview添加adapter
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ViewHolder(new TextView(viewGroup.getContext()));//4.创建viewholder
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {//这里可以对控件赋值,第二个参数是索引
ViewHolder vh =(ViewHolder)viewHolder;
vh.getTv().setText("Item"+i);
}
@Override
public int getItemCount() {
return 10;
}
//3创建viewholder
class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv;
public ViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView;
}
public TextView getTv(){
return tv;
}
}
});
}
}
范例2:存数组中数据
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = new RecyclerView(this);//1、创建一个recyclerview
setContentView(R.layout.activity_main);
recyclerView.setAdapter(new RecyclerView.Adapter() {//2.位recyclerview添加adapter
private String[] data = new String[]{"hello","jike","nihao","ds","dwe"};
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ViewHolder(new TextView(viewGroup.getContext()));//4.创建viewholder
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {//这里可以对控件赋值,第二个参数是索引
ViewHolder vh =(ViewHolder)viewHolder;
vh.getTv().setText(data[i]);
}
@Override
public int getItemCount() {
return data.length;
}
//3创建viewholder
class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv;
public ViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView;
}
public TextView getTv(){
return tv;
}
}
});
}
}
范例3:使用资源文件自定义列表项
mainactivity:
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = new RecyclerView(this);//1、创建一个recyclerview
setContentView(recyclerView );
recyclerView.setLayoutManager(new LinearLayoutManager(this));//一定要写manager
recyclerView.setAdapter(new MyAdapter());
}
}
adapter:
package com.example.myapplication;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by 董梦娇 on 2015/11/8.
*/
class MyAdapter extends RecyclerView.Adapter { //2.位recyclerview添加adapter
//数据初始化
private CellData[] data = new CellData[]{new CellData("title1","content1"),new CellData("title1","content1"),new CellData("title1","content1"),new CellData("title2","content2"),new CellData("title3","content3")};
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item,null));//4.利用布局解析器传入布局
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {//这里可以对控件赋值,第二个参数是索引
ViewHolder vh = (ViewHolder) viewHolder;
CellData cd = data[i];
vh.getTvTitle().setText(cd.getTitle());
vh.getTvContent().setText(cd.getContent());
}
@Override
public int getItemCount() {
return data.length;
}
//3创建viewholder
class ViewHolder extends RecyclerView.ViewHolder {
private View root;//1.view定义
private TextView tvTitle,tvContent;
public ViewHolder(View root) {//传过来的布局就是哪个layout_item
super(root);
//获取控件
tvTitle= (TextView) root.findViewById(R.id.tv_title);
tvContent = (TextView) root.findViewById(R.id.tv_content);
}
public TextView getTvContent() {//两个方法来获取title和content
return tvContent;
}
public TextView getTvTitle() {
return tvTitle;
}
}
}
数据:
package com.example.myapplication;
/**
* Created by 董梦娇 on 2015/11/8.
*/
public class CellData {
private String title;
private String content;
public CellData(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
范例:瀑布流
main:
package com.example.myapplication;
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;
import android.support.v7.widget.StaggeredGridLayoutManager;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<CellData> datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
//设置布局
// recyclerView.setLayoutManager(new LinearLayoutManager(this));//一定要写manager
//通过StaggeredGridLayoutManager设置两列瀑布流
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
//初始化数据
init();
recyclerView.setAdapter(new MyAdapter(datas));
}
/**
*初始化数据
*/
private void init() {
datas = new ArrayList<>();
for (int i=0;i<10;i++) {
CellData data = new CellData(R.mipmap.baby);
datas.add(data);
CellData data1 = new CellData(R.mipmap.betifel);
datas.add(data1);
CellData data2 = new CellData(R.mipmap.fanbingbing1);
datas.add(data2);
CellData data3 = new CellData(R.mipmap.fanbingbing3);
datas.add(data3);
CellData data4 = new CellData(R.mipmap.gaoyuanyuan);
datas.add(data4);
}
}
}
adapter:
package com.example.myapplication;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by 董梦娇 on 2015/11/8.
*/
class MyAdapter extends RecyclerView.Adapter { //2.位recyclerview添加adapter
private List<CellData> data;
public MyAdapter(List<CellData> data) {
this.data = data;
}
//数据初始化
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item,null));//4.利用布局解析器传入布局
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {//这里可以对控件赋值,第二个参数是索引
ViewHolder vh = (ViewHolder) viewHolder;
CellData cd = data.get(i);
vh.getImageView().setImageResource(cd.getImgId());
// Log.d("log","控件值"+cd.getTitle()+";"+cd.getContent());
}
@Override
public int getItemCount() {
return data.size();
}
//3创建viewholder
class ViewHolder extends RecyclerView.ViewHolder {
private View root;//1.view定义
private ImageView imageView;
public ViewHolder(View root) {//传过来的布局就是哪个layout_item
super(root);
// this.root = root;
//获取控件
imageView = (ImageView) root.findViewById(R.id.img_recycler);
}
public ImageView getImageView() {
return imageView;
}
}
}
数据:
package com.example.myapplication;
/**
* Created by 董梦娇 on 2015/11/8.
*/
public class CellData {
private int imgId;
public CellData(int imgId) {
this.imgId = imgId;
}
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
}
效果: