如何优雅的实现recycleview加入顶部headerview,实现一起滑动效果

如何优雅的实现recycleview加入顶部headerview,实现一起滑动效果,第1张

 (1)在adapter中定义两个标志量,分别代表header布局和普通布局:

//header布局标志

private static final int TYPE_HEADER=0

//普通布局标志

public static final int TYPE_NORMAL=1

(2)然后通过get构造器让我们可以通过adapter直接调用构造器直接设置headerview:

//headerview的布局

private View mHeaderView

public View getmHeaderView() {

return mHeaderView

}

public void setmHeaderView(View mHeaderView) {

this.mHeaderView = mHeaderView

notifyItemInserted(0)

}

(3)接下来重写getviewtype方法,判断是否有headerview,根据是否有headerview返回不同的标志位;

@Override

public int getItemViewType(int position) {

if(mHeaderView==null) return TYPE_NORMAL

if(position==0)return TYPE_HEADER//将header插入到顶部

return TYPE_NORMAL

}

(4)在onCreateViewHolder根据不同的类型返回不同的viewholder:

@Override

public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

if(mHeaderView!=null&&viewType==TYPE_HEADER) return new BaseViewHolder(mHeaderView,mHeaderView,null)

View mView = null

mView = mInflater.inflate(itemViewID, null, false)

return new BaseViewHolder(mView, mHeaderView,monItemClickListener)

}

(5)最后在onBindViewHolder方法中根据不同的类型返回不同的布局,

@Override

public void onBindViewHolder(BaseViewHolder holder, int position) {

if(getItemViewType(position)==TYPE_HEADER) return

bindData(holder, position)

}

(6)最后在baseviewholder中判断itemview是否为headerview,然后让adapter加载不同的viewholder

public BaseViewHolder(View itemView,View mHeaderView, BaseAdapter.onItemClickListener onItemClickListener) {

super(itemView)

this.mHeaderView=mHeaderView

if(itemView==mHeaderView) return

mViews = new SparseArray<>()

this.monItemClickListener = onItemClickListener

itemView.setOnClickListener(this)

}

接下来讲addheaderview最关键的部分了,小伙伴们看好了--

首先添加依赖库:

compile 'com.bartoszlipinski.recyclerviewheader:library:1.2.0'

compile 'com.android.support:recyclerview-v7:23.0.1'

接下来实现布局文件编写:<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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">

<android.support.v7.widget.RecyclerView

android:id="@+id/recycler"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center_horizontal|top" />

<com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader

android:id="@+id/header"

android:layout_width="match_parent"

android:layout_height="100dp"

android:layout_gravity="center_horizontal|top">

<ImageView

android:layout_width="match_parent"

android:layout_height="200dp"

android:src="@mipmap/ic_launcher"/>

</com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader>

</FrameLayout>最后只需要在代码中设置完adapter之后,调用attachto方法即可实现了核心代码就这么点:header = (RecyclerViewHeader) findViewById(R.id.header)

recyclerView = (RecyclerView) findViewById(R.id.recycler)

mAdapter = new MyAdapter(this, mDatas, R.layout.template_text)

recyclerView.setAdapter(mAdapter)

recyclerView.setLayoutManager(new LinearLayoutManager(this))

header.attachTo(recyclerView, true)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11254777.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存