Android 初学者:android gridview 中的触摸事件 [英] Android beginner: Touch events in android gridview
问题描述
我正在使用以下代码来处理gridview(稍微修改自http://developer.android.com/resources/tutorials/views/hello-gridview.html).我想用它们的触摸"等价物替换 onClicklistener 和 onClick() 方法,即 touchlistener 和 onTouch() 以便当我触摸 gridview 中的元素时,元素的图像会发生变化,并且对同一元素的双击会得到它回到原来的状态.
I am using the following code to do things with gridview(slightly modified from http://developer.android.com/resources/tutorials/views/hello-gridview.html). I want to replace the onClicklistener and the onClick() method with their "touch" equivalents i.e. touchlistener and onTouch() so that when i touch an element in the gridview the image of the element changes and a double touch on the same element takes it back to the orginal state.
我该怎么做?我无法让我的代码执行此操作.clicklistener 在某种程度上有效,但触摸无效.请帮忙.
How do I do this? I can't get my code to do this. The clicklistener works to some extent but the touch isn't. Please help.
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 Adapter
public 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 images
private 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
.阅读 MotionEvent 类型,如 ACTION_UP
、ACTION_MOVE
和 ACTION_DOWN
,这意味着此处按下了键、移动了鼠标或未按下键这里...
Use the OnTouchListener
in this way. Read up on MotionEvent types like ACTION_UP
, ACTION_MOVE
, and ACTION_DOWN
which mean that the key was pressed here, mouse was moved, or key was unpressed here...
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 中的触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!