Android初学者:触摸android gridview中的事件

Android初学者:触摸android gridview中的事件,第1张

概述我使用以下代码来处理gridview(从http://developer.android.comesourcesutorials/views/hello-gridview.html稍微修改).我想用它们的“触摸”等价物替换onClicklistener和onClick()方法,即touchlistener和onTouch(),这样当我触摸gridview中的元素时,元素的图像会发生变化,并且

我使用以下代码来处理grIDvIEw(从http://developer.android.com/resources/tutorials/views/hello-gridview.html稍微修改).我想用它们的“触摸”等价物替换onClickListener和onClick()方法,即touchListener和ontouch(),这样当我触摸grIDvIEw中的元素时,元素的图像会发生变化,并且同一个元素上的双击会接受它回到原始状态.

我该怎么做呢?我无法让我的代码执行此 *** 作. clickListener在某种程度上起作用,但触摸不是.请帮忙.

public class ImageAdapter extends BaseAdapter {private Context mContext;public ImageAdapter(Context c) {    mContext = c;}public int getCount() {    return mThumbIDs.length;}public Object getItem(int position) {    return null;}public long getItemID(int position) {    return 0;}// create a new ImageVIEw for each item referenced by the Adapterpublic VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {    ImageVIEw imageVIEw;    if (convertVIEw == null) {  // if it's not recycled, initialize some attributes        imageVIEw = new ImageVIEw(mContext);        imageVIEw.setLayoutParams(new GrIDVIEw.LayoutParams(85, 85));        imageVIEw.setScaleType(ImageVIEw.ScaleType.CENTER_CROP);        imageVIEw.setpadding(8, 8, 8, 8);        imageVIEw.setonClickListener(new VIEw.OnClickListener()            {              @OverrIDe              public voID onClick(VIEw vIEw)               {                  if(position==0)                  {                                  //do this                              }                              else                              {                                //do this                              }                           }                    });    } else {        imageVIEw = (ImageVIEw) convertVIEw;    }    imageVIEw.setimageResource(mThumbIDs[position]);    return imageVIEw;}// references to our imagesprivate Integer[] mThumbIDs = {        R.drawable.sample_2, R.drawable.sample_3,        R.drawable.sample_4, R.drawable.sample_5,        R.drawable.sample_6, R.drawable.sample_7,        R.drawable.sample_0, R.drawable.sample_1,        R.drawable.sample_2, R.drawable.sample_3,        R.drawable.sample_4, R.drawable.sample_5,        R.drawable.sample_6, R.drawable.sample_7,        R.drawable.sample_0, R.drawable.sample_1,        R.drawable.sample_2, R.drawable.sample_3,        R.drawable.sample_4, R.drawable.sample_5,        R.drawable.sample_6, R.drawable.sample_7};}

解决方法:

以这种方式使用OntouchListener.阅读ACTION_UP,ACTION_MOVE和ACTION_DOWN之类的MotionEvent类型,这意味着在此处按下了键,移动了鼠标,或者在此处键入了键…

public voID addListenerToGrID() {    grIDVIEw = (GrIDVIEw) findVIEwByID(R.ID.grIDVIEw1);    grIDVIEw.setontouchListener(new OntouchListener() {        public boolean ontouch(VIEw v, MotionEvent me) {            int action = me.getActionMasked();            float currentXposition = me.getX();            float currentYposition = me.getY();            int position = grIDVIEw.pointToposition((int) currentXposition, (int) currentYposition);                        if (action == MotionEvent.ACTION_UP) {                            // Key was pressed here                        }            return true;

}

总结

以上是内存溢出为你收集整理的Android初学者:触摸android gridview中的事件全部内容,希望文章能够帮你解决Android初学者:触摸android gridview中的事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115850.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存