2016-07-15 40 views
-1

學習Android和現在我堅持了一個簡單的問題。集成教程代碼爲產生NavigationView代碼

我開始一個新的項目,菜單滑動

package com.naradanews.naradanews; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.FragmentActivity; 
import android.support.v7.app.ActionBar; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
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.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

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


     Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar); 
     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public void onBackPressed() { 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

現在,這是教程代碼

package com.learn2crack.tab; 

import android.os.Bundle; 
import android.app.ActionBar; 
import android.app.FragmentTransaction; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 

public class MainActivity extends FragmentActivity { 
    ViewPager Tab; 
    TabPagerAdapter TabAdapter; 
    ActionBar actionBar; 

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

     TabAdapter = new TabPagerAdapter(getSupportFragmentManager()); 

     Tab = (ViewPager)findViewById(R.id.pager); 
     Tab.setOnPageChangeListener(
       new ViewPager.SimpleOnPageChangeListener() { 
        @Override 
        public void onPageSelected(int position) { 

         actionBar = getActionBar(); 
         actionBar.setSelectedNavigationItem(position);     } 
       }); 
     Tab.setAdapter(TabAdapter); 

     actionBar = getActionBar(); 
     //Enable Tabs on Action Bar 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     ActionBar.TabListener tabListener = new ActionBar.TabListener(){ 

      @Override 
      public void onTabReselected(android.app.ActionBar.Tab tab, 
        FragmentTransaction ft) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { 

       Tab.setCurrentItem(tab.getPosition()); 
      } 

      @Override 
      public void onTabUnselected(android.app.ActionBar.Tab tab, 
        FragmentTransaction ft) { 
       // TODO Auto-generated method stub 

      }}; 
      //Add New Tab 
      actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener)); 
      actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener)); 
      actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener)); 

    } 

} 

兩個MainActivity不同類擴展看到。我問如何將教程與我的代碼集成。

+0

問題是每個教程MainActivity類別與不同類延伸。我正在問如何在創建應用程序時做所有這些事情。沒有屏幕截圖bcz我無法開始編碼:) bcz這個問題。我嘗試單獨擴展片段類與mainactivity,但它顯示重複類:( – Kaleel

+0

你完成了其中一個教程。你還讀過[片段](https://developer.android.com/guide/components/fragments.html )。你可以創建擴展'Fragment'自定義類,並把它添加到你的'MainActivity'。也許添加您的教程鏈接,沒有鏈接,沒有截圖,不能想象什麼是你想! –

+0

在Java中,你只能從一個延伸如果你描述你想在一般情況下實現什麼,它會更有幫助 – Rashin

回答

0

你可能想要的是一個ViewPager一起使用TabLayout

這裏有一個簡單的例子,讓你上手的,也許幫助您更好地瞭解它是如何工作的。

在您的MainActivity中,您將致電onCreate()中的setContentView(R.layout.tabbed_activity)

XML可以是這個樣子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/view_pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</LinearLayout> 

,然後在MainActivity你會onCreate初始化這些小部件:

mTabLayout = (TabLayout) findViewById(R.id.tab_layout); 
mViewPager = (ViewPager) findViewById(R.id.view_pager); 

針對這種觀點尋呼機,您將需要一個適配器來處理標籤(在這種情況下爲片段):

public class ViewPagerTabsAdapter extends FragmentPagerAdapter { 

    private List<String> mTabTitles; 
    private List<Fragment> mFragments; 

    public ViewPagerTabsAdapter(FragmentManager fm) { 
     super(fm); 
     mTabTitles = new ArrayList<>(); 
     mFragments = new ArrayList<>(); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return mFragments.get(position); 
    } 

    @Override 
    public int getCount() { 
     return mFragments.size(); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mTabTitles.get(position); 
    } 

    public void addTab(int pos, String title, Fragment fragment) { 
     mTabTitles.add(pos, title); 
     mFragments.add(pos, fragment); 
    } 
} 

,然後在MainActivity#onCreate,您將通過設置它全部加起來跟着片(碎片)的適配器:

public class MainActivity extends AppCompatActivity { 

     private ViewPager mViewPager; 
     private TabLayout mTabLayout; 

     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.tabbed_activity); 

      mTabLayout = (TabLayout) findViewById(R.id.tab_layout); 
      mViewPager = (ViewPager) findViewById(R.id.view_pager); 

      ViewPagerTabsAdapter pagerAdapter = new ViewPagerTabsAdapter(getSupportFragmentManager()); 

      pagerAdapter.addTab(0, "Tab 1", new Fragment()); 
      pagerAdapter.addTab(1, "Tab 2", new Fragment()); 

      mViewPager.setAdapter(pagerAdapter); 
      mTabLayout.setupWithViewPager(mViewPager); 
     } 
} 

你說,你是一個初學者,但這裏有一個教程,帶你經歷了很多的關於這個的事情。我想如果你仔細閱讀,你會設法解決你的問題。 http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

但是你應該也看過一些關於活動和片段。 https://developer.android.com/guide/components/activities.html