gpt4 book ai didi

android - 滑动不起作用

转载 作者:行者123 更新时间:2023-11-29 21:17:20 26 4
gpt4 key购买 nike

在我的代码中,从右向左滑动正常,但从左向右滑动不工作。不知道是什么原因。

代码-

public class Types extends Activity{

RelativeLayout layout1;
int[] backgroundResId;
int currentIndex=0;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type);
backgroundResId=new int[]{R.drawable.back,R.drawable.bg,R.drawable.frame1};
layout1 = (RelativeLayout) findViewById(R.id.layout1);
changeBackground();
ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
layout1.setOnTouchListener(activitySwipeDetector);

}


private void changeBackground(){
layout1.setBackgroundResource(backgroundResId[currentIndex]);
}

public class ActivitySwipeDetector implements View.OnTouchListener {

static final String logTag = "ActivitySwipeDetector";
static final int MIN_DISTANCE = 100;
private float downX, upX;
private Activity activity;

public ActivitySwipeDetector(Activity activity){
this.activity = activity;
}

public void onRightToLeftSwipe(){
Log.i(logTag, "RightToLeftSwipe!");
currentIndex++;
if(currentIndex<backgroundResId.length){
changeBackground();
}
}

public void onLeftToRightSwipe(){
Log.i(logTag, "LeftToRightSwipe!");
if(currentIndex>0){
changeBackground();
}
}

public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();

float deltaX = downX - upX;

// swipe horizontal?
if(Math.abs(deltaX) > MIN_DISTANCE){
// left or right
if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
}
else {
Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
return false; // We don't consume the event
}


return true;
}
}
return false;
}

}

在这一行 private Activity activity; 我收到了警告。为什么?

最佳答案

你忘了减去 currentIndex

 public void onLeftToRightSwipe(){
Log.i(logTag, "LeftToRightSwipe!");
if(currentIndex>0){
currentIndex--;
changeBackground();
}
}

关于android - 滑动不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21111205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com