1

我試圖做一個登錄界面,其中註冊,登錄和忘記密碼的屏幕都是碎片,放在這樣的:選擇一個片段開始在ViewPager - Android電子

忘記密碼|登錄|註冊

我在YouTube上發現了一個簡單的視頻,向我展示瞭如何做到這一點。我唯一的問題是,當我開始活動時,它會在忘記密碼片段上打開,因爲它是第一個,但我希望它在中間開始,即登錄片段。這是主要活動的代碼。我應該怎麼更改或添加該代碼或我服用了什麼,我想實現

Access.java

public class Access extends FragmentActivity { 

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

     initializePaging(); 
    } 

    private void initializePaging() { 
     List<Fragment> fragments = new Vector<>(); 
     fragments.add(Fragment.instantiate(this, AccessForgotPassword.class.getName())); 
     fragments.add(Fragment.instantiate(this, AccessLogin.class.getName())); 
     fragments.add(Fragment.instantiate(this, AccessRegister.class.getName())); 

     PagerAdapter mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments); 

     ViewPager accessViewPager = (ViewPager) findViewById(R.id.accessViewPager); 
     accessViewPager.setAdapter(mPagerAdapter); 
    } 

} 

access.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/accessMainLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".access.Access"> 

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

</LinearLayout> 

AccessForgotPassword.java錯誤的做法

public class AccessForgotPassword extends Fragment { 

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

} 

access_forgot_password.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/accessForgotPasswordMainLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#FF0000"> 
</LinearLayout> 

AccessLogin.java

public class AccessLogin extends Fragment { 

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

} 

access_login.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/accessLoginMainLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#00FF00"> 
</LinearLayout> 

AccessRegister.java

public class AccessRegister extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 
     return inflater.infalte(R.layout.access_register, container, false); 
    } 

} 

access_register.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/accessRegisterMainLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#0000FF"> 
</LinearLayout> 

回答

2

accessViewPager.setCurrentItem(1); 
+0

謝謝了!這很好! :) –

1

嘗試onCreate()

1

嘗試的末尾添加accessViewPager.setCurrentItem(1)設置accessViewPager.setCurrentItem(1);在活動的onCreate()。

1

只設置適配器,你可以手動設置設置第二個片段,查看傳呼機

accessViewPager.setCurrentItem(1); // 0= ForgotPassword, 1=LoginScreen,...so on