RecyclerView的使用之HelloWorld

2019-12-10 18:49:45王冬梅

5:RecycleViewActivity.java

public class RecycleViewActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
//item 显示所需
private String[] title = {"Blog : http://www.easck.com/Leejizhou.",
"A good laugh and a long sleep are the best cures in the doctor's book.",
"all or nothing, now or never ",
"Be nice to people on the way up, because you'll need them on your way down.",
"Be confident with yourself and stop worrying what other people think. Do what's best for your future happiness!",
"Blessed is he whose fame does not outshine his truth.",
"Create good memories today, so that you can have a good past"
};
/**
* 图片资源版权归属于Smartisan.com
*/
private int[] pic = {R.mipmap.aa1, R.mipmap.aa0, R.mipmap.aa2, R.mipmap.aa3, R.mipmap.aa4, R.mipmap.aa5, R.mipmap.aa6};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle);
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
// 创建一个线性布局管理器
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//设置垂直滚动,也可以设置横向滚动
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//另外两种显示模式
// mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2)); Grid视图
// mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL)); 这里用线性宫格显示 类似于瀑布流
//RecyclerView设置布局管理器
mRecyclerView.setLayoutManager(layoutManager);
//RecyclerView设置Adapter
mRecyclerView.setAdapter(new RecyclerViewAdapter(this, title, pic));
}
}


注:相关教程知识阅读请移步到Android开发频道。