RecyclerView添加头脚布局,使用SwipeRefreshLayout的实现

本文介绍如何在Android中将ViewPager作为头部布局添加到RecyclerView,并结合SwipeRefreshLayout实现。通过包装类设计模式,创建了WrapRecyclerView和RecylerWrapAdapter,详细阐述了实现过程及代码示例。
摘要由CSDN通过智能技术生成

上篇文章记录了将viewpager抽取出来,那么这篇文章我们继续来实现,将viewpager做为头布局添加给RecyclerView。这使用到了一种设计模式 - 包装类设计模式,大家可以先了解下这个模式更有利于理解,我也会把本篇实现的思路给出个思维导图的。
看下效果图吧:
这里写图片描述

首先来个bean对象,等下封装数据使用:

public class Item {

    private String maintitle;
    private String subtitle;

    public Item(String maintitle, String subtitle){
        this.maintitle = maintitle;
        this.subtitle = subtitle;
    }

    public String getMaintitle() {
        return maintitle;
    }

    public String getSubtitle() {
        return subtitle;
    }
}

然后我们创建一个类WrapRecyclerView继承自RecyclerView:

// 将recycle进行封装
public class WrapRecyclerView extends RecyclerView {
   

    private ArrayList<View> mHeadViewList = new ArrayList<View>();
    private ArrayList<View> mFootViewList = new ArrayList<View>();
    private Adapter mAdapter;

    public WrapRecyclerView(Context context) {
        super(context);
    }

    public WrapRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WrapRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void addHeaderView(View view){
        mHeadViewList.clear();
        mHeadViewList.add(view);
        if (mAdapter != null){
            if (!(mAdapter instanceof RecylerWrapAdapter)){
                mAdapter = new RecylerWrapAdapter(mHeadViewList,mFootViewList,mAdapter);
            }
        }
    }

    public void addFootView(View view){
        mFootViewList.clear();
        mFootViewList.add(view);
        if (mAdapter != null){
            if (!(mAdapter instanceof RecylerWrapAdapter)){
                mAdapter = new RecylerWrapAdapter(mHeadViewList,mFootViewList,mAdapter);
            }
        }
    }

    @Override
    public void setAdapter(Adapter adapter) {
        if (mHeadViewList.isEmpty() && mFootViewList.isEmpty()){
            super.setAdapter(adapter);
        }else{
            adapter = new RecylerWrapAdapter(mHeadViewList,mFootViewList,adapter);
            super.setAdapter(adapter);
        }
        mAdapter = adapter;
    }
}
//在布局使用,使用系统的SwipeRefreshLayout包裹下
<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srl_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <com.wb.head.widget.WrapRecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    </android.
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以尝试使用以下代码来解决这个问题: 1. 在布局文件中添加 SwipeRefreshLayoutRecyclerView: ``` <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> ``` 2. 在代码中设置 RecyclerView布局管理器和适配器: ``` // 设置 RecyclerView布局管理器 recyclerView.layoutManager = LinearLayoutManager(this) // 设置 RecyclerView 的适配器 val adapter = MyAdapter(dataList) recyclerView.adapter = adapter ``` 3. 在代码中设置 SwipeRefreshLayout 的刷新监听器: ``` // 设置 SwipeRefreshLayout 的刷新监听器 swipeRefreshLayout.setOnRefreshListener { // 在这里执行刷新操作 // 刷新完成后调用 swipeRefreshLayout.isRefreshing = false swipeRefreshLayout.isRefreshing = false } ``` 至于图片高度异常的问题,您可以尝试使用 Glide 或 Picasso 等图片加载库来加载图片,并在布局文件中设置图片的宽高属性,例如: ``` <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="centerCrop" app:srcCompat="@drawable/image" /> ``` 其中,adjustViewBounds 属性可以让 ImageView 根据图片的宽高比例自动调整高度,scaleType 属性可以设置图片的缩放类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值