如何在自定义ViewPager中停止调用onCreateView方法

时间:2017-07-06 11:40:53

标签: android android-fragments

因为我有片段,所以我在ViewPager中显示另外三个片段,现在从所有三个片段调用onCreateView方法。 这是我的代码。

public class CustomViewPager extends ViewPager {
private boolean enabled;

public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.enabled = true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onTouchEvent(event);
    }
    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onInterceptTouchEvent(event);
    }
    return false;
}

public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
}
}

Xml

<com.customView.CustomViewPager
            android:id="@+id/newOrderContainerPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/layout_next_previous"
            android:layout_below="@+id/newTitle"
            android:layout_marginBottom="@dimen/ten_dp">
</com.customView.CustomViewPager>

这是适配器

public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

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

@Override
public Fragment getItem(int position) {`enter code here`

    return mFragmentList.get(position);
}

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

public void addFrag(Fragment fragment, String title) {
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);
}


@Override
public Object instantiateItem(ViewGroup container, int position) {
    return super.instantiateItem(container, position);
}
}

显示ViewPager

的主要片段
@Override
public View setContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_new_order, container, false);


    setupViewPager(customViewPager);
    customViewPager.setPagingEnabled(false);

    return view;
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
    adapter.addFrag(new NewOrderFragmentFirstPage(), NewOrderFragmentFirstPage.class.getName());
    adapter.addFrag(new NewOrderFragmentSecondPage(), NewOrderFragmentFirstPage.class.getName());
    adapter.addFrag(new NewOrderFragmentThirdPage(), NewOrderFragmentThirdPage.class.getName());
    viewPager.setAdapter(adapter);
}

 public void onClick(View view) {
    switch (view.getId()) {
        case R.id.txt_Next:
            txt_Previous.setVisibility(View.VISIBLE);
            if (count < 3) {
                count++;
            }
            customViewPager.setCurrentItem(customViewPager.getCurrentItem() + 1);
            textView.setText(count + "/3");
            break;
        case R.id.txt_previous:
            if (count > 1) {
                count--;
            }
            if (count == 1) {
                txt_Previous.setVisibility(View.INVISIBLE);
            }
            customViewPager.setCurrentItem(customViewPager.getCurrentItem() - 1);
            textView.setText(count + "/3");
            break;
    }
}

请帮忙。 感谢

1 个答案:

答案 0 :(得分:0)

你可以在onActivityCreated()中添加你想要只执行一次的代码,这将阻止它从所有3个片段调用