大家好,我是android studio的新手,我做了一个活动,通过viewpage2和适配器让我浏览3个片段
public class MainActivity extends AppCompatActivity {
private Button buttonContinue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
ViewPager2 viewPager2; = findViewById(R.id.mainactivity_viewpager);
MyAdapter adapter; = new MyAdapter(this);
viewPager2.setAdapter(adapter);
Button button = findViewById(R.id.mainactivity_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(viewPager2.getCurrentItem() +1 < adapter.getItemCount()){
viewPager2.setCurrentItem(viewPager2.getCurrentItem()+1);
}
else
FirstAccessActivity.this.finish();
}
}
}
}
现在我希望这个适配器不总是返回这3个片段,而是基于以某种方式传递的条件,它只返回第一个和第三个片段,或者只返回第二个片段,我该怎么办?
public class Adpter extends FragmentStateAdapter {
public Adpter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
default:
return null;
}
}
@Override
public int getItemCount() {
return 3;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!