在Android中,除了手動(dòng)滑動(dòng) Listview 之外,還有一些自動(dòng)滾動(dòng)的方法:
setScrollY(int Y)
setPostion(int position)
setPostionFromTop(int position, int offset)
scrollTo(int x, int y)
scrollBy(int x, int y)
scrollListBy(int y)
smoothScrollByOffset(int offset)
smoothScrollBy(int distance, int duration)
smoothScrollToPosition(int position)
smoothScrollToPosition(int position, int boundPositon)
smoothScrollToPositionFromTop(int position, int offset)
smoothScrollToPositionFromTop(int position, int offset, int duration)
smoothScrollByOffset(int offset)
首先,y自然是指最上邊的縱坐標(biāo),而Postion是指最上邊顯示的item標(biāo)號(hào)。
set開(kāi)頭的是移到目標(biāo)位置。
scroll開(kāi)頭的也是直接移到目標(biāo)位置。
smoonth開(kāi)頭的有動(dòng)畫(huà)效果,滑動(dòng)需要花費(fèi)一定的時(shí)間。
我所遇到的坑就是采用smooth后,還有一個(gè)修改界面的處理,兩個(gè)互相沖突了,導(dǎo)致出現(xiàn)了奇怪的效果。
一開(kāi)始我想著在smooth結(jié)束后來(lái)個(gè)回調(diào),在回調(diào)中再來(lái)處理修改界面,但不知道如何來(lái)創(chuàng)建回調(diào)。
之后通過(guò)為smooth設(shè)定好固定周期,并在這個(gè)周期后通過(guò)listview.post()來(lái)延時(shí)執(zhí)行修改界面處理,從而消除了沖突。
下面是各方法的詳細(xì)介紹:
setScrollY(int Y),直接修改scrollY屬性,不會(huì)通知adapter更新數(shù)據(jù),因而改變位置后可能會(huì)出現(xiàn)空白,且再點(diǎn)擊一下又會(huì)返回到之前的位置,一般沒(méi)什么用。
setPostion(int position),移動(dòng)到最頂端編號(hào)為position的item,簡(jiǎn)單粗暴,但適用場(chǎng)景有限。
setPostionFromTop(int position, int offset),在上面方法的基礎(chǔ)上還可以再移動(dòng)offset像素的偏移量,offset為正時(shí)為向上移動(dòng),能很好的應(yīng)付不需要滑動(dòng)動(dòng)畫(huà)時(shí)的需求。
scrollListBy(int y),與setScrollY相比,此方法會(huì)通知adapter更新數(shù)據(jù),但最低api需求為19。
scrollTo(int x, int y),一目了然,移動(dòng)到坐標(biāo)為(x,y)的位置,與set方法相比,它還可以進(jìn)行二維移動(dòng)。
scrollBy(int x, int y),在當(dāng)前的基礎(chǔ)上移動(dòng)(x,y)的像素距離。
smoothScrollByOffset(int offset) ,平緩的滑動(dòng)到據(jù)當(dāng)前編號(hào)差為offset的item,注意,這里的offset指的是item編號(hào)差而不是像素差,坑爹的樣子。
smoothScrollBy(int distance, int duration),這里的distance指的是像素了,平緩在duration毫秒內(nèi)滑動(dòng)distance的像素差。
smoothScrollToPosition(int position),平緩滑動(dòng)到編號(hào)為position的item。
smoothScrollToPosition(int position, int boundPositon),如果當(dāng)前編號(hào)大于boundPosition才進(jìn)行滑動(dòng),如果boundPosition為負(fù)時(shí)取絕對(duì)值。
smoothScrollToPositionFromTop(int position, int offset),類(lèi)似setPostionFromTop,滑動(dòng)到距編號(hào)為position的item像素差為offset的位置。
smoothScrollToPositionFromTop(int position, int offset, int duration),在上面方法的基礎(chǔ)上限制了滑動(dòng)時(shí)間為duration。