2017-09-29 6 views
1

tabsLayoutには、3つのタブ(ImageViewTextViewのカスタムビューがそれぞれ含まれています)があります。 タブを切り替えるときに、タブの1つをクリックすると、アイコンの外側をクリックするとタブが正しく切り替わります。ただし、アイコンをクリックすると、切り替えられません。 これを修正するにはどうすればよいですか?カスタムタブを表示するタブライアウト - アイコンクリックでタブが切り替わらない

enter image description here

私は偽=フォーカス可能にしようとしたが、それは私のために動作しませんでした。

マイカスタムビュー:

<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> 

マイ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" /> 

そして最後に、私の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

関連コードを提供できますか? – Mehmed

+0

@Mehmed私は責任コード – Simon

答えて

1

はあなたがfalseにclickableを設定することで、あなたのImageViewのがunclickable作ってみますonClickを使用すると上書きされ、ImageViewはクリック可能になります。利得。そのため、ImageViewのクリックはImageView自体によって消費され、その親に伝播されません。だからあなたのカスタムタブレイアウトのImageViewの中に、この行を削除する必要があります。この行を除く

android:onClick="airspacesButtonOnClick" 

、それは与えられたコードとレイアウトと私のために働きました。

+0

を追加しました。それはありがとうございました! :) – Simon

関連する問題