Android 滑動(dòng)定位和吸附懸停效果實(shí)現(xiàn)代碼
在前兩篇文章中,分別介紹了tablayout+scrollview 和 tablayout+recyclerview 實(shí)現(xiàn)的滑動(dòng)定位的功能,文章鏈接:
Android 實(shí)現(xiàn)錨點(diǎn)定位
Android tabLayout+recyclerView實(shí)現(xiàn)錨點(diǎn)定位
仔細(xì)看的話,這種滑動(dòng)定位的功能,還可以整體滑動(dòng),再加上頂部tablayout 吸附懸停的效果。
實(shí)現(xiàn)效果:

布局
這里采用的是兩個(gè) tablayout。
一個(gè)用于占位,位于原始位置,scrollview內(nèi)部,隨scrollview滾動(dòng);另一個(gè)則是在滑動(dòng)過(guò)程中,不斷滑動(dòng),滑動(dòng)到頂部時(shí)吸附在屏幕頂部,用戶(hù)實(shí)際操作的也是這個(gè)tablayout。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/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í)際用戶(hù)操作的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è)模塊的高度,不滿(mǎn)一屏?xí)r讓最后一個(gè)模塊撐滿(mǎn)屏幕
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高度撐滿(mǎn)屏幕
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 newPos) {
if (lastPos != newPos) {
realTabLayout.setScrollPosition(newPos, 0, true);
}
lastPos = newPos;
}
tablayout點(diǎn)擊切換
由于實(shí)際操作的是realtablayout ,所以這里只需要一直監(jiān)聽(tīng)該tablayout。
//實(shí)際的tablayout的點(diǎn)擊切換
realTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
isScroll = false;
int pos = tab.getPosition();
int top = anchorList.get(pos).getTop();
//同樣這里滑動(dòng)要加上頂部?jī)?nèi)容區(qū)域的高度(這里寫(xiě)死的高度)
scrollView.smoothScrollTo(0, top + 200 * 3);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
至此,滑動(dòng)定位+頂部吸附懸停 的效果結(jié)束了。做完之后,再看這個(gè)效果,其實(shí)和 支付寶-首頁(yè) 更多 那個(gè)頁(yè)面里的滑動(dòng)效果一樣。
代碼與之前文章的在同一個(gè)git地址里。
詳細(xì)代碼見(jiàn)
github地址: https://github.com/taixiang/tabScroll
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)滑動(dòng)到頂部懸停的效果
這篇文章給大家介紹一種不常見(jiàn)的實(shí)現(xiàn)Android滑動(dòng)到頂部懸停效果的方式,對(duì)大家開(kāi)發(fā)Android具有一定的參考借鑒價(jià)值,有需要的朋友們可以來(lái)一起看看。2016-09-09
Android回調(diào)與觀察者模式的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Android回調(diào)與觀察者模式的實(shí)現(xiàn)原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android adb安裝apk時(shí)提示Invalid APK file的問(wèn)題
這篇文章主要介紹了Android adb安裝apk時(shí)提示Invalid APK file的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Android圖片實(shí)現(xiàn)壓縮處理的實(shí)例代碼
本篇文章主要介紹了Android圖片實(shí)現(xiàn)壓縮處理的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
Android實(shí)現(xiàn)捕獲TextView超鏈接的方法
這篇文章主要介紹了Android實(shí)現(xiàn)捕獲TextView超鏈接的方法,涉及Android查找TextView中超鏈接的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android手機(jī)衛(wèi)士之綁定sim卡序列號(hào)
這篇文章主要介紹了Android手機(jī)衛(wèi)士之綁定sim卡序列號(hào)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android 實(shí)現(xiàn)錨點(diǎn)定位思路詳解
本篇文章就使用tablayout、scrollview來(lái)實(shí)現(xiàn)android錨點(diǎn)定位的功能。通過(guò)<a href="#head" rel="external nofollow" > 去設(shè)置頁(yè)面內(nèi)錨點(diǎn)定位跳轉(zhuǎn)。具體實(shí)現(xiàn)思路大家跟隨腳本之家小編一起通過(guò)本文看下吧2018-07-07
Android開(kāi)發(fā)事件處理的代碼如何寫(xiě)手摸手教程
這篇文章主要為大家介紹了Android開(kāi)發(fā)事件處理的代碼如何寫(xiě)手摸手教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

