-1

我想實現微調以更改我的選項卡。 我從here複製了此代碼,並作了一個包含帶有微調工具欄的工具欄的更改。如何使用微調器更改TABS

我相信我需要爲微調器實現一個偵聽器,也許是一個setOnItemSelectedListener,但問題是如何更改與微調器上選擇的項目位置的選項卡?

public class MainActivity extends FragmentActivity { 
 
    
 
    /** 
 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
 
    * fragments for each of the sections. We use a 
 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
 
    * will keep every loaded fragment in memory. If this becomes too memory 
 
    * intensive, it may be best to switch to a 
 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
 
    */ 
 
    SectionsPagerAdapter mSectionsPagerAdapter; 
 
    
 
    /** 
 
    * The {@link ViewPager} that will host the section contents. 
 
    */ 
 
    ViewPager mViewPager; 
 
    
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
    
 
     // Create the adapter that will return a fragment for each of the three 
 
     // primary sections of the app. 
 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
 
    
 
     // Set up the ViewPager with the sections adapter. 
 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarListaJogos); 
 
     setSupportActionBar(toolbar); 
 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 
 
     
 
     Spinner sp = (Spinner) findViewById(R.id.spinner_nav); 
 
     List<String> listaMenu = new ArrayList<>(); 
 
     listMenu.add("Section 1"); 
 
     listMenu.add("Section 2"); 
 
     listMenu.add("Section 3"); 
 

 
     
 
     ArrayAdapter<String> adapter = 
 
     \t \t new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, listaMenu); 
 
     
 
     sp.setAdapter(adapter); 
 
    
 
    } 
 
    
 
    @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; 
 
    } 
 
    
 
    /** 
 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
 
    * one of the sections/tabs/pages. 
 
    */ 
 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 
 
    
 
     public SectionsPagerAdapter(FragmentManager fm) { 
 
      super(fm); 
 
     } 
 
    
 
     @Override 
 
     public Fragment getItem(int position) { 
 
      // getItem is called to instantiate the fragment for the given page. 
 
      // Return a DummySectionFragment (defined as a static inner class 
 
      // below) with the page number as its lone argument. 
 
      Fragment fragment = new DummySectionFragment(); 
 
      Bundle args = new Bundle(); 
 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
 
      fragment.setArguments(args); 
 
      return fragment; 
 
     } 
 
    
 
     @Override 
 
     public int getCount() { 
 
      // Show 3 total pages. 
 
      return 3; 
 
     } 
 
    
 
     @Override 
 
     public CharSequence getPageTitle(int position) { 
 
      Locale l = Locale.getDefault(); 
 
      switch (position) { 
 
      case 0: 
 
       return getString(R.string.title_section1).toUpperCase(l); 
 
      case 1: 
 
       return getString(R.string.title_section2).toUpperCase(l); 
 
      case 2: 
 
       return getString(R.string.title_section3).toUpperCase(l); 
 
      } 
 
      return null; 
 
     } 
 
    } 
 
    
 
    /** 
 
    * A dummy fragment representing a section of the app, but that simply 
 
    * displays dummy text. 
 
    */ 
 
    public static class DummySectionFragment extends Fragment { 
 
     /** 
 
     * The fragment argument representing the section number for this 
 
     * fragment. 
 
     */ 
 
     public static final String ARG_SECTION_NUMBER = "section_number"; 
 
    
 
     public DummySectionFragment() { 
 
     } 
 
    
 
     @Override 
 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
 
      View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false); 
 
      TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label); 
 
      dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); 
 
      return rootView; 
 
     } 
 
    } 
 
    
 
}

佈局XML爲在MainActivity:

<?xml version="1.0" encoding="utf-8"?> 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res/br.com.pixells.simuladorbr" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="fill_parent" 
 
    android:layout_height="fill_parent" 
 
    android:orientation="vertical" > 
 
    
 
    <android.support.v7.widget.Toolbar 
 
\t \t xmlns:android="http://schemas.android.com/apk/res/android" 
 
\t \t android:id="@+id/toolbarListaJogos" 
 
\t \t android:layout_width="match_parent" 
 
\t \t android:layout_height="wrap_content" 
 
\t \t android:background="#DC143C" 
 
\t \t app:subtitle="Teste" 
 
\t \t app:theme="@style/Theme.Abar.Widget" 
 
\t \t app:title="ToolBar"> 
 
     <Spinner 
 
      android:id="@+id/spinner_nav" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" /> 
 
    </android.support.v7.widget.Toolbar> 
 
    
 
\t \t <android.support.v4.view.ViewPager 
 
\t \t  xmlns:android="http://schemas.android.com/apk/res/android" 
 
\t \t \t android:id="@+id/pager" 
 
\t \t \t android:layout_width="match_parent" 
 
\t \t \t android:layout_height="match_parent" 
 
\t \t \t tools:context=".MainActivity" > 
 
\t \t 
 
\t \t 
 
\t \t 
 
<!-- 
 
This title strip will display the currently visible page title, as well as the page 
 
titles for adjacent pages. 
 
--> 
 
\t \t 
 
\t \t \t <android.support.v4.view.PagerTitleStrip 
 
\t \t \t \t android:id="@+id/pager_title_strip" 
 
\t \t \t \t android:layout_width="match_parent" 
 
\t \t \t \t android:layout_height="wrap_content" 
 
\t \t \t \t android:layout_gravity="top" 
 
\t \t \t \t android:background="#33b5e5" 
 
\t \t \t \t android:paddingBottom="4dp" 
 
\t \t \t \t android:paddingTop="4dp" 
 
\t \t \t \t android:textColor="#fff" /> 
 
\t \t 
 
\t \t 
 
\t \t </android.support.v4.view.ViewPager> 
 

 
</LinearLayout>

+0

使用此標籤模型不可行嗎? – caezar 2014-12-05 15:20:44

+0

多張貼:http://pt.stackoverflow.com/questions/42959/como-posso-alternar-tabs-usando-um-spinner – pnuts 2015-11-05 20:12:03

回答

1

我解決了這個問題,這一點:

mViewPager.setCurrentItem(position); 
+0

不要忘記接受你的答案。 – 2014-12-09 18:22:22