2017-08-13 96 views
0

我試圖修改此應用程序的代碼:https://github.com/udacity/ud839_ViewPager_Example/tree/quiz 使MainActivity在啓動時顯示TuesdayFragment而不是MondayFragment。選擇特定片段以在活動中顯示?

我想了解如何選擇Activity在啓動時顯示的特定fragment.java文件。

你們中有些人可能沒有時間去檢查GitHub上的代碼,所以這裏是:

MainActivity.java

package com.example.android.viewpager; 

import android.os.Bundle; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 

/** 
* Displays a {@link ViewPager} where each page shows a different day of the week. 
*/ 
public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 

     // Find the view pager that will allow the user to swipe between fragments 
     ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 

     // Create an adapter that knows which fragment should be shown on each page 
     SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager()); 

     // Set the adapter onto the view pager 
     viewPager.setAdapter(adapter); 
    } 
} 

,它的佈局activity_main.xml中文件:

<?xml version="1.0" encoding="utf-8"?> 

<!-- Layout for the main screen --> 
<LinearLayout 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:orientation="vertical" 
    tools:context="com.example.android.viewpager.MainActivity"> 

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

</LinearLayout> 

TuesdayFragment.java,WednesdayFragment.java和MondayFragment.java非常相似,只是用不同的layouts.xml,這顯示星期的文字不同的日子:

package com.example.android.viewpager; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* Fragment that displays "Monday". 
*/ 
public class MondayFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_monday, container, false); 
    } 
} 

且有SimpleFragmentPagerAdapter.java:

package com.example.android.viewpager; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 

/** 
* Provides the appropriate {@link Fragment} for a view pager. 
*/ 
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     if (position == 0) { 
      return new MondayFragment(); 
     } else if (position == 1){ 
      return new TuesdayFragment(); 
     } else { 
      return new WednesdayFragment(); 
     } 
    } 

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

我正在閱讀關於碎片和交易以及玩弄交易和替換,試圖強制在主動活動開始時顯示星期二碎片,並帶有如下想法:

getSupportFragmentManager().beginTransaction().replace(R.id.viewpager, TuesdayFragment).commit(); 

但沒有任何工作。

問題:有人可以這麼善良,告訴我需要修改/添加哪些代碼,以便MainActivity能夠以我想要的任何碎片開始?現在它總是從MondayFragment開始。

我被卡住了,所以即使將我推向正確的方向,在這一點上也會非常感激。

回答

2

一旦您設置適配器,可以調用您的onCreate方法這個方法:

viewPager.setCurrentItem(indexOfItemYouWantToDisplay) 

由於默認爲0,則顯示MondayFragment。將其設置爲1,並顯示TuesdayFragment。

+0

正是我在找的東西。謝謝KMP! – LearningToProgramStuff