android - cannot change tabs in TabLayout -
i new android programming , trying change tab on touch of tab header text. here code
activity_main
<android.support.design.widget.appbarlayout> <android.support.design.widget.collapsingtoolbarlayout> <imageview android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="match_parent" android:src="@drawable/test_image" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:scaletype="centercrop" /> <android.support.design.widget.tablayout android:id="@+id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:background="@android:color/transparent" app:layout_collapsemode="pin" /> <android.support.v7.widget.toolbar/> </android.support.design.widget.collapsingtoolbarlayout> </android.support.design.widget.appbarlayout>
mainactivity.java
tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(viewpager);
output application tablayout fragments tabs contain cardview. when touch tab header able switch different tabs can switch tab swiping fragments finger.however when add this
tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { charsequence _header = tab.gettext(); selected_tab_header = string.valueof(_header); if (selected_tab_header == "tab1") { headerimage.setimageresource(r.drawable.someimage); } } @override public void ontabreselected(tablayout.tab tab) { } @override public void ontabunselected(tablayout.tab tab) { } });
i cannot switch tab's anymore i.e when touch tab header though code fired(image changes) tab not switched swiping tab's finger still works.is there thing missing code? or there other way achieve this?
dont forget viewpager.setcurrentitem(tab.getposition())
in ontabselected
:
public void ontabselected(tablayout.tab tab) { charsequence _header = tab.gettext(); selected_tab_header = string.valueof(_header); if (selected_tab_header == "tab1") { headerimage.setimageresource(r.drawable.someimage); } if (viewpager != null) { viewpager.setcurrentitem(tab.getposition()); } }
hope helps!!
Comments
Post a Comment