2

Je suis confronté à ce problème uniquement dans moto g 2ème génération. Ce code fonctionne parfaitement sur d'autres appareils.OnClick est appelé à la place de onLongClick

J'ai mis en œuvre onClick et onLongClick adaptateur intérieur sur des articles de lisview. Mais lorsque je clique longtemps sur un élément, onClick est appelé à la place de onLongClick. OnClick est également appelé si je fais défiler la liste lentement.

code à l'intérieur getView() est ci-dessous:

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; 

Quelqu'un peut-il me dire ce qui peut être la cause de cela? Merci de me guider pour résoudre ce problème.

Répondre

0

Vous pouvez utiliser OnTouchListener au lieu de OnClick et OnLongClick. Il vous faudra un champ pour travailler à l'intérieur de l'adaptateur. Aussi pour ListView vous devez utiliser OnItemLongClickListener et OnItemClickListener.

0

Vérifiez les lignes de code ci-dessous et essayez de comprendre la logique, vous devez retourner true dans onlongclick.

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; 
    } 
}); 
+0

Je ne trouve pas de changement. Le code ci-dessus renvoie également vrai. Aussi ce code ne fonctionne pas seulement pour moto g 2ème génération. – megha

1

Dans votre fragment ou MainActivity, mettre en œuvre avec OnLongClickListener

// Fragment dans mon cas

public class Returning extends Fragment implements OnLongClickListener 

la mise en œuvre, il vous demandera de passer outre

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

Puis à l'intérieur de oncreateview (En cas de fragment) ou surcréer en cas d'activité faites-le

view.setOnLongClickListener(this); 

essayé et il travaille pour moi

+0

Ce code fonctionne bien pour tous les mobiles sauf moto g 2e génération. – megha

+0

ok, en fait, je n'ai pas moto g donc ne pas comprendre la véritable cause d'un tel comportement. Eh bien, laissez-nous savoir quand vous avez trouvé la solution. Merci – Hulk