SwipMenuListView學習筆記

SwipeMenuListView類

涉及到的一些知識點桑寨,在onInterceptTouchEvent方法的down事件中的三個方法:

pointToPosition
getChildAt
inRangeOfView

SwipeMenuLayout類

涉及知識點:

GestureDetectorCompat手勢類

以及

手勢類的監(jiān)聽器OnGestureListener
平滑滑動的類ScrollerCompat

詳細解釋

1冬三、pointToPosition

AbsListView類中的方法

  /**
     * Maps a point to a position in the list.
     *
     * @param x X in local coordinate
     * @param y Y in local coordinate
     * @return The position of the item which contains the specified point, or
     *         {@link #INVALID_POSITION} if the point does not intersect an item.
     */
    public int pointToPosition(int x, int y) {
    }

解釋是:返回觸摸到的點的position

listview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int position = listview.pointToPosition((int)event.getX(), (int)event.getY());
                return false;
            }
        });

可以打印下position的值敦腔,它不存在什么復用不復用的問題己英,實際得到的position值就是item的位置。

2捂龄、getChildAt

/**
     * Returns the view at the specified position in the group.
     *
     * @param index the position at which to get the view from
     * @return the view at the specified position or null if the position
     *         does not exist within the group
     */
    public View getChildAt(int index) {
        if (index < 0 || index >= mChildrenCount) {
            return null;
        }
        return mChildren[index];
    }

由于我們在使用listview的時候漆诽,都存在復用問題,所以在用getChildAt方法得到view時扮休,得到的都是復用的那個view,所以需要用得到的position減去getFirstVisiblePosition

View view = getChildAt(position- getFirstVisiblePosition());

3漩绵、inRangeOfView

這個就是判斷事件是否在某個view內(nèi)部
這個是SwipMenuListView類中自己實現(xiàn)的方法,不是系統(tǒng)方法

/**
     * 判斷點擊事件是否在某個view內(nèi)
     *
     * @param view
     * @param ev
     * @return
     */
    public static boolean inRangeOfView(View view, MotionEvent ev) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        if (ev.getRawX() < x || ev.getRawX() > (x + view.getWidth()) || ev.getRawY() < y || ev.getRawY() > (y + view.getHeight())) {
            return false;
        }
        return true;
    }

4、GestureDetectorCompat

private GestureDetectorCompat mGestureDetector;
private OnGestureListener mGestureListener;
mGestureDetector = new GestureDetectorCompat(getContext(),
                mGestureListener);
mGestureListener = new SimpleOnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2,
                    float velocityX, float velocityY) {
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        };

其實在這個接口中有很多關(guān)于手勢的回調(diào)方法:

 /**
     * A convenience class to extend when you only want to listen for a subset
     * of all the gestures. This implements all methods in the
     * {@link OnGestureListener} and {@link OnDoubleTapListener} but does
     * nothing and return {@code false} for all applicable methods.
     */
    public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener {
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        public void onLongPress(MotionEvent e) {
        }

        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            return false;
        }

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            return false;
        }

        public void onShowPress(MotionEvent e) {
        }

        public boolean onDown(MotionEvent e) {
            return false;
        }

        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }

        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }

        public boolean onSingleTapConfirmed(MotionEvent e) {
            return false;
        }
    }

5肛炮、ScrollerCompat

跟Scroller一樣止吐,一般想要實現(xiàn)平滑過渡,可以使用

public static ScrollerCompat create(Context context, Interpolator interpolator) {
        return new ScrollerCompat(context, interpolator);
    }

這是個靜態(tài)方法侨糟,直接用類可以調(diào)用碍扔,并且可以傳入一個差值器,很有用的秕重。

調(diào)用startScroll方法進行滑動

 public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        mImpl.startScroll(mScroller, startX, startY, dx, dy, duration);
    }

最后需要覆寫computeScroll方法完成

@Override
    public void computeScroll() {
         // ......
          postInvalidate();
        //  ......
  }

postInvalidate會調(diào)用view的draw方法不同,draw方法內(nèi)部會調(diào)用computeScroll(源碼中能找到),循環(huán)調(diào)用完成動畫,computeScroll方法中需要有動畫結(jié)束邏輯二拐。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末服鹅,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子百新,更是在濱河造成了極大的恐慌企软,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饭望,死亡現(xiàn)場離奇詭異仗哨,居然都是意外死亡,警方通過查閱死者的電腦和手機铅辞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門厌漂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人斟珊,你說我怎么就攤上這事苇倡。” “怎么了囤踩?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵旨椒,是天一觀的道長。 經(jīng)常有香客問我高职,道長钩乍,這世上最難降的妖魔是什么辞州? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任怔锌,我火速辦了婚禮,結(jié)果婚禮上变过,老公的妹妹穿的比我還像新娘埃元。我一直安慰自己,他們只是感情好媚狰,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布岛杀。 她就那樣靜靜地躺著,像睡著了一般崭孤。 火紅的嫁衣襯著肌膚如雪类嗤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天辨宠,我揣著相機與錄音遗锣,去河邊找鬼。 笑死嗤形,一個胖子當著我的面吹牛精偿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼笔咽,長吁一口氣:“原來是場噩夢啊……” “哼搔预!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起叶组,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤拯田,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后扶叉,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體勿锅,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年枣氧,在試婚紗的時候發(fā)現(xiàn)自己被綠了溢十。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡达吞,死狀恐怖张弛,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情酪劫,我是刑警寧澤吞鸭,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站覆糟,受9級特大地震影響刻剥,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜滩字,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一造虏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧麦箍,春花似錦漓藕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至诀蓉,卻和暖如春栗竖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背渠啤。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工狐肢, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人埃篓。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓处坪,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子同窘,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容