recyclerview多布局实现

安卓 Android Recyclerview多布局适配
开始
实现目标
代码
布局文件
java文件
点个赞吧,给个关注
开始

好久没有写博客了,不知道会不会有人看见,最近忙着上线项目和维护,哎《《《加班

实现原理就是重写了适配器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”>

</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”>

</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”>

</android.support.constraint.ConstraintLayout>

java文件
这里在代码中注释写好了,可以看得明白,不懂得点击左方QQ问我!
适配器中重写了onCreateViewHolder、onBindViewHolder、getItemCount、getItemViewType

onCreateViewHolder:创建布局,加载item
onBindViewHolder:绑定事件
getItemCount:总条目数量
getItemViewType:根据条件判断返回不同的int值,之后进入onCreateViewHolder方法中判断,返回的int值即 等于onCreateViewHolder中i的值,所以能够进行判断返回不同类型的布局!

MainActivity.java

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这个方法,作用是,根据当前是第几个条目,返回不同的布局,如果是网络数据的话,则判断网络数据和自己的条件返回不同的类型即可!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值