2013-07-04 2 views
1

J'ai un TabHost avec quelques onglets, et après avoir appuyé longuement sur une tabulation, je veux obtenir la position ou l'étiquette de l'onglet qui a été pressé longtemps, et non l'onglet actuel qui est montré. Ci-dessous il y a un code dans lequel je crée long écouteur de presse pour la TabHost:Android clic long sur l'onglet

myTabHost.getTabWidget().getChildAt(i).setOnLongClickListener(new OnLongClickListener() { 

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

Toute solution? Est-ce correct d'appliquer l'écouteur à TabHost dans mon cas?

Répondre

1

Je résolus mon problème en ajoutant à la vue de l'onglet une information d'étiquette, puis-je attaché à la vue d'un auditeur qui obtient et imprime ce tag:

View tabView= mTabHost.getTabWidget().getChildAt(i); 
// set the tag information at the view of the tab (the tag contains the position number of the tab) 
tabView.setTag(Integer.valueOf(i)); 
tabView.setOnLongClickListener(new OnLongClickListener() { 

      @Override 
      public boolean onLongClick(View v) { 
       // TODO Auto-generated method stub 
       // I print the number position of the tab 
       Log.d("tab number", ((Integer)view.getTag()).toString()); 
       return false; 
      } 
     }); 
0

L'identificateur de l'onglet qui a fait l'objet d'un clic long réside dans le paramètre View v de la fonction onLongClick. Appelez v.getId() et le reste sont des détails

+0

c. getId() me renvoie toujours -1 – lory105