ScrollView嵌套SwipeMenuListView焦點問題

    背景:SwipeMenuListView大家都是比較常用的,是類似於QQ左滑刪除ListView的item,但是爲了某些用戶體驗上,不得以把SwipeMenuListView放入在ScrollView裏面,但是在一些處理之後,還是會出現焦點事件問題,比如滑動不太好使,或者上下滑動不好使等問題,最後我通過事件分發傳遞才解決這個問題,只能說做到了最好的優化 ,微微有些瑕疵,不過還好影響不大。得意

   SwipeMenuListView的github:https://github.com/baoyongzhang/SwipeMenuListView

  上圖:

  

   1,處理ScrollView嵌套listView高度問題最常見的,首先自定義一個SwipeListView 去繼承SwipeMenuListView然後重寫onMeasure計算高度嘛。

   

public class SwipeListView extends SwipeMenuListView {
    public SwipeListView(Context context) {
        super(context);
    }

    public SwipeListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SwipeListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
   2,在根據官方使用方法step1,2,3使用之後,就是在Activity中處理他們的焦點問題,這個焦點就是再左滑的時候這個事件很容易被ScrollView給攔截,導致滑不出來。

   我滑動半天可能才滑出來,然後點擊事件有時候又不好使,如果再處理左右事件之後呢,上下滑動又不好使了,要半天才能滑動。如果你的是這種情況,那你就來對了。

  處理方法:看源碼

  給listView添加OnTouchListenter監聽處理橫向和縱向

private int localWigth = 0;     private int localHeigth = 0;

listView.setOnTouchListener(new View.OnTouchListener() {   
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch(motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        localWigth = (int)motionEvent.getX();
                        localHeigth = (int)motionEvent.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int sx = (int)motionEvent.getX();
                        int sy = (int)motionEvent.getY();
                        if (localWigth - sx > 10 || localWigth - sx < 10) {  //重點就是來判斷橫向或者縱向滑動的距離來分配焦點
                            scrollview.requestDisallowInterceptTouchEvent(true);  //攔截ScrollView的事件,讓listView能滑動
                        }else {
                            scrollview.requestDisallowInterceptTouchEvent(false);   //取消ScrollView的攔截事件,讓listView不能滑動
                        }
                        if (localHeigth - sy >= 80 || localHeigth - sy < -80){
                            scrollview.requestDisallowInterceptTouchEvent(false);
                        }else {
                            scrollview.requestDisallowInterceptTouchEvent(true);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                   
                        localWigth = 0;
                        localHeigth= 0;
                        break;
                }
                return false;
            }
        });
通過以上方法,我其實處理的差不過了,不過有點沒中不足的是,在很小的機率下,左右滑動還是會有焦點分配錯誤的情況是在即在左滑又在上滑且上滑超過50的時候會出現這個問題,但是機率很少,基本是不影響的。

費了今天半天的勁終於解決這個問題了。

可以好好睡覺了~


紀實:2016-9-12


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章