1

私のアプリのタブ内にタブを作成したいと思います。 ViewPagerと一緒にMainActivity XMLファイルにTabLayoutを作成しました。次に、対応するViewPagerで内側のTabLayoutを含む別のXMLファイルを作成しました。今、私のアプリを実行すると、私の最初のタブ(ホームアイコンのタブ)を除いて、他のすべてのタブの膨らんだビューを見ることができます。また、私の内側のタブの1つが選択されていて、表示されていないときに表示されるCardViewがあります。助けてください! これは私のMainActivity.javaコードです:ViewPagerはすべてのXMLレイアウトファイルを膨らませません

public class MainActivity extends AppCompatActivity { 

    ViewPager viewPagerOuter, viewPagerInner; 
    TabLayout tabLayoutouter, tabLayoutinner; 

    public static class InnerFragment extends Fragment{ 
    private static final String TAB_POSITION = "tab_position"; 
    View rootView; 
    public InnerFragment(){ 

    } 
    public static InnerFragment newInstance(int tabPosition){ 
     InnerFragment fragment = new InnerFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(TAB_POSITION, tabPosition); 
     fragment.setArguments(args); 
     return fragment; 
    } 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     Bundle args = getArguments(); 
     int tabPosition = args.getInt(TAB_POSITION); 
     switch (tabPosition){ 
      case 0:{ 
       rootView = inflater.inflate(R.layout.card_view_layout, container, false); 
       break; 
      } 
      case 1:{ 
       TextView tv = new TextView(getActivity()); 
       tv.setGravity(Gravity.CENTER); 
       tv.setText("Text in tab #" + tabPosition); 
       rootView = tv; 
       break; 
      } 
     } 
     return rootView; 
    } 
} 
static class InnerPagerAdapter extends FragmentStatePagerAdapter{ 
    public InnerPagerAdapter(FragmentManager fm){ 
     super(fm); 
    } 
    @Override 
    public Fragment getItem(int position) { 
     return InnerFragment.newInstance(position); 
    } 

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

    @Override 
    public CharSequence getPageTitle(int position){ 
     String x = ""; 
     switch (position){ 
      case 0: 
       x = "feed"; break; 
      case 1: 
       x = "events"; break; 
     } 
     return x; 
    } 
} 

public static class OuterFragment extends Fragment{ 
    private static final String TAB_POSITION1 = "tab_position"; 
    View rootView; 
    public OuterFragment(){ 

    } 
    public static OuterFragment newInstance(int tabPosition){ 
     OuterFragment fragment1 = new OuterFragment(); 
     Bundle args1 = new Bundle(); 
     args1.putInt(TAB_POSITION1, tabPosition); 
     fragment1.setArguments(args1); 
     return fragment1; 
    } 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater1, ViewGroup container1, Bundle savedInstanceState){ 
     Bundle args1 = getArguments(); 
     int tabPosition = args1.getInt(TAB_POSITION1); 
     switch (tabPosition){ 
      case 0:{ 
       rootView = inflater1.inflate(R.layout.inner_tab_layout, container1, false); 
       break; 
      } 
      default:{ 
       TextView tv = new TextView(getActivity()); 
       tv.setGravity(Gravity.CENTER); 
       tv.setText("Text in tab #" + tabPosition); 
       rootView = tv; 
       break; 
      } 
     } 
     return rootView; 
    } 
} 
static class OuterPagerAdapter extends FragmentStatePagerAdapter{ 
    public OuterPagerAdapter(FragmentManager fm){ 
     super(fm); 
    } 
    @Override 
    public Fragment getItem(int position) { 
     return OuterFragment.newInstance(position); 
    } 

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

    @Override 
    public CharSequence getPageTitle(int position) {return null;} 
} 
final int icons[] = new int[]{R.drawable.ic_chat_black_24dp, R.drawable.ic_home_black_24dp, R.drawable.ic_notifications_black_24dp, R.drawable.ic_person_black_24dp}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    InnerPagerAdapter innerPagerAdapter = new InnerPagerAdapter(getSupportFragmentManager()); 
    OuterPagerAdapter outerPagerAdapter = new OuterPagerAdapter(getSupportFragmentManager()); 

    viewPagerOuter = (ViewPager) findViewById(R.id.viewpagerouter); 
    viewPagerOuter.setAdapter(outerPagerAdapter); 
    tabLayoutouter = (TabLayout) findViewById(R.id.tablayoutouter); 

    final LayoutInflater factory = getLayoutInflater(); 
    final View view = factory.inflate(R.layout.inner_tab_layout, null); 
    viewPagerInner = (ViewPager) view.findViewById(R.id.viewpagerinner); 
    viewPagerInner.setAdapter(innerPagerAdapter); 
    tabLayoutinner = (TabLayout) view.findViewById(R.id.tabLayoutinner); 
    tabLayoutinner.setupWithViewPager(viewPagerInner); 
} 
} 

そして、これはMainActivityの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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tablayoutouter" 
     android.support.tabIndicatorColor="?attr/colorPrimary" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark" 
     app:tabGravity="fill" 
     app:tabIndicatorColor="@color/colorPrimary" 
     app:tabSelectedTextColor="@android:color/background_light"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpagerouter" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

    </android.support.v4.view.ViewPager> 

</LinearLayout> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="16dp" 
    android:layout_marginBottom="16dp" 
    android:src="@drawable/ic_create_black_24dp" /> 

</RelativeLayout> 

これはinner_tab_layout.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" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tabLayoutinner" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabGravity="fill" 
    app:tabIndicatorColor="@color/colorPrimaryDark" 
    app:tabSelectedTextColor="@color/colorBlack"> 
</android.support.design.widget.TabLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpagerinner" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 

</LinearLayout> 

である私は、このような長いを投稿するため申し訳ありませんコードのビット。どうか気にしないでください。

答えて

0

viewPagerInnertabLayoutinnerおよびinnerPagerAdapterコードをOuterFragmentに移動してください。

1.あなたOuterFragment次のようにアップデート:以下のように

public static class OuterFragment extends Fragment { 
    private static final String TAB_POSITION1 = "tab_position"; 
    View rootView; 

    ViewPager viewPagerInner; 
    TabLayout tabLayoutinner; 

    public OuterFragment(){ 

    } 
    public static OuterFragment newInstance(int tabPosition){ 
     OuterFragment fragment1 = new OuterFragment(); 
     Bundle args1 = new Bundle(); 
     args1.putInt(TAB_POSITION1, tabPosition); 
     fragment1.setArguments(args1); 
     return fragment1; 
    } 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater1, ViewGroup container1, Bundle savedInstanceState){ 
     Bundle args1 = getArguments(); 
     int tabPosition = args1.getInt(TAB_POSITION1); 
     switch (tabPosition){ 
      case 0:{ 
       rootView = inflater1.inflate(R.layout.inner_tab_layout, container1, false); 

       viewPagerInner = (ViewPager) rootView.findViewById(R.id.viewpagerinner); 
       tabLayoutinner = (TabLayout) rootView.findViewById(R.id.tabLayoutinner); 

       InnerPagerAdapter innerPagerAdapter = new InnerPagerAdapter(getActivity().getSupportFragmentManager()); 

       viewPagerInner.setAdapter(innerPagerAdapter); 
       tabLayoutinner.setupWithViewPager(viewPagerInner); 

       break; 
      } 
      default:{ 
       TextView tv = new TextView(getActivity()); 
       tv.setGravity(Gravity.CENTER); 
       tv.setText("Text in tab #" + tabPosition); 
       rootView = tv; 
       break; 
      } 
     } 
     return rootView; 
    } 
} 

2.更新onCreate()方法:ケース0で

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    viewPagerOuter = (ViewPager) findViewById(R.id.viewpagerouter); 
    tabLayoutouter = (TabLayout) findViewById(R.id.tablayoutouter); 

    OuterPagerAdapter outerPagerAdapter = new OuterPagerAdapter(getSupportFragmentManager()); 

    viewPagerOuter.setAdapter(outerPagerAdapter); 
    tabLayoutouter.setupWithViewPager(viewPagerOuter); 
} 

ホープ、これは動作します〜

+0

、静的コンテキストからgetSupportFragmentManager()にアクセスすることはできません。 – thapro

+0

getActivity()。getSupportFragmentManager();を使用します。私は私の答えを更新しました。 – FAT

+0

小さな問題が発生しました。外側のTabLayoutの最後のタブにスクロールして最初に戻ったら、カードビューは表示されません。また、内側のTabLayoutのタブの数が増えました...なぜ考えられないのでしょうか...任意のアイデアですか? – thapro

関連する問題