国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Android實現(xiàn)快速滾動FastScrollView效果

瀏覽:7日期:2023-12-08 09:53:51

先看效果圖:

Android實現(xiàn)快速滾動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ò)展拖動區(qū)域?qū)挾龋勺孕行薷膍ScrollExHeight = dip2px(50); //擴(kuò)展拖動區(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è)置了大滾動條樣式,滾動時更加明顯:

<com.zwxuf.apkparserdemo.FastScrollViewandroid: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'> <HorizontalScrollViewandroid:layout_width='match_parent'android:layout_height='match_parent' > <EditTextandroid: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>

同理,可以依此制作快速滾動的 HorizontalScrollView。

到此這篇關(guān)于Android實現(xiàn)快速滾動FastScrollView的文章就介紹到這了,更多相關(guān)android滾動FastScrollView內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 辽中县| 白玉县| 黄陵县| 普格县| 正阳县| 吉隆县| 雅安市| 阿克陶县| 云和县| 湛江市| 信阳市| 玉山县| 张家港市| 永州市| 保定市| 库车县| 芒康县| 应城市| 沧源| 新乡市| 南开区| 内乡县| 德格县| 巴南区| 乌海市| 鹤壁市| 海伦市| 南漳县| 大田县| 囊谦县| 醴陵市| 邵阳县| 任丘市| 乌苏市| 丁青县| 清远市| 霸州市| 博兴县| 苏尼特右旗| 海阳市| 广平县|