【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)滑動(dòng)定位和吸附懸停效果_第1頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)滑動(dòng)定位和吸附懸停效果_第2頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)滑動(dòng)定位和吸附懸停效果_第3頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)滑動(dòng)定位和吸附懸停效果_第4頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)滑動(dòng)定位和吸附懸停效果_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論