2016-04-07 8 views
0

Benim android etkinliğimde radio button group ve tab layoutviewpager kullanarak var. İki sekme ve 2 radyo düğmesi vardır.Android: Radyo düğmesi başına seçilemez/kaydırılamıyor nasıl tıklatılır

  • ilk sekme ikinci bir olay sekme
  • bir bağlantı uzantısı, bir.

İlk radyo düğmesini işaretlediğimde, kullanıcının ilk sekmeyi görmesine izin verilmiyor, bu yüzden ilk sekmeyi devre dışı bırakmam ve kullanıcı için ikinci sekmeyi göstermem gerekiyor.İkinci radyo düğmesini seçtim İlk sekmenin ve kullanıcının ilk sekmeye erişmesine izin vermek için kullanılır. Sekmeyi kaldırmak istemiyorum, sadece kullanıcının onu kaydırarak veya sekmeyi tıklayarak erişememesi için devre dışı bırakmak istiyorum.

Bu nasıl yapılır? Aşağıda sekme gösterilecek kodum var.

@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 dosya

   <?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

Lütfen tüm kodunuzu radyo düğmeleriyle gönderin – Newbiee

+0

@ Newbiee radyo düğmesi kodunu neden istiyorsunuz? – KJEjava48

+0

böylece tamamen size rehberlik edebilir :( – Newbiee

cevap

1

this.Below olduğu için kodu. Buna xml sizin ViewPager değiştirin :

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

Kullanım Swicth durumu: kodunun altında faaliyet kullanımda Şimdi

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

: Aşağıda

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

özel görünüm çağrı cihazı sınıftır senin ihtiyacına göre.

2

Bu deneyebilirsiniz: Bu özel bir ViewPager kullanmak zor one.You ihtiyaç var

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

vermek bu sonuçlar NullPointerException radyo düğmesi kontrolünü değiştirdi – KJEjava48

+0

Atleast size radyo düğmesi ile xml için tam kodunuzu sağlayın, böylece size yardımcı olabilirim – Drv

+0

ok.I kodumu düzenledim radiobuttons ile – KJEjava48