当前位置:  首页  >  PHP资讯  >  业界资讯

RecyclerVIew实现悬浮吸顶效果

这篇文章主要为大家详细介绍了RecyclerVIew实现悬浮吸顶效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

RecyclerVIew实现悬浮吸顶效果图

这里写图片描述

主页面布局

 <&#63;xml version="1.0" encoding="utf-8"&#63;>  

RecyclerView的子条目布局

 <&#63;xml version="1.0" encoding="utf-8"&#63;>  

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 getData() { List 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", "name" + index, "gender" + index)); } else if (index <35) { stickyExampleModels.add(new StickyBean( "吸顶文本3", "name" + index, "gender" + index)); } else { stickyExampleModels.add(new StickyBean( "吸顶文本4", "name" + index, "gender" + index)); } } return stickyExampleModels; } } 

adapter代码

 public class StickyExampleAdapter extends RecyclerView.Adapter { //第一个吸顶 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 datas; StickyExampleAdapter(Context context, List 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 &#63; 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' } 

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

内容推荐:免费高清PNG素材下载
吐了个 "CAO" !
扫码关注 PHP1 官方微信号
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有 京ICP备19059560号-4