2016-04-07 5 views
0

私のAndroidのアクティビティでは、私はradio button grouptab layoutviewpagerを使用しています。 2つのタブと2つのラジオボタンがあります。Android:ラジオボタンごとに選択/スワイブルできないタブを作る方法

  • 最初のタブは、第1のイベントタブである取付タブ
  • あります。

私は、最初のラジオボタンをチェックすると、ユーザは、最初のタブを見ることができたので、私は最初のタブを無効にする必要があり、私は必要な2つ目のラジオボタンを選択しuser.Ifための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

ラジオボタンでコード全体を投稿してください – Newbiee

+0

@Newbieeなぜラジオボタンコードが必要ですか? – KJEjava48

+0

私は完全にあなたを導くことができるように:( – Newbiee

答えて

1

ですコード。これに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条件を使用しますあなたの要件に応じて。

2

あなたはこの試すことができます:それはトリッキーone.Youがthis.BelowのカスタムViewPagerを使用する必要があります

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

この結果NullPointerExceptionはラジオボタンのチェックを変更しました – KJEjava48

+0

あなたはxmlのラジオボタンを完全に提供していますので、お手数ですが – Drv

+0

ok.I私のコードを編集しましたラジオボタンで – KJEjava48