RecyclerVIew實(shí)現(xiàn)懸浮吸頂效果
RecyclerVIew實(shí)現(xiàn)懸浮吸頂效果圖
這里寫圖片描述
主頁面布局
<?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", "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<StickyExampleAdapter.RecyclerViewHolder> {
//第一個(gè)吸頂
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);
}
}
//通過此處設(shè)置ContentDescription,作為內(nèi)容描述,可以通過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'
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android選擇圖片或視頻進(jìn)行循環(huán)播放
這篇文章主要為大家詳細(xì)介紹了Android選擇圖片或視頻進(jìn)行循環(huán)播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android中Intent組件的入門學(xué)習(xí)心得
Intent組件雖然不是四大組件,但卻是連接四大組件的橋梁,學(xué)習(xí)好這個(gè)知識,也非常的重要,下面這篇文章主要給大家介紹了關(guān)于Android中Intent組件的相關(guān)資料,需要的朋友可以參考下2021-12-12
Android之軟鍵盤自動(dòng)彈出和關(guān)閉【代碼分享】
本文主要介紹了Android中軟鍵盤自動(dòng)彈出和關(guān)閉的相關(guān)知識。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04
結(jié)合Windows窗口深入分析Android窗口的實(shí)現(xiàn)
在Android中,窗口是一個(gè)基本的圖形用戶界面元素,它提供了一個(gè)屏幕區(qū)域來放置應(yīng)用程序的用戶界面元素。窗口可以是全屏的,也可以是一個(gè)小的對話框。每個(gè)窗口都有一個(gè)特定的主題和樣式,可以根據(jù)應(yīng)用程序的需求進(jìn)行自定義2023-04-04
android中圖片的三級緩存cache策略(內(nèi)存/文件/網(wǎng)絡(luò))
實(shí)現(xiàn)圖片緩存也不難,需要有相應(yīng)的cache策略。這里我采用 內(nèi)存-文件-網(wǎng)絡(luò) 三層cache機(jī)制,其中內(nèi)存緩存包括強(qiáng)引用緩存和軟引用緩存(SoftReference),其實(shí)網(wǎng)絡(luò)不算cache,這里姑且也把它劃到緩存的層次結(jié)構(gòu)中2013-06-06
Android 仿抖音的評論列表的UI和效果的實(shí)現(xiàn)代碼
抖音是一款音樂創(chuàng)意短視頻社交軟件,此app已在android各大應(yīng)用商店和app store 上線。下面小編給大家?guī)砹薃ndroid 仿抖音的評論列表的UI和效果的實(shí)現(xiàn)代碼,感興趣的朋友參考下吧2018-03-03
解決Android自定義view獲取attr中自定義顏色的問題
這篇文章主要介紹了Android自定義view獲取attr中自定義顏色的問題解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的
這篇文章主要介紹了 Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的方法的相關(guān)資料,需要的朋友可以參考下2016-01-01

