我正在关注this tutorial,除了我正在尝试为每个图片添加“触摸”方面。
正如您所看到的,HelloGridView
与教程完全相同:
public class HelloGridView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
对于ImageAdapter
,我添加了onTouchListener
:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
Log.v("log_tag", "MotionEvent.ACTION_DOWN");
return true;
} else if(event.getAction()==MotionEvent.ACTION_UP){
Toast.makeText(mContext, "ACTION_UP", Toast.LENGTH_SHORT).show();
Log.v("log_tag", "MotionEvent.ACTION_UP");
return false;
} else {
return false;
}
}
});
return imageView;
}
}
问题是,每当我触摸其中一张图片时,onTouchListener
会在ImageAdapter
中触发,而onItemClickListener
中的HelloGridView
触发onItemClickListener
。
我希望ACTION_UP
触发MotionEvent.ACTION_UP
,因此如果事件为MotionEvent.ACTION_DOWN
则返回false,如果事件为onTouchListener
则返回true。
根据我的理解,如果我返回true,则onTouchListener
会消耗该事件并完成。但是如果我返回false,那么onItemClickListener
不会消耗该事件,而是会将其传递给HelloGridView
中的HelloGridView
。
不幸的是,如果事件为ACTION_DOWN
时返回false,则事件仅传递给ACTION_UP
。但是,当我这样做时,{{1}}永远不会被执行。有人能告诉我我做错了什么以及如何解决它?如果我的问题不明确,请告诉我,我会尽力澄清! :)
答案 0 :(得分:1)
此代码为
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
if(!mIsTouchHandeled){
int position = mChatListView.pointToPosition((int) event.getX(), (int) event.getY());
if(position!=ListView.INVALID_POSITION){
mChatListView.performItemClick(mChatListView.getChildAt(position-mChatListView.getFirstVisiblePosition()), position, mChatListView.getItemIdAtPosition(position));
}
}
break;
}
// return super.onTouchEvent(event);
return false;
}
}
答案 1 :(得分:0)
仅在Action.Down上触发OnItemClickListener。
只需执行以下操作:
摆脱OnItemClickListener并用你自己的方法替换它。类似“public void myOnItemActionUp(View view, int pos)
”的东西 - 无论如何。
将HelloGridView
的引用传递给ImageAdapter,然后在OnTouchListener mHelloGridView.myOnItemActionUp(v, position)
中调用if (event.getAction() == MotionEvent.ACTION_UP)
。