Подтвердить что ты не робот

Как я могу отключить нажатие TabLayout на Android

Я хочу использовать TabLayout в своем приложении для использования fragment и ViewPager.
но я хочу отключить клик на TabLayout, чтобы переключаться между fragment, просто салфть, чтобы переключаться между fragmenst.
Я пишу ниже коды, но не работаю со мной, и когда нажимаем на элементы TabLayout, переходите к этому 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);
        }
    }
}

для отключения нажмите TabLayout. Я использую этот код:

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;
        }
    });
}

Как я могу решить эту проблему? Спасибо всем < 3

4b9b3361

Ответ 1

Вы получаете доступ к вкладкам до setupWithViewPager, поэтому ваш код не работает. Итак, сначала установите табуляции, а затем установочный код.

Попробуйте следующее:

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;
            }
        });
    }

Ответ 2

tabLayout.clearOnTabSelectedListeners();

Ответ 3

сенсорный список массивов решит эту проблему.

объявить деятельность

var touchableList: ArrayList<View>? = ArrayList()

отключить все вкладки

touchableList = tabLayout?.touchables
touchableList?.forEach { it.isEnabled = false }

Включить вкладку

touchableList?.get(0)?.isEnabled = true

Ответ 4

Вот что сработало для меня:

class LockableTabLayout : TabLayout {

    var swipeable: Boolean = true

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean = !swipeable
}

Ответ 5

Ответ выше, здесь, отключил щелчок элемента вкладки, когда вы уже закончили заполнение элемента вкладки или уже определили tabitem в xmls.

например:

        <android.support.design.widget.TabLayout
            android:id="@+id/tabla_quiz_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:text="1"
                android:layout_height="wrap_content"/>

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:text="2"
                android:layout_height="wrap_content"/>

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:text="3"
                android:layout_height="wrap_content"/>

        </android.support.design.widget.TabLayout>

Но, если у вас разные случаи, при использовании наблюдаемых данных или динамического табита. например:

        // QuizFragment.kt
        quizListLive.observe([email protected], Observer { quizList ->

            quizList?.takeIf { list -> list.isNotEmpty() }?.let {

                // this is where new fragment added
                it.forEachIndexed { j, quiz ->
                    mViewPagerAdapter?.addFragment(
                            fragment = QuizFragmentSub.newInstance(quiz = quiz),
                            title = (j + 1).toString()
                    )
                }

                // notify the viewpager, I'm using kotlin extension to call widget id
                vp_quiz_content.adapter?.notifyDataSetChanged()

                // then, childCount will have size > 0
                // disable tablayout click
                val tabStrip = tabla_quiz_navigation.getChildAt(0) as LinearLayout
                for (i in 0 until tabStrip.childCount) {
                    tabStrip.getChildAt(i).setOnTouchListener { _, _ -> true }
                }
            }
        })

====

        // quiz_fragment.xml
        <android.support.design.widget.TabLayout
            android:id="@+id/tabla_quiz_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.design.widget.TabLayout>

метод немного отличается во время его вызова, вам просто нужно настроить ваш tabitem, чтобы отключить его щелчок после того, как все фрагменты viewpager уже добавлены.

если есть кто-то лучше подходить к этому, пожалуйста, отредактируйте мой ответ

Ответ 6

Нет необходимости использовать линейный макет, вы можете просто добавить это в упражнение:

private void setUntouchableTab () {
    tablayout.setupWithViewPager(viewpager, true);
    tablayout.clearOnTabSelectedListeners();
    for (View v : tablayout.getTouchables()) {
        v.setEnabled(false);
    }
}