gpt4 book ai didi

Android:在不选择项目的情况下停止画廊滚动

转载 作者:太空狗 更新时间:2023-10-29 13:40:45 25 4
gpt4 key购买 nike

我目前正在使用画廊 View ,在 onFling 期间它会快速滚动。我的问题是我已经设置了每个项目的 onClick 以将您带到新 Activity 。但我只希望在图库当前未滚动时才起作用。如果图库正在滚动,我希望第一次点击时在当前位置停止滚动。

谁能分享让我检查图库滚动状态的代码,或者可以停止滚动操作的自定义图库代码?

谢谢,乔希

最佳答案

我想这个答案有点晚了,但这是对这个问题的回应,我刚刚修复它并想与遇到同样问题的任何人分享......

public class CustomGallery extends Gallery {
public CustomGallery(Context context) {
super(context);
}

public CustomGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
//In case you might want the gallery to start from left(another known issue...)
public void alignToLeft(Activity activity) {
super.onAttachedToWindow();
DisplayMetrics display = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(display);
// start the images from the left...
int scrollTo = (int) (display.widthPixels - getPixelsFromDIP(display,POSTER_WIDTH_DIP) - 2 * getPixelsFromDIP(display, 5));

MarginLayoutParams margins = (MarginLayoutParams)getLayoutParams();
MarginLayoutParams.class.cast(this.getLayoutParams()).setMargins(-scrollTo + margins.leftMargin, margins.topMargin, margins.rightMargin, margins.bottomMargin);
}

private int getPixelsFromDIP(DisplayMetrics display, int dps){
return (int)(dps * display.scaledDensity + 0.5F);
}

//Override single tap up, to prevent propagation of the event, and force
//the listener to push the event calculating position from X,Y coords
private OnItemClickListener listener;
@Override
public boolean onSingleTapUp(MotionEvent e) {
if(listener != null){
listener.onItemClick(null, this, pointToPosition((int) e.getX(), (int) e.getY()), 0x0345);
}else{
return super.onSingleTapUp(e);
}
return false;
}

//Override the method to assign our custom item listener...
@Override
public void setOnItemClickListener(
android.widget.AdapterView.OnItemClickListener listener) {
this.listener = listener;
}}

现在您所要做的就是,将图库用作常规组件,为其分配 OnItemClickListener,您将在不滚动的情况下获得项目的位置...

final CustomGallery customGallery = (CustomGallery)personDetailView.findViewById(R.id.customGallery);
customGallery(this);
customGallery(castCrewAdapter);
customGallery(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
android.util.Log.e("customGallery TAG", "Item clicked: " + position);
}
});

关于Android:在不选择项目的情况下停止画廊滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6029562/

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