如何停止滚动的画廊工具?画廊、工具

2023-09-12 21:54:48 作者:爱吃甜筒的猫

我装一些图片到画廊。现在,我可以滚动,但一旦开始滚动了滚动不会停止。我想画廊只是滚动到下一个图像,然后停止,直到用户执行滚动手势了。

这是我的code

 进口android.widget.ImageView;
进口android.widget.Toast;
 进口android.widget.AdapterView.OnItemClickListener;

公共类GalleryExample延伸活动{

私人画廊的画廊;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

     画廊=(图库论坛)findViewById(R.id.examplegallery);
     gallery.setAdapter(新AddImgAdp(本));

     gallery.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图父,视图V,INT位置,长的id){

            Toast.makeText(GalleryExample.this,位置=+位置,Toast.LENGTH_SHORT).show();
        }
    });

}

公共类AddImgAdp扩展了BaseAdapter {
    INT GalItemBg;
    私人语境续;


    私人整数[] Imgid = {
            R.drawable.a_1,R.drawable.a_2,R.drawable.a_3,R.drawable.a_4,R.drawable.a_5,R.drawable.a_6,R.drawable.a_7
    };

    公共AddImgAdp(上下文C){
        CONT = C;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground,0);
        typArray.recycle();
    }

    公众诠释getCount将(){
        返回Imgid.length;
    }

    公共对象的getItem(INT位置){
        返回的位置;
    }

    众长getItemId(INT位置){
        返回的位置;
    }

    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        ImageView的imgView =新ImageView的(续)

        imgView.setImageResource(Imgid [位置]);

        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imgView.setBackgroundResource(GalItemBg);

        返回imgView;
    }
}
 

}

和xmlLayout文件

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout机器人:ID =@ + ID / LinearLayout01
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<画廊的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / examplegallery
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>
 

解决方案

我想我找到了一种方法来实现这两个仅滚动1视图在画廊时间,并能够有一个最小的滑动长度来触发动画。

覆盖图库小部件的onFling方法而不是调用super.onFling,检查的刷卡是否是从左到右或从右到左,并调用相应的DPAD事件,如下所示:

 私人布尔isScrollingLeft(MotionEvent E1,E2 MotionEvent){
  返回e2.getX()> e1.getX();
}

@覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
  INT KEVENT;
  如果(isScrollingLeft(E1,E2)){//检查滚动左
    KEVENT = KeyEvent.KEY code_DPAD_LEFT;
  }
  其他{//否则滚动权
    KEVENT = KeyEvent.KEY code_DPAD_RIGHT;
  }
  的onkeydown(KEVENT,NULL);
  返回true;
}
 
会声会影x2中怎么让滚动的字幕中途暂停啊

I loaded some images into a gallery. Now I'm able to scroll but once started scrolling the scrolling won't stop. I would like the gallery to just scroll to the next image and then stop until the user does the scroll gesture again.

this is my code

import android.widget.ImageView;
import android.widget.Toast;
 import android.widget.AdapterView.OnItemClickListener;

public class GalleryExample extends Activity {

private Gallery gallery;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     gallery = (Gallery) findViewById(R.id.examplegallery);
     gallery.setAdapter(new AddImgAdp(this));

     gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {

            Toast.makeText(GalleryExample.this, "Position=" + position, Toast.LENGTH_SHORT).show();
        }
    });

}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;


    private Integer[] Imgid = {
            R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
    };

    public AddImgAdp(Context c) {
        cont = c;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }

    public int getCount() {
        return Imgid.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imgView = new ImageView(cont);

        imgView.setImageResource(Imgid[position]);

        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;
    }
}

}

and the xmlLayout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

解决方案

I think I found a way to achieve both only scrolling 1 view at a time in the gallery and be able to have a minimal swipe length to trigger animation.

Override the onFling method of the gallery widget and instead of calling super.onFling, check to see whether or not the swipe was from left to right or right to left and call the appropriate dpad event as shown below:

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

@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;  
}

 
精彩推荐
图片推荐