为什么在调用第一个片段时会自动调用第二个片段?
Why on calling first fragment second fragment is automatically getting called?
我正在使用 ViewPager 显示 4 个片段,但问题是在加载第一个片段时,第二个片段也被调用,即两个片段的 onActivityCreate()
方法被一起调用并滑动到第二个片段第三个片段的 onActivityCreate()
等等其他片段和最后一个片段没有 onActivityCreate()
被调用。如何解决。
这是我的文件:
购买详情
public class PurchaseDetails extends AppCompatActivity implements ActionBar.TabListener {
ViewPager viewPager;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchase_details);
viewPager=findViewById(R.id.master_purchase_pager);
FragmentManager fm=getSupportFragmentManager();
actionBar = getSupportActionBar();
actionBar.setTitle("Purchase Details");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager.setAdapter(new PurchaseFragmentAdapter(fm));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Date Selector");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Week");
tab2.setTabListener(this);
ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Month");
tab3.setTabListener(this);
ActionBar.Tab tab4 = actionBar.newTab();
tab4.setText("Year");
tab4.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
actionBar.addTab(tab4);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
PurchaseFragmentAdapter
public class PurchaseFragmentAdapter extends FragmentPagerAdapter {
public PurchaseFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if(position==0)
{
fragment= new PurchaseDateSelector();
}
else if(position==1)
{
fragment= new PurchaseWeek();
}
else if(position==2)
{
fragment= new PurchaseMonth();
}
else if(position==3)
{
fragment= new PurchaseYear();
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
}
购买日期选择器
public class PurchaseDateSelector extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_date_selector_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Date Selector", Toast.LENGTH_SHORT).show();
}
}
采购周
public class PurchaseWeek extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_week_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Week", Toast.LENGTH_SHORT).show();
}
}
购买月份
public class PurchaseMonth extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_month_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Month", Toast.LENGTH_SHORT).show();
}
}
采购年份
public class PurchaseYear extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_year_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Year", Toast.LENGTH_SHORT).show();
}
}
这就是 ViewPager
设计的工作方式。默认情况下,ViewPager 仅创建当前页面以及当前页面左侧和右侧的离屏页面。这是为了让页面之间的平滑过渡。
您还可以设置在空闲状态下应保留在视图层次结构中当前页面任一侧的页面数。
这是视图寻呼机的默认和预期行为,它在其左右两侧创建每个片段以及可见片段以实现流畅的动画,您不能限制片段的创建,也许您可以做的是限制使用 setuserVisibleHint()
方法
在不可见片段上加载内容
@Override
public void setUserVisibleHint(boolean visible){
if(visible){
//load content
}
}
ViewPager 不会一次创建所有页面。当使用大量页面时,如果用户永远不会滑动浏览所有这些页面,这将非常慢,甚至是不必要的。默认情况下,ViewPager 仅创建当前页面以及当前页面左侧和右侧的离屏页面。
那么这个问题的解决方案是什么,他们有吗?
是的,
mViewPager.setOffscreenPageLimit(int limit);
Parameters : limit – How many pages will be kept offscreen in an idle
state.
ViewPager 至少需要 1 个离屏页面
如果您将值设置为 0,那么您应该会在 LogCat 中收到关于此的警告,例如:
Requested offscreen page limit 0 too small;
默认为1
这是因为 ViewPager 基于准备好要加载的下一页的概念。 ViewPager 无法查看不存在的页面。
我正在使用 ViewPager 显示 4 个片段,但问题是在加载第一个片段时,第二个片段也被调用,即两个片段的 onActivityCreate()
方法被一起调用并滑动到第二个片段第三个片段的 onActivityCreate()
等等其他片段和最后一个片段没有 onActivityCreate()
被调用。如何解决。
这是我的文件:
购买详情
public class PurchaseDetails extends AppCompatActivity implements ActionBar.TabListener {
ViewPager viewPager;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchase_details);
viewPager=findViewById(R.id.master_purchase_pager);
FragmentManager fm=getSupportFragmentManager();
actionBar = getSupportActionBar();
actionBar.setTitle("Purchase Details");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager.setAdapter(new PurchaseFragmentAdapter(fm));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Date Selector");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Week");
tab2.setTabListener(this);
ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Month");
tab3.setTabListener(this);
ActionBar.Tab tab4 = actionBar.newTab();
tab4.setText("Year");
tab4.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
actionBar.addTab(tab4);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
PurchaseFragmentAdapter
public class PurchaseFragmentAdapter extends FragmentPagerAdapter {
public PurchaseFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if(position==0)
{
fragment= new PurchaseDateSelector();
}
else if(position==1)
{
fragment= new PurchaseWeek();
}
else if(position==2)
{
fragment= new PurchaseMonth();
}
else if(position==3)
{
fragment= new PurchaseYear();
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
}
购买日期选择器
public class PurchaseDateSelector extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_date_selector_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Date Selector", Toast.LENGTH_SHORT).show();
}
}
采购周
public class PurchaseWeek extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_week_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Week", Toast.LENGTH_SHORT).show();
}
}
购买月份
public class PurchaseMonth extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_month_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Month", Toast.LENGTH_SHORT).show();
}
}
采购年份
public class PurchaseYear extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.purchase_year_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Purchase Year", Toast.LENGTH_SHORT).show();
}
}
这就是 ViewPager
设计的工作方式。默认情况下,ViewPager 仅创建当前页面以及当前页面左侧和右侧的离屏页面。这是为了让页面之间的平滑过渡。
您还可以设置在空闲状态下应保留在视图层次结构中当前页面任一侧的页面数。
这是视图寻呼机的默认和预期行为,它在其左右两侧创建每个片段以及可见片段以实现流畅的动画,您不能限制片段的创建,也许您可以做的是限制使用 setuserVisibleHint()
方法
@Override
public void setUserVisibleHint(boolean visible){
if(visible){
//load content
}
}
ViewPager 不会一次创建所有页面。当使用大量页面时,如果用户永远不会滑动浏览所有这些页面,这将非常慢,甚至是不必要的。默认情况下,ViewPager 仅创建当前页面以及当前页面左侧和右侧的离屏页面。
那么这个问题的解决方案是什么,他们有吗?
是的,
mViewPager.setOffscreenPageLimit(int limit);
Parameters : limit – How many pages will be kept offscreen in an idle state.
ViewPager 至少需要 1 个离屏页面
如果您将值设置为 0,那么您应该会在 LogCat 中收到关于此的警告,例如:
Requested offscreen page limit 0 too small;
默认为1 这是因为 ViewPager 基于准备好要加载的下一页的概念。 ViewPager 无法查看不存在的页面。