在我的Android活动中,我有radio button group
和tab layout
使用viewpager
。
有两个标签和2个单选按钮。
当我选中第一个单选按钮时,不允许用户看到第一个选项卡,所以我需要禁用第一个选项卡并显示用户的第二个选项卡。如果我选择了第二个单选按钮,我需要启用第一个标签和用户可以访问第一个标签。我不想删除标签,我只想禁用它,以便用户无法通过滑动或点击标签来访问它。
@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
档案
<?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>
答案 0 :(得分:2)
你可以试试这个:
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);
}
}
答案 1 :(得分:1)
这是一个棘手的问题。你需要为此使用自定义ViewPager.Below是代码。 将xml中的ViewPager更改为:
<yourPackage.NonSwipeableViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="@id/tab_layout"/>
以下是自定义视图分页器类:
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;
}
}
现在在您的活动中使用以下代码:
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;
}
}
});
根据您的要求使用swicth条件。