Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)
實(shí)現(xiàn)效果:
先看下效果:需求是 滑動(dòng)列表 ,其中一部分視圖(粉絲數(shù),關(guān)注數(shù)這一部分)在滑動(dòng)到頂端的時(shí)候不消失,而是停留在整個(gè)界面頭部。
我們先分析要解決的問題:
1、如何實(shí)現(xiàn)列表ListView頂部視圖跟隨ListView一起滑動(dòng)
2、如何實(shí)現(xiàn)滑動(dòng)過程中需要停留在頂部的視圖
解決:
第一個(gè)問題,實(shí)現(xiàn)ListView與頂部視圖一起滑動(dòng),ListView提供一個(gè)方法,addHeadView(View); 意思就是在ListView頂部添加一個(gè)View。那么這個(gè)View就能和ListView一起滾動(dòng)。
第二個(gè)問題,怎么保證界面中間的某一部分視圖滑動(dòng)到頂部的時(shí)候停留在頂部呢?
首先我們這個(gè)停留在頂部的View(稱為View1)是ListView.addHeadView()上去的,即滑動(dòng)列表,這個(gè)View1會(huì)劃出去,那么如何讓它不劃出去?只要在Listview所在布局最上方
也寫一個(gè)同樣的View(稱為View2,View2和ListView同屬于一個(gè)FragmentLayout)并先隱藏(Visible = 'gone'),當(dāng)View1剛劃出頂部的時(shí)候,View2顯示。
View1剛劃出頂部的時(shí)機(jī)就是:
當(dāng)滑動(dòng)時(shí) firstVisibleItem>=要懸浮的 item的position時(shí) 讓View2顯示 ,否則隱藏。
代碼實(shí)現(xiàn):
首先布局文件:
頭部布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="wrap_content" 6 android:gravity="center" 7 > 8 <ImageView 9 android:layout_width="match_parent" 10 android:layout_height="90dp" 11 android:src="@mipmap/p1" 12 android:scaleType="fitXY" 13 /> 14 </LinearLayout>
要停留在頂部的View布局:(這里是要停留在頂部的View,這里addHeadView到ListView頂部,跟隨者ListView滑動(dòng)到頂部消失,這時(shí)滿足firstVisibleItem>=要懸浮的 item的position條件,主界面里在寫一個(gè)相同的View顯示即可)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" > <ImageView android:layout_width="match_parent" android:layout_height="50dp" android:src="@mipmap/p2" android:scaleType="fitXY" /> </LinearLayout> 復(fù)制代碼
主布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center" android:text="個(gè)人中心" android:textSize="20dp" /> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/invis" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:visibility="gone" > <ImageView android:layout_width="match_parent" android:layout_height="50dp" android:src="@mipmap/p2" android:scaleType="fitXY" /> </LinearLayout> </FrameLayout> </LinearLayout>
java代碼:
private LinearLayout invis; private ListView lv; String[] strs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); invis = (LinearLayout) findViewById(R.id.invis); strs = new String[100]; for (int i = 0; i < 20; i++) { strs[i] = "data-----" + i; } lv = (ListView) findViewById(R.id.lv); View header = View.inflate(this, R.layout.stick_header, null);//頭部?jī)?nèi)容 lv.addHeaderView(header);//添加頭部 lv.addHeaderView(View.inflate(this, R.layout.stick_action, null));//ListView條目中的懸浮部分 添加到頭部 lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs)); lv.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem >= 1) { invis.setVisibility(View.VISIBLE); } else { invis.setVisibility(View.GONE); } } }); }
總結(jié)
到此這篇關(guān)于Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Android ListView懸浮頭部展現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中Notification用法實(shí)例總結(jié)
這篇文章主要介紹了Android中Notification用法,以實(shí)例形式較為詳細(xì)的分析并總結(jié)了Notification的功能與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android實(shí)現(xiàn)ImageView圖片縮放和拖動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)ImageView圖片縮放和拖動(dòng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android將camera獲取到的YuvData在jni中轉(zhuǎn)化為Mat方法
今天小編就為大家分享一篇Android將camera獲取到的YuvData在jni中轉(zhuǎn)化為Mat方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08實(shí)例探究Android應(yīng)用編寫時(shí)Fragment的生命周期問題
這篇文章主要介紹了Android應(yīng)用編寫時(shí)Fragment的生命周期問題探究,resumed和paused以及stoped三種狀態(tài)的控制需要熟練掌握,需要的朋友可以參考下2016-02-02Android 中 viewpager 滑動(dòng)指示器的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了android 中 viewpager 滑動(dòng)指示器,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12Android 狀態(tài)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 狀態(tài)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Flutter使用texture_rgba_renderer實(shí)現(xiàn)桌面端渲染視頻詳解
這篇文章主要為大家介紹了Flutter如何使用texture_rgba_renderer實(shí)現(xiàn)桌面端渲染視頻,文中的示例代碼講解詳細(xì),需要的可以了解一下2023-07-07