gpt4 book ai didi

android - 如何使用 fragment 中的按钮更改 View Pager 中的 fragment ?

转载 作者:行者123 更新时间:2023-11-29 23:27:01 26 4
gpt4 key购买 nike

<分区>

我在其中有一个名为 RegisterActivity 的 Activity ,我正在实现包含两个 fragment 的 ViewPager。这是 Activity 代码

public class RegisterActivity extends AppCompatActivity{

private Context context;
private Toolbar toolbar;
private TabLayout tabLayout;
public static ViewPager viewPager;
private ViewPagerAdapter adapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
context = RegisterActivity.this;

viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

}

private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "Step 1");
adapter.addFragment(new FragmentTwo(), "Step 2");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}

@Override
public int getCount() {
return fragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}

第一个 fragment 有一个 NEXT 按钮。按下此按钮时,我希望 View 寻呼机滚动到 FragmentTWo。同样,FragmentTwo 有一个 BACK 按钮,我希望 View 寻呼机在按下 BACK 按钮(存在于 FragmentTwo 布局中)时滚动到 fragmentOne。

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