gpt4 book ai didi

android - 如何更改画廊的 ScrollView

转载 作者:行者123 更新时间:2023-11-30 04:13:15 25 4
gpt4 key购买 nike

MyGallery gallery = (MyGallery)findViewById(R.id.gallery_photo);
PhonePhotoViewerAdapter = new PhonePhotoViewerAdapter(this, FilePath);
gallery.setAdapter(PhonePhotoViewerAdapter);
gallery.setSelection(0);

上面是我用 ScrollView 显示照片的代码。
但是如果我想专注于下一个,我必须在屏幕上移动更大的距离的手指。
我想让它在移动较小的距离时改变焦点
我该怎么做?
我修改了类 Gallery 如下:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > 50 && Math.abs(velocityX) > 100) {
// Fling left
} else if (e2.getX() - e1.getX() > 50 && Math.abs(velocityX) > 100) {
// Fling right
}
return false;
}

下面的代码是我修改的:

gallery.setLongClickable(false);
gallery.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if((action == MotionEvent.ACTION_DOWN) && !Clicking) {
StartX = event.getX();
StartIndex = gallery.getSelectedItemPosition();
Clicking = true;
}
else if((action == MotionEvent.ACTION_UP) && Clicking) {
EndX = event.getX();
EndIndex = gallery.getSelectedItemPosition();
Clicking = false;
if(((EndX - StartX) > 50) && (StartIndex == EndIndex)) {
if(EndIndex > 0) {
gallery.setSelection(EndIndex - 1);
}
}
else if(((StartX - EndX) > 50) && (StartIndex == EndIndex)) {
if(EndIndex < count - 1) {
gallery.setSelection(EndIndex + 1);
}
}
}
return false;
}
});

但显示有些异常。

最佳答案

我找到了我的问题的解决方案。
即定义 MyGallery 如下:

public class MyGallery extends Gallery {
public MyGallery(Context context, AttributeSet attrSet) {
super(context, attrSet);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int kEvent;
if(isScrollingLeft(e1, e2)) {
//Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
}
else {
//Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}

private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > e1.getX();
}

@Override
protected android.view.ViewGroup.LayoutParams generateLayoutParams (android.view.ViewGroup.LayoutParams p) {
return super.generateLayoutParams(p);
}
}

关于android - 如何更改画廊的 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10478833/

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