赞
踩
实现原理就是重写了适配器getItemViewType这个方法,作用是,根据当前是第几个条目,返回不同的布局,如果是网络数据的话,则判断网络数据和自己的条件返回不同的类型即可!!
RecyclerView实现列表不同布局,主要看RecyclerviewAdapter来实现,跟我一起做吧!!!
效果图:
首先给出布局文件 activity_main.cml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
第一个itme:item_one.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="2dp" android:layout_marginBottom="2dp"> <TextView android:id="@+id/one_text" android:layout_width="0dp" android:layout_height="match_parent" android:background="#ff8000" android:gravity="center" android:text="就想找个" android:textColor="#FFFFFF" app:layout_constraintEnd_toStartOf="@+id/textView" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="match_parent" android:background="#d240b0" android:gravity="center" android:text="女朋友" android:textColor="#FFFFFF" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/one_text" tools:ignore="MissingConstraints" tools:layout_editor_absoluteY="0dp" /> </android.support.constraint.ConstraintLayout>
第二个itme:item_two.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="100dp" android:background="#4c8ae6"> <ImageView android:layout_width="105dp" android:layout_height="match_parent" android:layout_marginTop="2dp" android:layout_marginEnd="8dp" android:layout_marginBottom="2dp" android:src="@mipmap/girl_friend1" app:layout_constraintEnd_toStartOf="@+id/imageView" tools:ignore="MissingConstraints" tools:layout_editor_absoluteY="0dp" /> <ImageView android:layout_width="105dp" android:layout_height="match_parent" android:layout_marginStart="8dp" android:src="@mipmap/girl_friend2" app:layout_constraintStart_toEndOf="@+id/imageView" tools:ignore="MissingConstraints" tools:layout_editor_absoluteY="0dp" /> <ImageView android:id="@+id/imageView" android:layout_width="105dp" android:layout_height="match_parent" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:src="@mipmap/girl_friend3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" tools:ignore="MissingConstraints" tools:layout_editor_absoluteY="0dp" /> </android.support.constraint.ConstraintLayout>
第三个itme:item_three.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_marginTop="2dp" android:layout_marginBottom="2dp" android:layout_height="100dp"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#a969ed" android:gravity="center" android:text="什么时候才能找到女朋友呀" android:textColor="#FFFFFF" /> </android.support.constraint.ConstraintLayout>
这里在代码中注释写好了,可以看得明白,不懂得点击左方QQ问我!
适配器中重写了onCreateViewHolder、onBindViewHolder、getItemCount、getItemViewType
onCreateViewHolder:创建布局,加载item
onBindViewHolder:绑定事件
getItemCount:总条目数量
getItemViewType:根据条件判断返回不同的int值,之后进入onCreateViewHolder方法中判断,返回的int值即 等于onCreateViewHolder中i的值,所以能够进行判断返回不同类型的布局!
package com.example.sj.recyclerviewexample; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定控件 initView(); //RecyclerView加载 intoRecyclerView(); } /** * 绑定控件 */ private void initView() { recyclerview = (RecyclerView) findViewById(R.id.recyclerview); } /** * RecyclerView加载 */ private void intoRecyclerView() { //Recycle布局方式 recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); //使用适配器 recyclerview.setAdapter(new RecyclerViewAdapter(this)); } }
RecyclerViewAdapter.java
package com.example.sj.recyclerviewexample; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { //三个final分别代表三个不同的布局 public static final int ITEMONE = 1; public static final int ITEMTWO = 2; public static final int ITEMTHREE = 3; //上下文 private Context context; /** * 构造方法 * * @param context */ public RecyclerViewAdapter(Context context) { this.context = context; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { //这时候就要根据这个i来判断加哪一个布局了 View inflate = null; RecyclerView.ViewHolder viewHolder = null; //根据i返回不同布局 switch (i) { case ITEMONE: inflate = LayoutInflater.from(context).inflate(R.layout.item_one, viewGroup, false); viewHolder = new OneItemHolder(inflate); break; case ITEMTWO: inflate = LayoutInflater.from(context).inflate(R.layout.item_two, viewGroup, false); viewHolder = new TwoItemHolder(inflate); break; case ITEMTHREE: inflate = LayoutInflater.from(context).inflate(R.layout.item_three, viewGroup, false); viewHolder = new ThreeItemHolder(inflate); break; } //返回布局 return viewHolder; } /** * 绑定控件,这里可以写一些事件方法等 * * @param viewHolder * @param i */ @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { //如果当前的 viewHolder 属于 OneItemHolder 则执行 if (viewHolder instanceof OneItemHolder) { //写绑定或这写事件可以如下 ((OneItemHolder) viewHolder).one_text.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "Toast就想找个女朋友", Toast.LENGTH_SHORT).show(); } }); } else if (viewHolder instanceof TwoItemHolder) { //此处省略。。。。 } else if (viewHolder instanceof ThreeItemHolder) { //此处省略。。。。 } } /** * 返回条目总数量,假设16个条目 * * @return */ @Override public int getItemCount() { return 16; } /** * 返回条目类型(这里就做个简单的判断区分) * * @param position 代表第几个条目 * @return */ @Override public int getItemViewType(int position) { if (position % 3 == 0) { return ITEMTHREE; } else if (position % 2 == 0) { return ITEMTWO; } else { return ITEMONE; } } /** * 第一个布局的Holder,要继承自RecyclerView.ViewHolder,这里你可以绑定控件 */ class OneItemHolder extends RecyclerView.ViewHolder { //举例 TextView one_text; public OneItemHolder(@NonNull View itemView) { super(itemView); one_text = itemView.findViewById(R.id.one_text); } } /** * 第二个布局的Holder,要继承自RecyclerView.ViewHolder,这里你可以绑定控件 */ class TwoItemHolder extends RecyclerView.ViewHolder { public TwoItemHolder(@NonNull View itemView) { super(itemView); } } /** * 第三个布局的Holder,要继承自RecyclerView.ViewHolder,这里你可以绑定控件 */ class ThreeItemHolder extends RecyclerView.ViewHolder { public ThreeItemHolder(@NonNull View itemView) { super(itemView); } } }
实现原理就是重写了适配器getItemViewType这个方法,作用是,根据当前是第几个条目,返回不同的布局,如果是网络数据的话,则判断网络数据和自己的条件返回不同的类型即可!!!
推荐文章:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。