gpt4 book ai didi

android - 调用 OnClick 而不是 onLongClick

转载 作者:行者123 更新时间:2023-11-29 01:34:42 52 4
gpt4 key购买 nike

我只在 moto g 2nd generation device 中遇到这个问题。此代码在其他设备上运行良好。

我已经在 lisview 项目的适配器内部实现了 onClickonLongClick。但是当我长按任何项目时,将调用 onClick 而不是 onLongClick。如果我缓慢滚动 ListView ,也会调用 onClick。

getView() 中的代码如下:

LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
lifeActionObj = actionList.get(position);

View v = (View)inflater.inflate(R.layout.single_action, null);

TextView actionNameView = (TextView) v.findViewById(R.id.action_name);
ImageView actionIcon = (ImageView) v.findViewById(R.id.action_icon);
final Button aInfoBtn = (Button) v.findViewById(R.id.action_info);
final Button aPickBtn = (Button) v.findViewById(R.id.action_pick);



final int actionLocked = lifeActionObj.isLocked();


aPickBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

if(actionLocked ==1)
{
diplayDialog();
}
else
{
LifeUser lifeUser = new LifeUser();
try {
LifeAction lifeActionObj = actionList.get(position);
int result = lifeUser.addHabit(traitId, lifeActionObj);

} catch (Exception e1) {
e1.printStackTrace();
}

actionActivity.finish();
}
}
});

aInfoBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(actionLocked ==1)
{
diplayDialog();
}
else
{
Intent in = new Intent(actionActivity,
DisplayHtmlActivity.class);
try
{
in.putExtra("title", actionList.get(position).getDisplayName());
in.putExtra("fileName", actionList.get(position).getActionInfoFileName(actionActivity.getLifeTraitObject().getTraitId())+".html");
}
catch (Exception e)
{
e.printStackTrace();
}
actionActivity.startActivity(in);
}
}
});

final ViewGroup parent1 = parent;
final View ex = v;

v.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(actionActivity, "on click", Toast.LENGTH_SHORT).show();
if(actionActivity.getActionMode() != null)
{
actionActivity.getActionMode().finish();
}

if(actionLocked == 1)
{
diplayDialog();
}
}
});


v.setOnLongClickListener(new OnLongClickListener()
{
@Override
public boolean onLongClick(View v) {
Toast.makeText(actionActivity, "on long click", Toast.LENGTH_SHORT).show();
if(actionLocked == 1)
{
return false;
}
else
{
ex.setBackgroundColor(Color.LTGRAY);
ex.setLongClickable(false);
ex.setClickable(false);
parent1.setClickable(false);
aInfoBtn.setClickable(false);
aPickBtn.setClickable(false);
final int traitPosition = position;

try {
actionActivity.startActionMode(new LifeCAB(
ex,
parent1,
traitPosition,
aInfoBtn,
aPickBtn,
actionList.get(position).getDisplayName()));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}

}
});

return v;

谁能告诉我这是什么原因造成的?请指导我解决这个问题。

最佳答案

在您的 fragment 或 MainActivity 中,使用 OnLongClickListener 实现它

//在我的案例中是 fragment

public class Returning extends Fragment implements OnLongClickListener

在实现它时,它会要求你覆盖

@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Log.e("ServiceHandler", "Couldn't get any data from the url");
return true;
}

然后在 oncreateview(如果是 fragment )或 oncreate(如果是 Activity )中执行此操作

view.setOnLongClickListener(this);

试过这个,它对我有用

关于android - 调用 OnClick 而不是 onLongClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29118093/

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