gpt4 book ai didi

java - 在 ViewPager 中选择要开始的 fragment - Android

转载 作者:行者123 更新时间:2023-11-29 07:32:44 25 4
gpt4 key购买 nike

我正在尝试制作一个登录屏幕,其中注册、登录和忘记密码屏幕都是 fragment ,放置如下:

忘记密码 |登入 |注册

我在 YouTube 上找到了一个向我展示如何执行此操作的简单视频。我遇到的唯一问题是,当我启动 Activity 时,它会在忘记密码 fragment 上打开,因为它是第一个 fragment ,但我希望它从中间开始,即登录 fragment 。这是主要 Activity 的代码。我应该更改或添加到此代码中,还是我采用了错误的方法来实现我想要实现的目标

访问.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);
}

}

访问.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>

访问忘记密码.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>

访问登录.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>

访问注册.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>

最佳答案

设置

accessViewPager.setCurrentItem(1);

关于java - 在 ViewPager 中选择要开始的 fragment - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39825455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com