调用OnClick而不是onLongClick

时间:2015-03-18 09:20:46

标签: android android-listview scroll onclicklistener onlongclicklistener

我只在 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;

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

3 个答案:

答案 0 :(得分:1)

在您的片段或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);

试过这个,它对我有用

答案 1 :(得分:0)

您可以使用OnTouchListener而不是OnClick和OnLongClick。它将带你在适配器内工作的领域。 同样对于ListView,您应该使用OnItemLongClickListener和OnItemClickListener。

答案 2 :(得分:0)

检查以下代码行并尝试理解逻辑,你必须在onlongclick中返回true。

item.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_SHORT).show();
    }
});


item.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
});