我仅在moto g第二代设备中面临此问题。此代码在其他设备上可以正常工作。

我已经在lisview的项内实现了onClickonLongClick适配器。但是,当我长时间单击任何项​​目时,将调用onClick而不是onLongClick。
如果我缓慢滚动列表视图,也会调用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;


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

最佳答案

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

//以我为例

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(如果是片段的话)或oncreate(如果是活动的话)内部执行此操作

view.setOnLongClickListener(this);


尝试了这个,对我有用

10-08 14:46