Android筆記 頁面右滑返回效果

2021-09-29 10:54:39 字數 1415 閱讀 6370

參考各位大牛的部落格後所總結的個人筆記。

view decorview;  

float downx, downy;  

float screenwidth, screenheight;  

boolean allow = false;  

public void initlefttorightback()  

@override  

public boolean ontouchevent(motionevent event)   

boolean isside = false;  

public void touchevent(motionevent event) else if(event.getaction() == motionevent.action_move && isside)  

}else if(event.getaction() == motionevent.action_up && isside)else  

}  }  

}  }  

2、新建二級頁面,繼承自lefttorightback,主要是為了繼承touchevent事件,然後再oncreate方法中新增初始化initlefttorightback();

public class serveractionactivity extends lefttorightback     }
3、此時已經可以實現右滑返回,但是在滑動過程中會出現亂的頁面。解決方法如下,在styles中加入透明的主題,這樣在返回的時候不會出現亂的頁面,而是出現上級頁面。並且在androidmanifest中引用。

styles

@color/colorprimarydark

@color/coloraccent

@android:color/transparent

@null

true  

manifest

4、新的問題又來了,擺放控制項後可以看到上級頁面,這樣不行,所以要在二級頁面上面新增上背景顏色

android:background=「@android:color/white」

5、經過測試發現在有乙個大的控制項擺放在頁面後右滑就沒有用,touch事件被控制項消費掉了,所以要利用事件分發機制,保證頁面可以正常返回。

獲取控制項後直接新增分發,如果還有其他控制項消費了該事件,都要加上分發機制

mlistviewarray = findviewbyid(r.id.sever_action_listview);  

mlistviewarray.setontouchlistener(new view.ontouchlistener()   

});

android 仿微信6 2右滑返回

為了讓activity背景完全透明,光上面的 還是不行的,還需要為activity指定如下的style。html view plaincopy null true null 這裡不做多的介紹啦!這樣activity其實已經有透明的底啦!現在只需要然activity隨著滑動位移。當然是試用上面提到的g...

android 檢測右滑的WebView

import import android.content.context import android.util.attributeset import android.view.motionevent import android.view.velocitytracker import andr...

android 檢測右滑的WebView

今天產品出新花樣非得要右滑。檢測到右滑手勢後事件不做處理放在activity中做對應的處理即可了。import android.content.context import android.util.attributeset import android.view.motionevent impor...