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);//頭部內(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
當(dāng)ListView有Header時(shí) onItemClick里的position不正確的原因
這篇文章主要介紹了當(dāng)ListView有Header時(shí) onItemClick里的position不正確的原因的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
基于Android引入IjkPlayer無法播放mkv格式視頻的解決方法
下面小編就為大家分享一篇基于Android引入IjkPlayer無法播放mkv格式視頻的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android編程之九宮格實(shí)現(xiàn)方法實(shí)例分析
這篇文章主要介紹了Android編程之九宮格實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android九宮格的實(shí)現(xiàn)方法與具體步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01
Android 四種動(dòng)畫效果的調(diào)用實(shí)現(xiàn)代碼
在這里, 我將每種動(dòng)畫分別應(yīng)用于四個(gè)按鈕為例,需要的朋友可以參考下2013-01-01
探討:如何修改Android超時(shí)休眠時(shí)間
本篇文章是對如何修改Android超時(shí)休眠時(shí)間的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解
這篇文章主要介紹了Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-01-01
基于Android-Skin-Loader實(shí)現(xiàn)換膚效果
這篇文章主要為大家詳細(xì)介紹了基于Android-Skin-Loader實(shí)現(xiàn)換膚效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09

