赞
踩
内容:同一个RecyclerView包含多种布局。本例设置了三个不同的布局样式,都在同一个RecyclerView中展示出来。
尾末简要介绍了下GridLayoutManager的setSpanSizeLookup()方法
效果如下:
步骤:
1、定义三种不同布局,用于填充到RecyclerView中
2、加入RecyclerView控件,设置好adapter和数据
3、新建类继承RecyclerView.Adapter<RecyclerView.ViewHolder>,注意:<RecyclerView.ViewHolder>,此处不是具体的viewHoler
4、重写getItemViewType(int position),这里设置好视图类型,后面会根据此处的返回类型去加载对应的布局
5、onCreateViewHolder(ViewGroup parent, int viewType)根据viewType填充不同布局
6、onBindViewHolder(RecyclerView.ViewHolder holder, final int position)根据holder类型设置数据填充
代码如下:
1、填充布局不贴出,就一个TextView宽高颜色不一样
2、加入RecyclerView控件,设置好adapter和数据
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/linearLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <android.support.v7.widget.RecyclerView
- android:id="@+id/recyclerView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
-
- </LinearLayout>
- public class MainActivity extends AppCompatActivity{
-
- private String[] mStrings = {"1", "2", "3", "3", "2", "1", "1", "3", "2", "3", "1", "3"};
- private List<String> data = new ArrayList<>();
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- data = Arrays.asList(mStrings);
- RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
- recyclerView.setLayoutManager(new LinearLayoutManager(this));
- recyclerView.setAdapter(new MyAdapter(this, data));
- }
- }
3、4、5、6
- public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
-
- private Context mContext;
- private List<String> content;
-
- public MyAdapter(List<String> content, Context context) {
- this.content = content;
- mContext = context;
- }
-
- public MyAdapter(Context context, List<String> content) {
- mContext = context;
- this.content = content;
- }
-
- /**
- * 根据position位置(或者自己需求)设置不同视图类型
- */
- @Override
- public int getItemViewType(int position) {
- switch (content.get(position)) {
- case "1":
- return 1;
- case "2":
- return 2;
- default:
- return 3;
- }
- }
-
- /**
- * @param parent
- * @param viewType
- * @return 根据viewType设置不同的布局 从而实现多布局
- */
- @Override
- public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- RecyclerView.ViewHolder viewHolder;
- View view;
-
- //根据不同的类型加载对应的布局
- switch (viewType) {
- case 1:
- view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_1, parent, false);
- viewHolder = new FirstHolder(view);
- return viewHolder;
- case 2:
- view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_2, parent,false);
- viewHolder = new SecondHolder(view);
- return viewHolder;
- case 3:
- view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_3, parent,false);
- viewHolder = new ThirdHolder(view);
- return viewHolder;
- default:
- return null;
- }
- }
-
- /**
- * @param holder
- * @param position 根据holder设置不同的数据
- */
- @Override
- public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
- if (holder instanceof FirstHolder) {
- ((FirstHolder) holder).mTextView.setText("FirstHolder");
- ((FirstHolder) holder).mTextView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- toast("FirstHolder pos:" + position);
- }
- });
- } else if (holder instanceof SecondHolder) {
- ((SecondHolder) holder).mTextView.setText("SecondHolder");
- ((SecondHolder) holder).mTextView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- toast("SecondHolder pos:" + position);
- }
- });
- }else if (holder instanceof ThirdHolder) {
- ((ThirdHolder) holder).mTextView.setText("ThirdHolder");
- ((ThirdHolder) holder).mTextView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- toast("ThirdHolder pos:" + position);
- }
- });
- }
-
- }
-
- @Override
- public int getItemCount() {
- return content.size();
- }
-
- private void toast(String string) {
- Toast.makeText(mContext, string, Toast.LENGTH_SHORT).show();
- }
-
- public static class FirstHolder extends RecyclerView.ViewHolder {
- public TextView mTextView;
-
- public FirstHolder(View itemView) {
- super(itemView);
- mTextView = itemView.findViewById(R.id.tv_1);
- }
- }
-
- public static class SecondHolder extends RecyclerView.ViewHolder{
- public TextView mTextView;
-
- public SecondHolder(View itemView) {
- super(itemView);
- mTextView = itemView.findViewById(R.id.tv_2);
- }
- }
-
- public static class ThirdHolder extends RecyclerView.ViewHolder{
- public TextView mTextView;
-
- public ThirdHolder(View itemView) {
- super(itemView);
- mTextView = itemView.findViewById(R.id.tv_3);
- }
- }
- }
Kotlin代码如下:
- class MainActivity : AppCompatActivity() {
-
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
-
- val data = listOf<String>("1", "2", "3", "3", "2", "1", "1", "3", "2", "3", "1", "3")
- recyclerView.layoutManager = LinearLayoutManager(this)
- recyclerView.adapter = MyAdapter(this, data)
- }
- }
- class MyAdapter(private val context: Context, private val data: List<String>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
-
- override fun getItemViewType(position: Int): Int {
- when (data[position]) {
- "1" -> return 1
- "2" -> return 2
- else -> return 3
- }
- }
-
- override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder? {
- val viewHolder: RecyclerView.ViewHolder
- val view: View
-
- //根据不同的类型加载对应的布局
- when (viewType) {
- 1 -> {
- view = LayoutInflater.from(parent?.getContext()).inflate(R.layout.item_1, parent, false)
- viewHolder = FirstHolder(view)
- return viewHolder
- }
- 2 -> {
- view = LayoutInflater.from(parent?.getContext()).inflate(R.layout.item_2, parent, false)
- viewHolder = SecondHolder(view)
- return viewHolder
- }
- 3 -> {
- view = LayoutInflater.from(parent?.getContext()).inflate(R.layout.item_3, parent, false)
- viewHolder = ThirdHolder(view)
- return viewHolder
- }
- else -> return null
- }
- }
-
-
- override fun getItemCount(): Int {
- return data.size
- }
-
- override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
- if (holder is FirstHolder) {
- holder.mTextView.text = "FirstHolder"
- holder.mTextView.setOnClickListener { toast("FirstHolder pos:$position") }
- } else if (holder is SecondHolder) {
- holder.mTextView.text = "SecondHolder"
- holder.mTextView.setOnClickListener { toast("SecondHolder pos:$position") }
- } else if (holder is ThirdHolder) {
- holder.mTextView.text = "ThirdHolder"
- holder.mTextView.setOnClickListener { toast("ThirdHolder pos:$position") }
- }
- }
-
- private fun toast(string: String) {
- Toast.makeText(context, string, Toast.LENGTH_SHORT).show()
- }
-
- class FirstHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
- var mTextView: TextView
-
- init {
- mTextView = itemView.findViewById(R.id.tv_1)
- }
- }
-
- class SecondHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
- var mTextView: TextView
-
- init {
- mTextView = itemView.findViewById(R.id.tv_2)
- }
- }
-
- class ThirdHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
- var mTextView: TextView
-
- init {
- mTextView = itemView.findViewById(R.id.tv_3)
- }
- }
- }
另一种是可以快速修改GridLayoutManager 布局 图片展示个数,通过设置setSpanSizeLookup()
代码如下:
- RecyclerView recylerview = (RecyclerView) findViewById(R.id.recylerview);
- GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
- gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
- @Override
- public int getSpanSize(int i) {
- if (i == 0 || i == 3 ){
- return 2;
- }else {
- return 1;
- }
- }
- });
- recylerview.setLayoutManager(gridLayoutManager);
- GridAdapter adapter = new GridAdapter();
- recylerview.setAdapter(adapter);
注意:getSpanSize return 的数量不能大于 你 new GridLayoutManager(this, 2);的数量
否则会报错
java.lang.IllegalArgumentException: Item at position 0 requires 3 spans but GridLayoutManager has only 2 spans.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。