2017-07-07 55 views
-1

我需要爲自定義選項卡布局提供水平滑動導航。我認爲這段代碼可以幫助你解決這個問題。有3個片段附加到viewpageradapter。我已經聲明Viewpager類的viewpager對象,但問題仍然存在。如何將片段從片段管理器傳遞到控制面板活動中的viewpager

public class DashboardActivity extends AppCompatActivity { 

// private SectionsPagerAdapter mSectionsPagerAdapter; 
// private ViewPager mViewPager; 


    ViewPagerAdapter viewPagerAdapter; 

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle("Toolbar"); 
     setSupportActionBar(toolbar); 
     TabLayout tabLayout; 
     ViewPager viewPager; 
     tabLayout=(TabLayout) findViewById(R.id.tabs); 
     viewPager=(ViewPager)findViewById(R.id.ViewPager); 

     viewPager= new ViewPagerAdapter(getSupportFragmentManager()); 
     viewPagerAdapter.addFragments(new HomeFragment(),"HOME"); 
     viewPagerAdapter.addFragments(new TopPaidFragment(),"TOP PAID"); 
     viewPagerAdapter.addFragments(new TopFreeFragment(),"TOP FREE"); 

     viewPager = (ViewPager)findViewById(R.id.ViewPager); 
     viewPager.setAdapter(viewPagerAdapter); 
     tabLayout.setupWithViewPager(viewPager); 




    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_dashboard, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.action_settings: 


       return true; 

      case R.id.add_pic: 

       return true; 
      case R.id.Add_Story: 

       return true; 
      case R.id.delete: 

       return true; 
      case R.id.home: 
       NavUtils.navigateUpFromSameTask(this); 


      default:`enter code here` 
       // If we got here, the user's action was not recognized. 
       // Invoke the superclass to handle it. 
       return super.onOptionsItemSelected(item); 

     } 
    } 


} 

`enter code here`layout_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" 
    android:orientation="vertical"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay" 
     android:id="@+id/appBarLayout"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 
     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabMode="fixed" 
      app:tabGravity="fill" 
      /> 
     <android.support.v4.view.ViewPager 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:id="@+id/ViewPager"/> 


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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/floatingActionButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginBottom="42dp" 
     android:layout_marginEnd="33dp" 
     android:layout_marginRight="33dp" 
     android:clickable="true" 
     app:backgroundTint="@android:color/primary_text_dark" 
     app:fabSize="mini" 
     app:srcCompat="@drawable/ic_plus_circle" 
     android:hint="@string/add_story"/> 

</RelativeLayout> 
+1

你可以發佈您的錯誤日誌嗎? –

+0

viewPagerAdapter實例爲空。此代碼拋出NullPointerException。 viewPager = new ViewPagerAdapter(getSupportFragmentManager()); – mustafasevgi

回答

1

您正在嘗試設置適配器對象viewpager

改變這一行

viewPager= new ViewPagerAdapter(getSupportFragmentManager());

viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

+0

感謝解決方案的工作..但現在整個屏幕的背景顏色已被更改爲選項卡背景顏色 – user3323487

+0

因此,因爲您已將ViewPager放在您的AppBarLayout中,只需從那裏取出。 – Safeer

+0

非常感謝,現在它的工作正常。 – user3323487

相關問題