2017-09-29 4 views
1

J'ai un tabsLayout avec trois onglets, chacun contenant une vue personnalisée avec un ImageView et un TextView. Lorsque je veux changer d'onglet et que je clique sur l'un d'entre eux, si je clique à l'extérieur de l'icône, ils basculent correctement. Cependant, lorsque je clique sur l'icône, ils ne basculent pas. Comment puis-je résoudre ce problème?TabLayout Android avec des onglets d'affichage personnalisé - l'icône ne change pas l'onglet

enter image description here

J'ai essayé avec focusable = faux, mais il ne fonctionne pas pour moi.

Mon point de vue personnalisé:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/customButton" 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:alpha="0.5" 
    android:orientation="vertical"> 

    <ImageButton 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_height="0dp" 
     android:layout_weight="4" 
     android:clickable="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:background="@android:color/transparent" 
     android:onClick="airspacesButtonOnClick" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 
     android:tint="@color/colorIcons"/> 

    <TextView 
     android:id="@+id/string" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:clickable="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:textAlignment="center" 
     android:textSize="12sp" /> 
</LinearLayout> 

Mon TabLayout:

<android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="80dp" 
     app:tabIndicatorColor="@color/colorButton" 
     app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" 
     app:tabMode="fixed" 
     app:tabGravity="fill"/> 
<android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

Et enfin mon code java:

int[] iconIds = {R.drawable.ic_profile, R.drawable.ic_plane, R.drawable.ic_document}; 
int[] labelIds = {R.string.menu, R.string.menu, R.string.menu}; 
View customView; 
for (int i = 0; i < tabLayout.getTabCount(); i++) { 
customView = getActivity().getLayoutInflater().inflate(R.layout.custom_icon, null); 
customView.findViewById(R.id.icon).setBackgroundResource(iconIds[i]); 

((TextView) customView.findViewById(R.id.string)).setText(labelIds[i]); 
tabLayout.getTabAt(i).setCustomView(customView); 
+0

Pouvez-vous fournir le code correspondant? – Mehmed

+0

@Mehmed J'ai ajouté le code responsable – Simon

Répondre

1

Vous essayez de faire votre ImageView cliquables en mettant clickable false mais en utilisant onClick le remplace et votre ImageView devient cliquable Gain. C'est pourquoi un clic sur ImageView est consommé par ImageView lui-même et n'est pas propagé à son parent. Donc, cette ligne ImageView de votre mise en page de l'onglet personnalisé doit être retiré:

android:onClick="airspacesButtonOnClick" 

Sauf cette ligne, il a travaillé pour moi avec le code donné et mises en page.

+0

C'était tout, merci! :) – Simon