¿Cómo puedo deshabilitar hacer clic en TabLayout en Android

Quiero utilizar TabLayout en mi aplicación para el fragment uso y ViewPager .
Pero quiero deshabilitar haga clic en TabLayout para cambiar entre fragment , simplemente pasar para cambiar entre fragmenst .
Escribo debajo de los códigos, pero no me funciona y cuando haga clic en TabLayout elementos, vaya a ese fragment !

MainActivity XML:

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/app_namefull" android:textSize="23sp" /> </android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="72dp" app:tabGravity="fill" app:tabIndicatorColor="@color/white" app:tabMode="fixed" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> 

MainActivity java:

 public class Main_Page extends AppCompatActivity { private CollapsingToolbarLayout mCollapsingToolbarLayout; private Toolbar toolbar; private TabLayout tabLayout; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main__page); mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout); //mCollapsingToolbarLayout.setTitle(getResources().getString(R.string.app_name)); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); //getSupportActionBar().setDisplayHomeAsUpEnabled(true); viewPager = (ViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); tabLayout = (TabLayout) findViewById(R.id.tabs); LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0)); for(int i = 0; i < tabStrip.getChildCount(); i++) { tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); } tabLayout.setupWithViewPager(viewPager); setupTabIcons(); } /** * Adding custom view to tab */ private void setupTabIcons() { TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); tabOne.setText(R.string.free_fragment_title); tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_download_image, 0, 0); tabLayout.getTabAt(0).setCustomView(tabOne); TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); tabTwo.setText(R.string.paid_fragment_title); tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_paid_download_image, 0, 0); tabLayout.getTabAt(1).setCustomView(tabTwo); TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); tabThree.setText(R.string.pdf_fragment_title); tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_pdf_icon, 0, 0); tabLayout.getTabAt(2).setCustomView(tabThree); } /** * Adding fragments to ViewPager * @param viewPager */ private void setupViewPager(ViewPager viewPager) { ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFrag(new free_fragment(), "رایگان ها"); adapter.addFrag(new paid_fragment(), "پرداختی ها"); adapter.addFrag(new pdf_fragment(), "مقالات"); viewPager.setAdapter(adapter); } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } public void addFrag(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } } 

Para deshabilitar haga clic en TabLayout utilizo este código:

 LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0)); for(int i = 0; i < tabStrip.getChildCount(); i++) { tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); } 

¿Cómo puedo solucionar este problema? Gracias a todos <3

Usted está accediendo a las pestañas antes de setupWithViewPager, thats por qué su código no está funcionando. Así que primero establezca las pestañas entonces settouchlistener código.

Prueba esto:

 tabLayout.setupWithViewPager(viewPager); setupTabIcons(); LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0)); for(int i = 0; i < tabStrip.getChildCount(); i++) { tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); } 
 tabLayout.clearOnTabSelectedListeners(); 
  • Obtención de excepción de conversión de clase durante el uso de android.support.v7.widget.SearchView
  • Actualización asíncrona de vista fragmentada: ImageView no se actualiza, pero otros niños se actualizan en la vista
  • Deslizando un fragmento sobre otro en Android
  • Cómo crear una VISTA DE TAB en Android Layout
  • Cambiar la barra de herramientas al icono del cajón / menú después de popBackStack ()?
  • Seleccione la imagen de la galería en Android Fragment
  • Problemas de memoria en fragmentos que muestran imágenes
  • Fragmento 2 panel windowSoftInputMode para teclado suave
  • Cómo pasar valores entre fragmentos
  • Crear listview en el fragmento android
  • El atributo android: name en el fragmento <
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.