2016-03-28 2 views
1

В настоящее время я работаю над добавлением вкладок в свою деятельность. Для этого я использую Google SlidingTabLayout и SlidingTabStrip.Textview in Fragment не работает

Мой фрагмент XML выглядит следующим образом:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" > > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/textViewTab" /> 


</RelativeLayout> 

Мое содержимое XML выглядит следующим образом:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.musicisfun.allaboutfootball.MainActivity" 
    tools:showIn="@layout/activity_main"> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout 
     android:layout_width="match_parent" 
     android:id="@+id/tabs" 
     android:layout_height="wrap_content"> 
    </com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout> 

    <android.support.v4.view.ViewPager 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     /> 

</LinearLayout> 

</RelativeLayout> 

и мой код выглядит следующим образом:

public static class TabFragment extends Fragment{ 

     public static TabFragment getInstance(int position){ 
      TabFragment tabFragment = new TabFragment(); 
      Bundle bundle = new Bundle(); 
      bundle.putInt("site",position); 
      tabFragment.setArguments(bundle); 
      return tabFragment; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View layout = inflater.inflate(R.layout.fragment_tab,container,false); 
      TextView textViewTab = (TextView) layout.findViewById(R.id.textViewTab); 
      Bundle bundle = getArguments(); 
      if (bundle!=null){ 
       textViewTab.setText("Current Tab is" + bundle.getInt("site")); 

      } 
      return layout; 
     } 
    } 

Мой адаптер фрагмент выглядит например:

class TabPagerAdaptor extends FragmentPagerAdapter{ 

     String[] tab_names; 
     public TabPagerAdaptor(FragmentManager fm) { 
      super(fm); 
      tab_names=getResources().getStringArray(R.array.feed_names); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      TabFragment tabFragment = TabFragment.getInstance(position); 
      return tabFragment; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 

      return tab_names[position]; 
     } 

     @Override 
     public int getCount() { 
      return 2; 
     } 


    } 

и это, как я вызова вкладки:

mViewPager= (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(new TabPagerAdaptor(getSupportFragmentManager())); 
    mTabs = (SlidingTabLayout) findViewById(R.id.tabs); 
    mTabs.setViewPager(mViewPager); 

Но когда я запускаю код, я не могу найти TextView показываются, хотя две вкладки работают отлично.

+0

Вы можете разместить код, в котором вы устанавливаете адаптер ViewPager? –

+0

Я обновил код – nathandrake

ответ

0

Вы не можете просматривать TextView, поскольку высота ViewPager в вашем XML 0dp -

<android.support.v4.view.ViewPager 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     /> 

вы должны изменить что либо wrap_content или match_parent -

<android.support.v4.view.ViewPager 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 

    /> 

Кроме того, Ваши LinearLayout годов ориентация horizontal и ширина SlidingTabLayout - match_parent. SlidingTabLayout занимает всю ширину экрана, не оставляя места для отображения Viewpager.

Ваша ориентация LinearLayout должна быть вертикальной не горизонтально

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout 
     android:layout_width="match_parent" 
     android:id="@+id/tabs" 
     android:layout_height="wrap_content"> 
    </com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout> 

    <android.support.v4.view.ViewPager 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 

</LinearLayout> 
+0

Я внес изменения в match_parent и все тот же результат. – nathandrake

+0

Отредактирован ответ. –

+0

Я сделал это горизонтально, так как студия давала ошибку, когда пыталась установить ее вертикально, но теперь ее работая отлично. – nathandrake

Смежные вопросы