[英]Scroll to top of next item in a recyclerview
我有以下代碼,用於檢測onTouch中的滑動是否為向左/向右。 但是我想檢測頂部/底部。 請建議修改代碼,以了解如何通過修改代碼來實現
r.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int downX = 0,upX;
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:{
downX = (int) event.getX();}
case MotionEvent.ACTION_UP:{
upX = (int) event.getX();
float deltaX = downX - upX;
Log.e("DeltaX","Dd "+deltaX);
if(Math.abs(deltaX)>0){
if(deltaX>=0){
return true;
}else{
return true;
}
}
else {
}
}
}
return false;
}
});
請不要建議任何第三方庫或任何其他解決方案。 請僅建議對以上代碼進行更改。 我已經嘗試了很多其他方法,但這非常合適
感謝您的回應!
這是我想出的代碼
r.addOnScrollListener(new RecyclerView.OnScrollListener() {
public boolean top;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (top) {
int index = mLay.findFirstVisibleItemPosition();
r.smoothScrollToPosition(index);
} else {
int index = mLay.findLastVisibleItemPosition();
r.smoothScrollToPosition(index);
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
top = false;
} else {
top = true;
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.