我正在创建一个应用程序,我在处理左右滑动时遇到问题。
我想在一个条件后停止左右滑动。
这是我的代码:
public class MyAttendance extends Fragment{
int pos = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_my_attendance, container, false);
final GestureDetector gesture = new GestureDetector(getActivity(),
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
//Log.i(Constants.APP_TAG, "onFling has been called!");
final int SWIPE_MIN_DISTANCE = 120;
final int SWIPE_MAX_OFF_PATH = 250;
final int SWIPE_THRESHOLD_VELOCITY = 200;
try {
if ((Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)) {
return false;
}
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Toast.makeText(getActivity(),"right to left",Toast.LENGTH_SHORT).show();
pos++;
if (pos == 0)
{
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
c.add(Calendar.DATE, pos);
snddate = df.format(c.getTime());
Hit_dataFrag_myatt();
Toast.makeText(getActivity(),"right to left current",Toast.LENGTH_SHORT).show();
}
else
{
Calendar c1 = Calendar.getInstance();
SimpleDateFormat df1 = new SimpleDateFormat("dd/MMM/yyyy");
c1.add(Calendar.DATE, pos);
snddate = df1.format(c1.getTime());
Calendar c2 = Calendar.getInstance();
SimpleDateFormat df2 = new SimpleDateFormat("dd/MMM/yyyy");
todate = df2.format(c2.getTime());
if (c1.getTime().compareTo(c2.getTime()) < 0){
Hit_dataFrag_myatt();
Toast.makeText(getActivity(),"right to left"+snddate,Toast.LENGTH_SHORT).show();
}else if (c1.getTime().compareTo(c2.getTime())==0){
Hit_dataFrag_myatt();
Toast.makeText(getActivity(),"right to left"+snddate,Toast.LENGTH_SHORT).show();
}
// at this codition i want to stop swiping
else if (c1.getTime().compareTo(c2.getTime()) > 0){
// At this conditon i want to stop swiping...
}
}
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// Toast.makeText(getActivity(),"left to right",Toast.LENGTH_SHORT).show();
pos--;
if (pos == 0)
{
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
c.add(Calendar.DATE, pos);
snddate = df.format(c.getTime());
Hit_dataFrag_myatt();
Toast.makeText(getActivity(),"left to right current",Toast.LENGTH_SHORT).show();
}
else
{
Calendar c1 = Calendar.getInstance();
SimpleDateFormat df1 = new SimpleDateFormat("dd/MMM/yyyy");
c1.add(Calendar.DATE, pos);
snddate = df1.format(c1.getTime());
Calendar c2 = Calendar.getInstance();
SimpleDateFormat df2 = new SimpleDateFormat("dd/MMM/yyyy");
todate = df2.format(c2.getTime());
/* Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
c.add(Calendar.DATE, pos);
snddate = df.format(c.getTime());*/
if(c1.getTime().compareTo(c2.getTime())<0) {
Hit_dataFrag_myatt();
Toast.makeText(getActivity(), "left to right" + snddate, Toast.LENGTH_SHORT).show();
}
}
}
} catch (Exception e) {
// nothing
}
return super.onFling(e1, e2, velocityX, velocityY);
}
});
v.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gesture.onTouchEvent(event);
}
});
return v;
}
}
请帮助我在条件后停止滑动,我提到我想停止在我的代码中滑动..
由于
答案 0 :(得分:0)
尝试这种方式: -
首先定义变量
int pos = 0;
GestureDetector gesture;
然后从手势中移除final GestureDetector
个字词。
然后尝试以下代码停止在条件下添加滑动:
else if (c1.getTime().compareTo(c2.getTime()) > 0){
// At this conditon i want to stop swiping...
gesture = new GestureDetector(getActivity(),null);
}