RecyclerVIew实现悬浮吸顶效果

RecyclerVIew实现悬浮吸顶效果,第1张

概述RecyclerVIew实现悬浮吸顶效果图这里写图片描述主页面布局<?xmlversion=\"1.0\"encoding=\"utf-8\"?>

RecyclerVIEw实现悬浮吸顶效果图

这里写图片描述

主页面布局

<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent">  <androID.support.v7.Widget.RecyclerVIEw    androID:ID="@+ID/recycle"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent" />  <TextVIEw    androID:ID="@+ID/tv_sticky_header_vIEw"    androID:layout_wIDth="match_parent"    androID:layout_height="50dp"    androID:background="#EFFAE7"    androID:gravity="center"    androID:text="吸顶文本1" />  <!--<include layout="@layout/layout_sticky_header_vIEw"/>--></FrameLayout>

RecyclerVIEw的子条目布局

<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:orIEntation="vertical">  <relativeLayout    androID:layout_marginleft="5dp"    androID:layout_marginRight="5dp"    androID:ID="@+ID/rl_content_wrapper"    androID:layout_wIDth="match_parent"    androID:layout_height="30dp">    <TextVIEw      androID:ID="@+ID/name"      androID:layout_centerVertical="true"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content" />    <TextVIEw      androID:ID="@+ID/auto"      androID:layout_centerVertical="true"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:layout_alignParentRight="true" />    <VIEw      androID:layout_wIDth="match_parent"      androID:layout_height="1dp"      androID:layout_alignParentBottom="true"      androID:background="#ffffff"/>  </relativeLayout>  <TextVIEw    androID:ID="@+ID/tv_sticky_header_vIEw"    androID:layout_wIDth="match_parent"    androID:layout_height="50dp"    androID:background="#EFFAE7"    androID:gravity="center"    androID:text="吸顶文本1" /></FrameLayout>

activity代码

public class MainActivity extends AppCompatActivity {  private TextVIEw tvStickyheaderVIEw;  private RecyclerVIEw recyclerVIEw;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    initVIEw();    initListener();  }  /**   * 初始化VIEw   */  private voID initVIEw() {    recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recycle);    tvStickyheaderVIEw = (TextVIEw) findVIEwByID(R.ID.tv_sticky_header_vIEw);    recyclerVIEw.setLayoutManager(new linearlayoutmanager(this));    recyclerVIEw.setAdapter(new StickyExampleAdapter(this,getData()));  }  /**   * 初始化Listener   */  private voID initListener() {    recyclerVIEw.addOnScrollListener(new RecyclerVIEw.OnScrollListener() {      @OverrIDe      public voID onScrolled(RecyclerVIEw recyclerVIEw,int dx,int dy) {        super.onScrolled(recyclerVIEw,dx,dy);        VIEw stickvIEw = recyclerVIEw.findChildVIEwUnder(0,0);        if (stickvIEw != null && stickvIEw.getContentDescription() != null) {          if (!TextUtils.equals(tvStickyheaderVIEw.getText(),stickvIEw.getContentDescription())) {            tvStickyheaderVIEw.setText(stickvIEw.getContentDescription());          }        }        VIEw transInfoVIEw = recyclerVIEw.findChildVIEwUnder(            0,tvStickyheaderVIEw.getHeight() + 1);        if (transInfoVIEw.getTag() != null) {          int transVIEwStatus = (int) transInfoVIEw.getTag();          int top = transInfoVIEw.gettop();          if (transVIEwStatus == StickyExampleAdapter.HAS_STICKY_VIEW) {            if (top > 0) {              int dealtY = top - tvStickyheaderVIEw.getMeasuredHeight();              tvStickyheaderVIEw.setTranslationY(dealtY);            } else {              tvStickyheaderVIEw.setTranslationY(0);            }          } else if (transVIEwStatus == StickyExampleAdapter.NONE_STICKY_VIEW) {            tvStickyheaderVIEw.setTranslationY(0);          }        }      }    });  }  public List<StickyBean> getData() {    List<StickyBean> stickyExampleModels = new ArrayList<>();    for (int index = 0; index < 100; index++) {      if (index < 15) {        stickyExampleModels.add(new StickyBean(            "吸顶文本1","name" + index,"gender" + index));      } else if (index < 25) {        stickyExampleModels.add(new StickyBean(            "吸顶文本2","gender" + index));      } else if (index < 35) {        stickyExampleModels.add(new StickyBean(            "吸顶文本3","gender" + index));      } else {        stickyExampleModels.add(new StickyBean(            "吸顶文本4","gender" + index));      }    }    return stickyExampleModels;  }}

adapter代码

public class StickyExampleAdapter extends RecyclerVIEw.Adapter<StickyExampleAdapter.RecyclerVIEwHolder> {  //第一个吸顶  private static final int FirsT_STICKY_VIEW = 1;  //别的吸顶  static final int HAS_STICKY_VIEW = 2;  //正常VIEw  static final int NONE_STICKY_VIEW = 3;  private final LayoutInflater mInflate;  private final List<StickyBean> datas;  StickyExampleAdapter(Context context,List<StickyBean> datas){    mInflate = LayoutInflater.from(context);    this.datas = datas;  }  @OverrIDe  public RecyclerVIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {    VIEw inflate = mInflate.inflate(R.layout.item_ui,parent,false);    return new RecyclerVIEwHolder(inflate);  }  @OverrIDe  public voID onBindVIEwHolder(RecyclerVIEwHolder holder,int position) {    StickyBean stickyBean = datas.get(position);    holder.tvname.setText(stickyBean.name);    holder.tvGender.setText(stickyBean.autor);    if (position == 0) {      holder.tvStickyheader.setVisibility(VIEw.VISIBLE);      holder.tvStickyheader.setText(stickyBean.sticky);      holder.itemVIEw.setTag(FirsT_STICKY_VIEW);    } else {      if (!TextUtils.equals(stickyBean.sticky,datas.get(position - 1).sticky)) {        holder.tvStickyheader.setVisibility(VIEw.VISIBLE);        holder.tvStickyheader.setText(stickyBean.sticky);        holder.itemVIEw.setTag(HAS_STICKY_VIEW);      } else {        holder.tvStickyheader.setVisibility(VIEw.GONE);        holder.itemVIEw.setTag(NONE_STICKY_VIEW);      }    }    //通过此处设置ContentDescription,作为内容描述,可以通过getContentDescription取出,功效跟setTag差不多。    holder.itemVIEw.setContentDescription(stickyBean.sticky);  }  @OverrIDe  public int getItemCount() {    return datas == null ? 0 : datas.size();  }  public class RecyclerVIEwHolder extends RecyclerVIEw.VIEwHolder{    TextVIEw tvStickyheader;    relativeLayout rlContentWrapper;    TextVIEw tvname;    TextVIEw tvGender;    RecyclerVIEwHolder(VIEw itemVIEw) {      super(itemVIEw);      tvStickyheader = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_sticky_header_vIEw);      rlContentWrapper = (relativeLayout) itemVIEw.findVIEwByID(R.ID.rl_content_wrapper);      tvname = (TextVIEw) itemVIEw.findVIEwByID(R.ID.name);      tvGender = (TextVIEw) itemVIEw.findVIEwByID(R.ID.auto);    }  }}

StickyBean代码

public class StickyBean {  public String name;  public String autor;  public String sticky;  public StickyBean(String sticky,String name,String autor) {    this.sticky = sticky;    this.name = name;    this.autor = autor;  }}

app的build文件

apply plugin: 'com.androID.application'androID {  compileSdkVersion 26  buildToolsversion "25.0.3"  defaultConfig {    applicationID "com.lg.floating"    minSdkVersion 15    targetSdkVersion 26    versionCode 1    versionname "1.0"    testInstrumentationRunner "androID.support.test.runner.AndroIDJUnitRunner"  }  buildTypes {    release {      MinifyEnabled false      proguardfiles getDefaultProguardfile('proguard-androID.txt'),'proguard-rules.pro'    }  }}dependencIEs {  compile filetree(dir: 'libs',include: ['*.jar'])  androIDTestCompile('com.androID.support.test.espresso:espresso-core:2.2.2',{    exclude group: 'com.androID.support',module: 'support-annotations'  })  compile 'com.androID.support:appcompat-v7:26.0.0-Alpha1'  compile 'com.androID.support:recyclervIEw-v7:23.1.0'  testCompile 'junit:junit:4.12'}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的RecyclerVIew实现悬浮吸顶效果全部内容,希望文章能够帮你解决RecyclerVIew实现悬浮吸顶效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址: http://outofmemory.cn/web/1144551.html

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

发表评论

登录后才能评论

评论列表(0条)

保存