




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)滑動(dòng)定位和吸附懸停效果
本篇文章為大家展示了怎么在Android中實(shí)現(xiàn)滑動(dòng)定位和吸附懸停效果,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.tabscroll.CustomScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#ccc"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這里是頂部?jī)?nèi)容區(qū)域"
android:textSize="16sp"
/>
</LinearLayout>
<!--占位的tablayout-->
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_holder"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffffff"
app:tabIndicatorColor="@color/colorPrimary"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimary"
/>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
/>
</LinearLayout>
<!--實(shí)際用戶操作的tablayout-->
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_real"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffffff"
android:visibility="invisible"
app:tabIndicatorColor="@color/colorPrimary"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimary"
/>
</FrameLayout>
</com.tabscroll.CustomScrollView>
</LinearLayout>實(shí)現(xiàn)滑動(dòng)定位的功能可以參考之前的文章,這里主要是進(jìn)行吸附懸停的效果。數(shù)據(jù)初始化:/**
*
占位tablayout,用于滑動(dòng)過(guò)程中去確定實(shí)際的tablayout的位置
*/
private
TabLayout
holderTabLayout;
/**
*
實(shí)際操作的tablayout,
*/
private
TabLayout
realTabLayout;
private
CustomScrollView
scrollView;
private
LinearLayout
container;
private
String[]
tabTxt
=
{"客廳",
"臥室",
"餐廳",
"書(shū)房",
"陽(yáng)臺(tái)",
"兒童房"};
private
List<AnchorView>
anchorList
=
new
ArrayList<>();
//判讀是否是scrollview主動(dòng)引起的滑動(dòng),true-是,false-否,由tablayout引起的
private
boolean
isScroll;
//記錄上一次位置,防止在同一內(nèi)容塊里滑動(dòng)
重復(fù)定位到tablayout
private
int
lastPos
=
0;
//監(jiān)聽(tīng)判斷最后一個(gè)模塊的高度,不滿一屏?xí)r讓最后一個(gè)模塊撐滿屏幕
private
ViewTreeObserver.OnGlobalLayoutListener
listener;
for
(int
i
=
0;
i
<
tabTxt.length;
i++)
{
AnchorView
anchorView
=
new
AnchorView(this);
anchorView.setAnchorTxt(tabTxt[i]);
anchorView.setContentTxt(tabTxt[i]);
anchorList.add(anchorView);
container.addView(anchorView);
}
for
(int
i
=
0;
i
<
tabTxt.length;
i++)
{
holderTabLayout.addTab(holderTabLayout.newTab().setText(tabTxt[i]));
realTabLayout.addTab(realTabLayout.newTab().setText(tabTxt[i]));
}一開(kāi)始讓實(shí)際的tablayout移動(dòng)到占位的tablayout處,覆蓋占位的tablayout。listener
=
new
ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public
void
onGlobalLayout()
{
//計(jì)算讓最后一個(gè)view高度撐滿屏幕
int
screenH
=
getScreenHeight();
int
statusBarH
=
getStatusBarHeight(AliHomeMoreActivity.this);
int
tabH
=
holderTabLayout.getHeight();
int
lastH
=
screenH
-
statusBarH
-
tabH
-
16
*
3;
AnchorView
anchorView
=
anchorList.get(anchorList.size()
-
1);
if
(anchorView.getHeight()
<
lastH)
{
LinearLayout.LayoutParams
params
=
new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.height
=
lastH;
anchorView.setLayoutParams(params);
}
//一開(kāi)始讓實(shí)際的tablayout
移動(dòng)到
占位的tablayout處,覆蓋占位的tablayout
realTabLayout.setTranslationY(holderTabLayout.getTop());
realTabLayout.setVisibility(View.VISIBLE);
container.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
};
container.getViewTreeObserver().addOnGlobalLayoutListener(listener);
private
int
getScreenHeight()
{
return
getResources().getDisplayMetrics().heightPixels;
}
public
int
getStatusBarHeight(Context
context)
{
int
result
=
0;
int
resourceId
=
context.getResources()
.getIdentifier("status_bar_height",
"dimen",
"android");
if
(resourceId
>
0)
{
result
=
context.getResources().getDimensionPixelSize(resourceId);
}
return
result;
}scrollview滑動(dòng)主要在滑動(dòng)過(guò)程這不斷監(jiān)聽(tīng)滑動(dòng)的距離,再移動(dòng)實(shí)際的tablayout,當(dāng)在屏幕內(nèi)時(shí),讓其一直覆蓋在占位的tablayout上,看上去是跟著scrollview一起滑動(dòng)的;當(dāng)滑出屏幕時(shí),實(shí)際的tablayout不斷移動(dòng)使其相對(duì)屏幕靜止,看上去是吸附在屏幕頂部。scrollView.setOnTouchListener(new
View.OnTouchListener()
{
@Override
public
boolean
onTouch(View
v,
MotionEvent
event)
{
if
(event.getAction()
==
MotionEvent.ACTION_DOWN)
{
isScroll
=
true;
}
return
false;
}
});
//監(jiān)聽(tīng)scrollview滑動(dòng)
scrollView.setCallbacks(new
CustomScrollView.Callbacks()
{
@Override
public
void
onScrollChanged(int
x,
int
y,
int
oldx,
int
oldy)
{
//根據(jù)滑動(dòng)的距離y(不斷變化的)
和
holderTabLayout距離父布局頂部的距離(這個(gè)距離是固定的)對(duì)比,
//當(dāng)y
<
holderTabLayout.getTop()時(shí),holderTabLayout
仍在屏幕內(nèi),realTabLayout不斷移動(dòng)holderTabLayout.getTop()距離,覆蓋holderTabLayout
//當(dāng)y
>
holderTabLayout.getTop()時(shí),holderTabLayout
移出,realTabLayout不斷移動(dòng)y,相對(duì)的停留在頂部,看上去是靜止的
int
translation
=
Math.max(y,
holderTabLayout.getTop());
realTabLayout.setTranslationY(translation);
if
(isScroll)
{
for
(int
i
=
tabTxt.length
-
1;
i
>=
0;
i--)
{
//需要y減去頂部?jī)?nèi)容區(qū)域的高度(具體看項(xiàng)目的高度,這里demo寫(xiě)死的200dp)
if
(y
-
200
*
3
>
anchorList.get(i).getTop()
-
10)
{
setScrollPos(i);
break;
}
}
}
}
});
private
void
setScrollPos(int
n
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年一次性使用麻醉穿刺針行業(yè)深度研究分析報(bào)告
- 2025年度新型建筑房屋質(zhì)量賠償協(xié)議書(shū)
- 二零二五年度城市應(yīng)急車(chē)輛駕駛員聘用協(xié)議
- 教育綜合體居間服務(wù)合同
- 二零二五年度知識(shí)產(chǎn)權(quán)授權(quán)轉(zhuǎn)讓協(xié)議模板
- 不可撤銷(xiāo)贈(zèng)予合同范本
- 日化用品包裝膜行業(yè)深度研究報(bào)告
- 2025年度商鋪?zhàn)赓U合同租賃期三年商鋪?zhàn)赓U及裝修補(bǔ)貼合同
- 2025年度房屋產(chǎn)權(quán)變更公證代理合同
- 2025年銀白鋁型材項(xiàng)目可行性研究報(bào)告
- 義務(wù)教育(音樂(lè))課程標(biāo)準(zhǔn)(2022年版)解讀
- 第三章企業(yè)的生產(chǎn)和成本
- 統(tǒng)編版 高中語(yǔ)文 必修下冊(cè) 《祝?!贰读纸填^風(fēng)雪山神廟》
- 2024年職教高考《機(jī)械制圖》考試題庫(kù)
- 九型人格與親子教育課件
- 2025屆高校畢業(yè)生公開(kāi)招聘工作人員報(bào)名登記表
- DB34∕T 2290-2022 水利工程質(zhì)量檢測(cè)規(guī)程
- GB/T 44399-2024移動(dòng)式金屬氫化物可逆儲(chǔ)放氫系統(tǒng)
- 薛崗小學(xué)反邪教工作計(jì)劃
- 某住宅小區(qū)物業(yè)服務(wù)投標(biāo)書(shū)范本
- 2024-2030年中國(guó)高空外墻清洗行業(yè)市場(chǎng)發(fā)展趨勢(shì)與前景展望戰(zhàn)略分析報(bào)告
評(píng)論
0/150
提交評(píng)論