RecyclerView的使用之多种Item加载布局

2019-12-10 18:49:12王旭

OK,Adapter建立好了,那么最后一步就是在Activity里面进行相关操作

6:列表页面的类文件 RecyclerViewActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
/**
* Created by Lijizhou on 2016/2/21.
*/
public class RecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
//item 显示所需(仅供DEMO)
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"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);
mRecyclerView=(RecyclerView)findViewById(R.id.rv_list);
//这里根据上一个页面的传入值来加载LIST或GRID,上一个页面仅仅2个按钮,参考演示DEMO
if (getIntent().getIntExtra("type", 0) == 1){
//List
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
}else if(getIntent().getIntExtra("type", 0) == 2){
//grid
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
}
//RecyclerView设置Adapter
mRecyclerView.setAdapter(new RecyclerViewAdapter(this, title));
}
}

Ok,这样RecyclerView的多Item布局的加载就实现,关于RecyclerView的使用之多种Item加载布局就给大家介绍这么多,希望对大家有所帮助!



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