简体   繁体   中英

Android : How to make a tab not selectable/swipable as per radiobutton click

In my android activity I have a radio button group and a tab layout using viewpager . There are two tabs and 2 radio buttons.

  • The first tab is an attachment tab
  • second one is an event tab.

When i checked the first radio button then user is not allowed to see the first tab,so i need to disable the first tab and shows the second tab for the user.If i selected the second radio button i need to enable the first tab and user is allowed to access the first tab.I don't want to remove the tab,i just want to disable it so that user can't access it by swiping or clicking on the tab.

How to do that? Below is my code to show tab.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_asset);
    //remaining code
    viewPager2 = (ViewPager) findViewById(R.id.viewPager2);
    setupViewPager2(viewPager2);
    tabLayout2 = (TabLayout) findViewById(R.id.tab_layout2);
    tabLayout2.setupWithViewPager(viewPager2);//setting tab over viewpager
    rdbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    }
    });

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

    //Implementing tab selected listener over tablayout
    tabLayout2.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager2.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
            switch (tab.getPosition()) {
                case 0:
                    Log.e("TAG","TAB1");
                    break;
                case 1:
                    Log.e("TAG","TAB2");
                    break;
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
    }  

//Setting View Pager
private void setupViewPager2(ViewPager viewPager) {
    expAttList = new ArrayList<COAAccount>();
    adapter2 = new ViewPagerAdapter(getSupportFragmentManager());
    adapter2.addFrag(new AttachmentFragment("Attachments",expAttList,fontFamily), "Attachments");
    adapter2.addFrag(new EventFragment("Events",fontFamily), "Events");
    viewPager2.setAdapter(adapter2);
}


//View Pager fragments setting adapter class
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();//fragment arraylist
    private final List<String> mFragmentTitleList = new ArrayList<>();//title arraylist

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }


    //adding fragments and title method
    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

layout file

                <?xml version="1.0" encoding="utf-8"?>
 <android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
 <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="130dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"/>

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

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

    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="24dp">


            <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/Lavender"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp">

                <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:padding="16dp">


                    <android.support.design.widget.TextInputLayout

                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <EditText
                                android:id="@+id/edtName"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:inputType="text"
                                android:hint="Name" />

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

                    <RadioGroup
                            android:id="@+id/rdbGroup"
                            android:layout_width="match_parent"
                            android:layout_height="25dp"
                            android:layout_marginTop="1dp"
                            android:layout_marginBottom="1dp"
                            android:background="@drawable/round_border"
                            android:orientation="horizontal" >

                        <RadioButton
                                android:id="@+id/rdb1"
                                android:layout_width="wrap_content"
                                android:layout_height="23dp"
                                android:layout_weight="1"
                                android:background="@drawable/bg_blue"
                                android:button="@android:color/transparent"
                                android:textColor="@drawable/txt_color"
                                android:gravity="center"
                                android:paddingBottom="2dp"
                                android:paddingTop="2dp"
                                android:singleLine="true"
                                android:text="radio1"
                                android:checked="true"
                                android:textSize="13sp" />

                        <View
                                android:id="@+id/vSep2"
                                android:layout_width="1dp"
                                android:layout_height="match_parent"
                                android:background="#000000"
                                android:visibility="visible" />

                        <RadioButton
                                android:id="@+id/rdb2"
                                android:layout_width="wrap_content"
                                android:layout_height="23dp"
                                android:layout_weight="1"
                                android:background="@drawable/bg_red"
                                android:button="@android:color/transparent"
                                android:textColor="@drawable/txt_color"
                                android:gravity="center"
                                android:paddingBottom="2dp"
                                android:paddingTop="2dp"
                                android:singleLine="true"
                                android:text="radio2"
                                android:textSize="13sp" />
                    </RadioGroup>
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/SkyBlue"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp">
                <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                    <android.support.design.widget.TabLayout
                            android:id="@+id/tab_layout2"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                    android:background="?attr/colorPrimary"
                     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:tabIndicatorColor="@android:color/holo_red_dark"
                    app:tabIndicatorHeight="4dp"
                            local:tabMode="scrollable" />
                    <android.support.v4.view.ViewPager
                            android:id="@+id/viewPager2"
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:layout_below="@id/tab_layout"/>

                </LinearLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
</android.support.v4.widget.NestedScrollView>

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


<android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        app:headerLayout="@layout/drawer_header_expense"
        app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

You can try this:

ViewPager viewPager2;
TabLayout tabLayout2;
ViewPagerAdapter adapter2;
RadioGroup rdbGroup;
RadioButton rdb1, rdb2;

LinearLayout tabstrip;

int pos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //remaining code
    viewPager2 = (ViewPager) findViewById(R.id.viewPager2);
    setupViewPager2(viewPager2);
    tabLayout2 = (TabLayout) findViewById(R.id.tab_layout2);
    rdbGroup = (RadioGroup) findViewById(R.id.rdbGroup);
    rdb1 = (RadioButton) findViewById(R.id.rdb1);
    rdb2 = (RadioButton) findViewById(R.id.rdb2);

    tabLayout2.setupWithViewPager(viewPager2);//setting tab over viewpager

    //get position of already checked radiobutton
    int radioButtonID = rdbGroup.getCheckedRadioButtonId();
    View radioButton = rdbGroup.findViewById(radioButtonID);
    pos = rdbGroup.indexOfChild(radioButton);

    tabstrip = (LinearLayout) tabLayout2.getChildAt(0);

    //check which radiobutton is already checked and as per its pos disbale or enable the tabs as below
    if (pos == 0) {
        tabLayout2.getTabAt(1).select();
        tabstrip.getChildAt(0).setClickable(false);
        tabstrip.getChildAt(0).setEnabled(false);
    } else if (pos == 1) {
        tabstrip.getChildAt(0).setClickable(true);
        tabstrip.getChildAt(0).setEnabled(true);
    }

    rdbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            pos = rdbGroup.indexOfChild(findViewById(checkedId));

            switch (pos) {
                case 0:
                    tabstrip.getChildAt(0).setClickable(false);
                    tabstrip.getChildAt(0).setEnabled(false);
                    break;
                case 1:
                    tabstrip.getChildAt(0).setClickable(true);
                    tabstrip.getChildAt(0).setEnabled(true);
                    break;
                default:
                    //here add whatever you like
                    tabstrip.getChildAt(0).setClickable(true);
                    tabstrip.getChildAt(0).setEnabled(true);
                    break;
            }
        }
    });


    //Implementing tab selected listener over tablayout
    tabLayout2.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager2.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
            switch (tab.getPosition()) {
                case 0:
                    Log.e("TAG", "TAB1");
                    break;
                case 1:
                    Log.e("TAG", "TAB2");
                    break;
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}

//Setting View Pager
private void setupViewPager2(ViewPager viewPager) {
    expAttList = new ArrayList<COAAccount>();
    expAttList = new ArrayList<COAAccount>();
    adapter2 = new ViewPagerAdapter(getSupportFragmentManager());
    adapter2.addFrag(new AttachmentFragment("Attachments", expAttList, fontFamily), "Attachments");
    adapter2.addFrag(new EventFragment("Events", fontFamily), "Events");
    viewPager2.setAdapter(adapter2);
}


//View Pager fragments setting adapter class
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();//fragment arraylist
    private final List<String> mFragmentTitleList = new ArrayList<>();//title arraylist

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }


    //adding fragments and title method
    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

It's a tricky one.You need to use a custom ViewPager for this.Below is the code. Change your ViewPager in xml to this :

<yourPackage.NonSwipeableViewPager
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:layout_below="@id/tab_layout"/>

Below is the custom view pager class :

public class NonSwipeableViewPager extends ViewPager {
private boolean enabled;

public NonSwipeableViewPager(Context context) {
    super(context);
    this.enabled = true;
}

public NonSwipeableViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.enabled = true;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onInterceptTouchEvent(event);
    }

    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onTouchEvent(event);
    }

    return false;
}

public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
}
}

Now in your activity use below code :

NonSwipeableViewPager viewPager = (NonSwipeableViewPager) findViewById(R.id.pager);
//write code to set viewpager to the tablayout that u have already done.Use the same.
rdbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        pos = rdbGroup.indexOfChild(findViewById(checkedId));
        LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
        switch (pos) {
            case 0:
                viewPager.setCurrentItem(1);
                viewPager.setPagingEnabled(false);
                tabstrip.getChildAt(0).setClickable(false);
                tabstrip.getChildAt(0).setEnabled(false);
                break;
            case 1:
                viewPager.setCurrentItem(0);
                viewPager.setPagingEnabled(true);
                tabstrip.getChildAt(0).setClickable(true);
                tabstrip.getChildAt(0).setEnabled(true);
                break;
            default:
                //here add whatever you like
                break;
        }
    }
});

Use the swicth condition as per your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM