먼저 터치 이벤트 반환을 하위보기로 반환합니다. 그들을 위해 onClick 또는 onTouch 리스너를 정의하면 parnt 뷰 (예 : fragment)는 터치 리스너를 수신하지 않습니다. 이 상황에서 조각에 대한 슬쩍 리스너를 정의하고자한다면, 당신은 새로운 클래스를 구현해야합니다
는
package com.neganet.QRelations.fragments;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;
public class SwipeListenerFragment extends FrameLayout {
private float x1,x2;
static final int MIN_DISTANCE=150;
private onSwipeEventDetected mSwipeDetectedListener;
public SwipeListenerFragment(Context context) {
super(context);
}
public SwipeListenerFragment(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SwipeListenerFragment(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean result=false;
switch(ev.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = ev.getX();
break;
case MotionEvent.ACTION_UP:
x2 = ev.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE)
{
if(deltaX<0)
{
result=true;
if(mSwipeDetectedListener!=null)
mSwipeDetectedListener.swipeLeftDetected();
}else if(deltaX>0){
result=true;
if(mSwipeDetectedListener!=null)
mSwipeDetectedListener.swipeRightDetected();
}
}
break;
}
return result;
}
public interface onSwipeEventDetected
{
public void swipeLeftDetected();
public void swipeRightDetected();
}
public void registerToSwipeEvents(onSwipeEventDetected listener)
{
this.mSwipeDetectedListener=listener;
}
}
완전히 같은 레이아웃의 다른 유형의 구현을 만들 수 있습니다. 이 클래스는 오른쪽 및 왼쪽 스 와이프를 모두 감지 할 수 있으며 특히 감지 후에는 onInterceptTouchEvent true를 반환합니다. 왜냐하면 우리가 그것을하지 않으면 몇 번 자식 뷰가 이벤트를받을 것이고 조각을위한 스 와이프와 자식 뷰를위한 onClick (둘 다)이 실행되어 몇 가지 문제를 일으킬 수 있기 때문입니다. 당신이이 태그를 시작 참조
<com.neganet.QRelations.fragments.SwipeListenerFragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:id="@+id/main_list_layout"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="match_parent" tools:context="com.neganet.QRelations.fragments.mainList"
android:background="@color/main_frag_back">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:id="@+id/farazList"
android:scrollbars="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|center_vertical" />
</com.neganet.QRelations.fragments.SwipeListenerFragment>
우리가 만든 클래스입니다 : 이 클래스를 한 후, 당신은 당신의 조각 XML 파일을 변경해야합니다. 지금 조각 클래스 :
View view=inflater.inflate(R.layout.fragment_main_list, container, false);
SwipeListenerFragment tdView=(SwipeListenerFragment) view;
tdView.registerToSwipeEvents(this);
and then Implement SwipeListenerFragment.onSwipeEventDetected in it:
@Override
public void swipeLeftDetected() {
Toast.makeText(getActivity(), "left", Toast.LENGTH_SHORT).show();
}
@Override
public void swipeRightDetected() {
Toast.makeText(getActivity(), "right", Toast.LENGTH_SHORT).show();
}
그것은 조금 복잡하지만 난 그것을 시도 완벽한 :)
타겟이 1.6보다 새로운 경우에는 focusable을 설정하는 대신 android : onClick = "functionname"을 직접 사용할 수 있으며 onclick 리스너를 코드에 할당 할 수 있습니다. – Yenchi