Android初学者:android gridview中的触摸事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2570415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android beginner: Touch events in android gridview
提问by aj.
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.
我正在使用以下代码来处理 gridview(从http://developer.android.com/resources/tutorials/views/hello-gridview.html稍作修改)。我想用它们的“触摸”等效项替换 onClicklistener 和 onClick() 方法,即 touchlistener 和 onTouch() 以便当我触摸 gridview 中的元素时,该元素的图像会发生变化,并且在同一元素上双击即可获取它回到原来的状态。
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
};
}
回答by PS376
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...
使用OnTouchListener
这种方式。阅读 MotionEvent 类型,例如ACTION_UP
, ACTION_MOVE
,ACTION_DOWN
这意味着此处按下了键、移动了鼠标或此处未按下键...
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;
}
}
回答by Emile
Following on from your code, as jaydeepfifadra suggested you can use the setOnItemClickListener method of the gride view rather than the image itself. (Not to say that that isn't possible but i've not tried it).
根据您的代码,正如 jaydeepfifadra 建议的那样,您可以使用 gride 视图的 setOnItemClickListener 方法而不是图像本身。(并不是说这是不可能的,但我还没有尝试过)。
gridView.setOnItemClickListener(
new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
((Image)view.setSelected(!(Image)view.getSelected()));
}
});
);
Now i'm not 100% sure if the following will work, but in the above i've set the view.setSelected() state of the image to toggle. I'm sort of guessing that you can set the image resource to a drawable selector. i.e.
现在我不是 100% 确定以下是否有效,但在上面我已经将图像的 view.setSelected() 状态设置为切换。我有点猜测您可以将图像资源设置为可绘制选择器。IE
make your R.drawable.sample_2 resources selectors, such as:
制作您的 R.drawable.sample_2 资源选择器,例如:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/image_selected" />
<item android:drawable="@drawable/image_unselected" />
</selector>
However, whilst this allows you to detect the single tap that would toggle the selected state. It doesn't cover the double tap. There also doesn't appear to be a setOnDoubleClickItemListener() method.
但是,虽然这允许您检测将切换选定状态的单击。它不包括双击。似乎也没有 setOnDoubleClickItemListener() 方法。
As such it might be required for your specific situation that you implement a GestureDetector and implement your own SimpleOnGestureListener class (MyGestureListener extends SimpleOnGestureListener) that manages the double and single tap events your looking for.
因此,对于您的特定情况,您可能需要实现 GestureDetector 并实现您自己的 SimpleOnGestureListener 类(MyGestureListener 扩展 SimpleOnGestureListener)来管理您要查找的双击和单击事件。
You can see an example of implementing the gesture listener and detector here.
您可以在此处查看实现手势侦听器和检测器的示例。
how to implement both ontouch and also onfling in a same listview?
如何在同一个列表视图中同时实现 ontouch 和 onfling?
You would set the gestureListener as the onTouchListener of your grid view.
您可以将gestureListener 设置为网格视图的onTouchListener。
回答by jaydeepfifadra
Use the onItemSelectListener
of gridView.
使用onItemSelectListener
gridView 的。