0

我正嘗試使用滑動選項卡創建導航抽屜。但我的viewpager工作不正常。我只能選擇標籤。 Viewpager無法加載任何片段。所以我無法刷卡。如果我改變viewpager的高度,它隱藏選項卡並加載所有的片段。Swipable選項卡不是在Android中滑動

Activity_main.xml 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical"> 
<include 
    android:id="@+id/app_bar" 
    layout="@layout/app_bar" /> 



<android.support.v4.widget.DrawerLayout 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/drawer_layout"> 
    <FrameLayout 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     > 


     <android.support.design.widget.TabLayout 
      android:id="@+id/sliding_tab" 
      style="@style/MyCustomTabLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      app:tabMode="scrollable" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewPager" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:background="@android:color/white" /> 

    </FrameLayout> 

    <fragment 
     android:id="@+id/nav_drawer" 
     android:name="com.example.usaukglu.tablayoyt.NavigationDrawer" 
     android:layout_width="@dimen/drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/navigation_drawer" 
     tools:layout="@layout/navigation_drawer" /> 

</android.support.v4.widget.DrawerLayout> 


</LinearLayout> 

這是我MainActivity.java代碼:

package com.example.usaukglu.tablayoyt; 

import android.support.design.widget.TabLayout; 
import android.support.v4.view.ViewPager; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.MenuItem; 
import android.widget.LinearLayout; 

public class MainActivity extends AppCompatActivity { 
private Toolbar toolbar; 
NavigationDrawer navigationDrawer; 
DrawerLayout drawerLayout; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_appbar); 

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); 
    TabLayout tabLayout= (TabLayout) findViewById(R.id.sliding_tab); 
    toolbar= (Toolbar) findViewById(R.id.app_bar); 
    drawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout); 

    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),MainActivity.this)); 
    tabLayout.setupWithViewPager(viewPager); 

    navigationDrawer= (NavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.nav_drawer); 
    navigationDrawer.setUp(R.id.nav_drawer,drawerLayout,toolbar); 

} 

} 

這裏是我的ViewpagerAdapter.java代碼

package com.example.usaukglu.tablayoyt; 

import android.content.Context; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.app.FragmentStatePagerAdapter; 


class ViewPagerAdapter extends FragmentPagerAdapter { 
final int PAGE_COUNT = 3; 
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" }; 
private Context context; 

public ViewPagerAdapter(FragmentManager fm, Context context) { 
    super(fm); 
    this.context = context; 
} 

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

@Override 
public Fragment getItem(int position) { 
    Fragment fragment=null; 
    if (position==0){ 
     fragment=new FragmentPage(); 
    } 
    if (position==1){ 
     fragment=new FragmentExpense(); 
    } 
    if (position==2){ 
     fragment=new FragmentIncome(); 
    } 
    return fragment; 
} 

@Override 
public CharSequence getPageTitle(int position) { 
    // Generate title based on item position 
    return tabTitles[position]; 

} 
} 
+0

FragmentPage,FragmentExpense和FragmentIncome所有這些設置與他們的XML視圖? – Sanf0rd

+0

是的所有這些都設置 – Fatema

回答

0

你不應該使用的FrameLayout作爲標籤+尋呼機容器。 FrameLayout主要用於將視圖置於其他視圖之上。要修復它,你應該改變這個FrameLayout到垂直方向的LinearLayout。此外,視圖頁面高度應該是match_parent。

+0

謝謝。有效。 – Fatema

+0

將amswer標記爲正確。 – Sanf0rd