Android實(shí)現(xiàn)快速滾動(dòng)FastScrollView效果
先看效果圖:

public class FastScrollView extends ScrollView {
private Rect mBarRect = new Rect();
private int mScrollExWidth;
private int mScrollExHeight;
private boolean mScrollStart;
private int dx, dy;
private int mRightPadding;
public FastScrollView(Context context) {
super(context);
init();
}
public FastScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FastScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mScrollExWidth = dip2px(16); //擴(kuò)展拖動(dòng)區(qū)域?qū)挾龋勺孕行薷?
mScrollExHeight = dip2px(50); //擴(kuò)展拖動(dòng)區(qū)域高度,可自行修改
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction() & ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
//Log.e("fly", "down");
mScrollStart = false;
if (canScroll()) {
dx = (int) ev.getX();
dy = (int) ev.getY();
float scrollPos = (float) getHeight() / getChildView().getHeight();
int barHeight = (int) (scrollPos * getHeight());
//Log.e("fly", scrollPos+", "+ barHeight);
mBarRect.left = getWidth() - mRightPadding - getVerticalScrollbarWidth();
mBarRect.right = getWidth() - mRightPadding;
mBarRect.top = (int) (getScrollY() * scrollPos);
mBarRect.bottom = mBarRect.top + barHeight;
//Dbg.print(mBarRect.left, mBarRect.right, mBarRect.top, mBarRect.bottom, dx, dy);
if (dx >= mBarRect.left - mScrollExWidth
&& dx < mBarRect.right
&& dy >= mBarRect.top - mScrollExHeight
&& dy < mBarRect.bottom + mScrollExHeight) {
//Log.e("fly", "mScrollStart");
mScrollStart = true;
}
}
break;
case MotionEvent.ACTION_MOVE:
if (mScrollStart) {
int offsetY = (int) ev.getY() - dy;
//Log.e("fly", "move: " + ev.getY());
int top = mBarRect.top + offsetY;
float scrollPos = (float) top / getHeight();
int scrollY = (int) (scrollPos * getChildView().getHeight());
if (scrollY < 0) {
scrollY = 0;
}
if (scrollY > getChildView().getHeight() - getHeight()) {
scrollY = getChildView().getHeight() - getHeight();
}
setScrollY(scrollY);
return true;
}
break;
}
return super.dispatchTouchEvent(ev);
}
private boolean canScroll() {
View child = getChildView();
if (child != null && child.getHeight() > getHeight()) {
return true;
} else {
return false;
}
}
private View getChildView() {
if (getChildCount() > 0) {
return getChildAt(0);
} else {
return null;
}
}
private int dip2px(float dpValue) {
final float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 這里不用view的paddding,用于預(yù)留特殊空隙
*
* @param mRightPadding
*/
public void setRightPadding(int mRightPadding) {
this.mRightPadding = mRightPadding;
}
}
布局中設(shè)置了大滾動(dòng)條樣式,滾動(dòng)時(shí)更加明顯:
<com.zwxuf.apkparserdemo.FastScrollView android:id="@+id/mScrollView" android:layout_width="0dp" android:layout_weight="1" android:scrollbarSize="10dp" android:paddingRight="10dp" android:scrollbarThumbVertical="@drawable/fast_scroll_thumb_drawable" android:layout_height="match_parent"> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/et_xml" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:editable="false" android:textColor="#000000" android:textSize="10sp" android:typeface="monospace"/> </HorizontalScrollView> </com.zwxuf.apkparserdemo.FastScrollView>
同理,可以依此制作快速滾動(dòng)的 HorizontalScrollView。
到此這篇關(guān)于Android實(shí)現(xiàn)快速滾動(dòng)FastScrollView的文章就介紹到這了,更多相關(guān)android滾動(dòng)FastScrollView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android notifyDataSetChanged() 動(dòng)態(tài)更新ListView案例詳解
- 解決java.lang.NoClassDefFoundError: android.support.v4.animation.AnimatorCompatHelper問題
- AndroidStudio報(bào)錯(cuò)Emulator:PANIC:Cannot find AVD system path. Please define ANDROID_SDK_ROOT(解決方案)
- 在Android項(xiàng)目中使用AspectJ的詳細(xì)攻詻
- 捕獲與解析Android NativeCrash
- Android AS創(chuàng)建自定義布局案例詳解
相關(guān)文章
Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能示例
這篇文章主要介紹了Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能,結(jié)合實(shí)例形式分析了widget組件在桌面應(yīng)用中的事件響應(yīng)與屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
Android編程實(shí)現(xiàn)獲得內(nèi)存剩余大小與總大小的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲得內(nèi)存剩余大小與總大小的方法,涉及Android基于ActivityManager實(shí)現(xiàn)內(nèi)存信息的操作技巧,需要的朋友可以參考下2015-12-12
android studio 清單配置文件androidmainfest.xml詳細(xì)解讀
AndroidManifest官方解釋是應(yīng)用清單,每個(gè)應(yīng)用的根目錄中都必須包含一個(gè),并且文件名必須一模一樣,這個(gè)文件中包含了APP的配置信息,系統(tǒng)需要根據(jù)里面的內(nèi)容運(yùn)行APP的代碼,顯示界面,這篇文章介紹了android studio 清單配置文件androidmainfest.xml解讀,需要的朋友可以參考下2024-04-04
如何使用Flutter實(shí)現(xiàn)手寫簽名效果
Flutter插件提供了用于繪制平滑簽名的簽名板,下面這篇文章主要給大家介紹了關(guān)于如何使用Flutter實(shí)現(xiàn)手寫簽名效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
android遞歸壓縮上傳多張圖片到七牛的實(shí)例代碼
本篇文章主要介紹了android遞歸壓縮上傳多張圖片到七牛的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09

