2016-11-29 1 views
5

私は画面に7つの日付タブを持っています。ただし、その日付が別の月になると、ユーザーはそのタブをクリックできません。私はそれらを見えるようにしたいがクリック可能にしない。ユーザーがタブの1つをクリックできないようにする

This is how the tabs look like

これは私のコードの一部です:

public void setupViewPager(ViewPager viewPager, ArrayList<String> id, ArrayList<String> tasks, 
          ArrayList<Double> mondayHours, ArrayList<Double> tuesdayHours, 
          ArrayList<Double> wednesdayHours, ArrayList<Double> thursdayHours, 
          ArrayList<Double> fridayHours, ArrayList<Double> saturdayHours, 
          ArrayList<Double> sundayHours) { 
    Bundle bundle = new Bundle(); 
    bundle.putStringArrayList(EXTRA_CHECKED_TASK_ID, id); 
    bundle.putStringArrayList(EXTRA_CHECKED_TASKS, tasks); 
    bundle.putSerializable(EXTRA_MONDAY, mondayHours); 
    bundle.putSerializable(EXTRA_TUESDAY, tuesdayHours); 
    bundle.putSerializable(EXTRA_WEDNESDAY, wednesdayHours); 
    bundle.putSerializable(EXTRA_THURSDAY, thursdayHours); 
    bundle.putSerializable(EXTRA_FRIDAY, fridayHours); 
    bundle.putSerializable(EXTRA_SATURDAY, saturdayHours); 
    bundle.putSerializable(EXTRA_SUNDAY, sundayHours); 

    final String MON = "MON" + "\n" + MainActivity.sevenDatesList.get(0); 
    final String TUE = "TUE" + "\n" + MainActivity.sevenDatesList.get(1); 
    final String WED = "WED" + "\n" + MainActivity.sevenDatesList.get(2); 
    final String THU = "THU" + "\n" + MainActivity.sevenDatesList.get(3); 
    final String FRI = "FRI" + "\n" + MainActivity.sevenDatesList.get(4); 
    final String SAT = "SAT" + "\n" + MainActivity.sevenDatesList.get(5); 
    final String SUN = "SUN" + "\n" + MainActivity.sevenDatesList.get(6); 

    adapter = new ViewPagerAdapter(getSupportFragmentManager(), bundle); 
    adapter.addFragment(new MondayFragment(), MON); 
    adapter.addFragment(new TuesdayFragment(), TUE); 
    adapter.addFragment(new WednesdayFragment(), WED); 
    adapter.addFragment(new ThursdayFragment(), THU); 
    adapter.addFragment(new FridayFragment(), FRI); 
    adapter.addFragment(new SaturdayFragment(), SAT); 
    adapter.addFragment(new SundayFragment(), SUN); 

    viewPager.setAdapter(adapter); 
} 

これは私のXMLコードです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.ada.foresight.TimesheetActivity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:paddingTop="8dp" 
     android:paddingBottom="8dp" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin"> 

     <TextView 
      android:id="@+id/lblWeekMsg" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/week" 
      android:textAppearance="?attr/textAppearanceListItem" /> 
     <TextView 
      android:id="@+id/lblWeekNo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toEndOf="@id/lblWeekMsg" 
      android:layout_alignBaseline="@id/lblWeekMsg" 
      android:text="" 
      android:textAppearance="?attr/textAppearanceListItem" /> 

    </RelativeLayout> 
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/coordinator_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="30"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tab_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:tabTextColor="@android:color/white" 
       app:tabMode="scrollable" 
       app:tabGravity="fill" /> 

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

     <android.support.v4.view.ViewPager 
      android:id="@+id/view_pager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    </android.support.design.widget.CoordinatorLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin"> 

    </RelativeLayout> 
</LinearLayout> 

私は同様の問題に研究し、私たちは、タブを作成するために異なるアプローチを使用していくつか私は理解していない。

@Override 
void selectTab(final Tab tab, boolean updateIndicator){ 
    if (tab.getPosition() == <selectable_position>){ 
     super.selectTab(tab, updateIndicator) 
    } 
} 

ただし、このロジックはまたViewPagerを経由してそのタブにスワイプ無効になりますので注意すべきである:あなたがTabLayoutクラスを拡張して、このような

void selectTab(final Tab tab, boolean updateIndicator) 

メソッドをオーバーライドすることができますResearch 1

答えて

1

これは、タブまたはカスタマイズタブを有効または無効にするときに行うことです。

//get tab view 
ViewGroup vg = (ViewGroup) tab_event.getChildAt(0); 
//get number of tab 
int tabsCount = vg.getChildCount(); 
//loop the tab 
for (int j = 0; j < tabsCount; j++) { 
    //get view of selected tab 
    ViewGroup vgTab = (ViewGroup) vg.getChildAt(j); 

    if(j==DESIRED_POSITION){ 
     //disable the selected tab 
     vgTab.setEnable(false); 
    }  
} 

ビューページで添付する場合、ビューページのスワイプを無効にする必要があります。表示されていない場合でも、無効にするタブにスワイプできます。

+0

はtab_eventでtabLayoutですか? –

+0

yes.its Tablayout。 'protected TabLayout tab_event =(TabLayout)findViewById(R.id.tabs)'; – ZeroOne

+0

ありがとう!あなたのソリューションは動作します! –

2

詳細については、sourceを参照してください。

関連する問題