2015-03-02 61 views
0

我是Android的新手。我正在製作一個使用Tabs的新應用程序。 現在,我想要在選項卡中顯示不同的片段。具有不同片段的多個選項卡

請注意,我的MinSDK的是Android 4.4奇巧,所以我不會使用過時的方法

我的代碼是:

MainActivity.java

public class MainActivity extends ActionBarActivity { 
SectionsPagerAdapter mSectionsPagerAdapter; 

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 activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

} 


@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_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; 
    } 

    if(id == R.id.search) 
    { 
     Toast.makeText(getApplicationContext(),"You tapped on SEARCH !", Toast.LENGTH_LONG).show(); 
    } 

    return super.onOptionsItemSelected(item); 
} 


/** 
* 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 PlaceholderFragment (defined as a static inner class below). 


     return PlaceholderFragment.newInstance(position + 1); 
    } 

    @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 placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_1, container, false); 

     Button b1 = (Button) rootView.findViewById(R.id.b1); 
     b1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getActivity(), "Done", Toast.LENGTH_LONG).show(); 
      } 
     }); 
     return rootView; 
    } 
} 
} 

Fragment_Main.xml

<RelativeLayout 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" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity$PlaceholderFragment"> 

<EditText 
    android:id="@+id/et1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter your Name (Fragment Main"/> 

<EditText 
    android:id="@+id/et2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/et1" 
    android:hint="Enter your ID (Fragment Main)"/> 

<Button 
    android:id="@+id/b1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Submit" 
    android:layout_below="@+id/et2"/> 
</RelativeLayout> 

Fragme nt_1.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
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" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity$FragmentOne"> 

<Button 
    android:id="@+id/b1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Submit" 
    android:layout_below="@+id/et2"/> 
</RelativeLayout> 

我已經改變了工具:上下文fragment_1.xml對我的測試。我不知道它在做什麼 現在,當用戶打開App時,他應該看到Fragment_Main片段。當他滑動下一個標籤時,他應該看到fragment_1。

請幫助詳細,因爲我很新。

回答

0

那究竟究竟是行不通的?

我只能猜測,但你不要在任何地方使用Fragment_Main.xml。 您已經將視圖尋呼機的位置傳遞給您的片段,您應該檢索它並根據它膨脹佈局。像這樣的事情您的片段裏面:

private int mFragmentIndex = 0; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     mFragmentIndex = getArguments().getInt(ARG_SECTION_NUMBER); 
    } 
} 

和onCreateView你應該誇大基於片段指數不同的佈局:

View rootView; 
    if (mFragmentIndex == 1) { 
     rootView = inflater.inflate(R.layout.Fragment_Main, container, false); 
    } else { 
     rootView = inflater.inflate(R.layout.fragment_1, container, false); 
    } 

無論如何,我會建議使用不同的片段,而不是膨脹不同的佈局。像這樣的東西:

@Override 
public Fragment getItem(int position) { 
    if (position == 0) { 
     return MainFragment.newInstance(); 
    } else { 
     return SomeOtherFragment.newInstance(); 
    } 

} 
+0

你可以做出這些改變,並把我的代碼請?我沒有得到它的地方。 – 2015-03-02 07:03:56

+0

@AdhishThite他們進去SectionsPagerAdapter – Sver 2015-03-02 07:05:21

+0

非常感謝@Sver。只是一個更簡單的問題。我想爲每個標籤添加標題。我沒有找到辦法做到這一點。請幫忙。 – 2015-03-02 07:12:32