StickyHeaderRecyclerView RecyclerView 悬浮吸顶 Header开源项目

我要开发同款
匿名用户2019年09月20日
145阅读
开发技术Java
所属分类AndroidUI组件、手机/移动开发、Android UI 组件
授权协议Apache-2.0

作品详情

RecyclerView悬浮吸顶Header,支持点击事件与状态绑定。

依赖compile'com.yuyang:stickyheaders:1.0.0'用法HeaderModel

HeaderModel需要实现 StickyHeaderModel 接口

publicclassHeaderItemimplementsStickyHeaderModel{publicfinalStringtitle;/***状态保存示例,如果header存在一些交互性行为,在onBindViewHolder里面需要绑定悬浮header的状态*/publicintcolor=0xff777777;publicHeaderItem(Stringtitle){this.title=title;}}publicclassItem{publicfinalStringtitle;publicfinalStringmessage;publicItem(Stringtitle,Stringmessage){this.title=title;this.message=message;}}Adapter

RecyclerViewAdapter需要实现 StickyHeaderProvider 接口,并在 getAdapterData() 返回model数据,用于判断对应position是否为Header

publicfinalclassRecyclerAdapterextendsRecyclerView.Adapter<RecyclerAdapter.BaseViewHolder>implementsStickyHeaderProvider{privatefinalList<Object>dataList=newArrayList<>();@OverridepublicBaseViewHolderonCreateViewHolder(ViewGroupparent,intviewType){if(viewType==0){returnnewItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false));}else{returnnewHeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view,parent,false));}}@OverridepublicvoidonBindViewHolder(finalBaseViewHolderholder,finalintposition){}@OverridepublicintgetItemCount(){returndataList.size();}@OverridepublicintgetItemViewType(intposition){returndataList.get(position)instanceofItem?0:1;}@OverridepublicList<?>getAdapterData(){returndataList;}privatestaticfinalclassItemViewHolderextendsBaseViewHolder{TextViewtitleTextView;TextViewmessageTextView;ItemViewHolder(ViewitemView){super(itemView);titleTextView=itemView.findViewById(R.id.tv_title);messageTextView=itemView.findViewById(R.id.tv_message);}}privatestaticfinalclassHeaderViewHolderextendsBaseViewHolder{TextViewtitleTextView;TextViewbutton;HeaderViewHolder(ViewitemView){super(itemView);titleTextView=itemView.findViewById(R.id.tv_title);button=itemView.findViewById(R.id.button);}}staticclassBaseViewHolderextendsRecyclerView.ViewHolder{BaseViewHolder(ViewitemView){super(itemView);}}}RecyclerViewRecyclerViewrecyclerView=findViewById(R.id.recycler_view);RecyclerAdapteradapter=newRecyclerAdapter();adapter.setDataList(genDataList(0));//StickyLinearLayoutManager替代LinearLayoutManagerStickyLinearLayoutManagerlayoutManager=newStickyLinearLayoutManager(this,adapter);recyclerView.setLayoutManager(layoutManager);recyclerView.setAdapter(adapter);
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论