gpt4 book ai didi

android - OnLongClickListener 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:12 25 4
gpt4 key购买 nike

我有一个 ImageView。我需要为 ImageView 使用 onLongClickListener。当我使用这段代码时,什么也没有发生。

代码:

 gallery=(Gallery)findViewById(R.id.gall1);

gallery.setAdapter(new ImageAdapter(MainActivity.this));

gallery.setOnItemClickListener(new OnItemClickListener()
{

@Override
public void onItemClick(
AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub

final ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);

imageView.setLongClickable(true);


imageView.setOnLongClickListener(new View.OnLongClickListener(){

@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(), "Long Press work", Toast.LENGTH_SHORT).show();

return true;
}

});


imageView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v,MotionEvent event) {
// TODO Auto-generated method stub
layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();

switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
{
parms = (RelativeLayout.LayoutParams) imageView.getLayoutParams();

dx = event.getRawX() - parms.leftMargin;
dy = event.getRawY() - parms.topMargin;
}
break;
case MotionEvent.ACTION_MOVE :
{
x = event.getRawX();
y = event.getRawY();
parms.leftMargin = (int) (x-dx);
parms.topMargin = (int) (y - dy);
imageView.setLayoutParams(parms);
}
break;
case MotionEvent.ACTION_UP :
{

}
break;
}

return true;
}
});
}


});

最佳答案

需要通过代码开启长按:

imageView.setLongClickable(true);
imageView.setClickable(true);

最后一行可能不是必需的,但也不会造成麻烦。

关于android - OnLongClickListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com