1

enter image description here如何使用工具欄上的導航抽屜和自定義圖標繪製材質滑動選項卡?

我想要一個完全像這樣的佈局。我已經搜索了很多關於此,並嘗試以下鏈接。

這是我的代碼,我到目前爲止已經試過。

MainActivity

import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.astuetz.PagerSlidingTabStrip; 

public class MainActivity extends AppCompatActivity { 

    NavigationView navigationView; 
    DrawerLayout drawerLayout; 
    ActionBarDrawerToggle actionBarDrawerToggle; 
    Toolbar mToolbar; 


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

     mToolbar = (Toolbar) findViewById(R.id.toolbar); 

     setSupportActionBar(mToolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     navigationView = (NavigationView) findViewById(R.id.nvView); 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

     ViewPager pager = (ViewPager) findViewById(R.id.pager); 
     if (pager != null) { 
      pager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager())); 
     } 

     // Bind the tabs to the ViewPager 
     PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs); 
     if (tabs != null) { 
      if (pager != null) { 
       tabs.setViewPager(pager); 
      } 
     } 



     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 

       if (menuItem.isChecked()) menuItem.setChecked(false); 
       else menuItem.setChecked(true); 

       drawerLayout.closeDrawers(); 

       switch (menuItem.getItemId()) { 

        case R.id.home: 

         Toast.makeText(getApplicationContext(), "Home Selected", Toast.LENGTH_SHORT).show(); 

         return true; 

        case R.id.about: 


         Toast.makeText(getApplicationContext(), "About Us Selected", Toast.LENGTH_SHORT).show(); 
         return true; 

        case R.id.Share: 


         Toast.makeText(getApplicationContext(), "Share Selected", Toast.LENGTH_SHORT).show(); 
         return true; 

        default: 

         Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show(); 
         return true; 

       } 
      } 
     }); 



     actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close) { 

      @Override 
      public void onDrawerClosed(View drawerView) { 

       super.onDrawerClosed(drawerView); 
      } 

      @Override 
      public void onDrawerOpened(View drawerView) { 

       super.onDrawerOpened(drawerView); 

      } 
     }; 
    } 



    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
      case android.R.id.home: 
       int id = item.getItemId(); 

       if (drawerLayout.isDrawerOpen(navigationView)) 
       { 
        drawerLayout.closeDrawer(navigationView); 
       } 
       else 
       { 
        drawerLayout.openDrawer(navigationView); 
       } 

     } 
     return super.onOptionsItemSelected(item); 

    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     actionBarDrawerToggle.syncState(); 
    } 





    public static class SampleFragmentPagerAdapter extends FragmentPagerAdapter{ 


     final int PAGE_COUNT = 3; 
     private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" }; 

     public SampleFragmentPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

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

     @Override 
     public Fragment getItem(int position) { 
      return PageFragment.newInstance(position + 1); 
     } 

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

     public static class PageFragment extends Fragment { 
      private int mPage; 
      public static final String ARG_PAGE = "ARG_PAGE"; 

      public static PageFragment newInstance(int page) { 
       Bundle args = new Bundle(); 
       args.putInt(ARG_PAGE, page); 
       PageFragment fragment = new PageFragment(); 
       fragment.setArguments(args); 
       return fragment; 
      } 

      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       mPage = getArguments().getInt(ARG_PAGE); 
      } 

      // Inflate the fragment layout we defined above for this fragment 
// Set the associated text for the title 
      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
       View view = inflater.inflate(R.layout.custom, container, false); 
       TextView tvTitle = (TextView) view.findViewById(R.id.text); 
       tvTitle.setText("Fragment #" + mPage); 
       return view; 
      } 


    } 
} 
} 

activity_main

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:focusableInTouchMode="true"> 

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

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 


      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimaryDark" 
       app:layout_scrollFlags="enterAlways|scroll" /> 

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

     <com.astuetz.PagerSlidingTabStrip 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="48dip" 
      /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tabs" 
      tools:context=".MainActivity" /> 

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

    <android.support.design.widget.NavigationView 
     android:id="@+id/nvView" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@android:color/white" 
     app:menu="@menu/drawer" /> 

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

有了這個代碼,我剛開始導航抽屜裏,連導航圖標沒有viewpager我用這個來實現。

+0

試試這個教程.... http://www.androidhive.info/2015/09/android-material-design-working-帶標籤/ –

回答