PagerAdapter、FragmentPagerAdapter的使用

前言: PagerAdapter 是在 android.v4包下的,FragmentPagerAdapter 是PagerAdapter的子类,PagerAdapter 是用来装载一些View。而FragmentPagerAdapter 在更多的时候是用来装载Fragment的!当我们使用ViewPager+Fragment 动态的添加Fragment的时候,在此我们可以借助FragmentPagerAdapter来实现!当然使用PagerAddapter 也可以实现!
在这里例举FragmentPagerAdapter 的使用,而不需要重复的创建Fragment实例!
PagerAdapter、FragmentPagerAdapter的使用_第1张图片
图 一

PagerAdapter、FragmentPagerAdapter的使用_第2张图片
图 二

由图一滑动切换到图二,在这里我们发现在界面上没有任何的变动而只是数据发生了变化。

/** * @since 2016/4/21 * @author wangly */
public class MainActivity extends FragmentActivity {
    List<Integer> datas = new ArrayList<Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.item);
        datas.add(1500);
        datas.add(1600);
        CustomViewPager viewPager = (CustomViewPager) findViewById(R.id.viewPager);
        TestFragmentAdapter adapter = new TestFragmentAdapter(getSupportFragmentManager());
        adapter.setViewCount(datas);
        adapter.setViewPagerScroll(viewPager);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
    }


}
/** * @since 2016/4/21 * @author wangly */
public class TestFragmentAdapter extends FragmentPagerAdapter{
    private List<Integer> values;
    private CustomViewPager viewPager;

    public void setViewCount(List<Integer> data){
        this.values = data;
    }

    public void setViewPagerScroll(CustomViewPager customViewPager){
        this.viewPager = customViewPager;
    }
    public TestFragmentAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        return TestFragment.newInstance(values.get(position),viewPager);
    }

    @Override
    public int getCount() {
        return values.size();
    }
}
/** * @since 2016/4/21 * @author wangly */
public final class TestFragment extends Fragment {
    private static final String TAG = TestFragment.class.getSimpleName();
    TestFragment fragment;
    private CustomViewPager viewPager;
    private View view;
    private  int values;
    private RelativeLayout rl_circleView;
    TreeView treeView;

    public static TestFragment newInstance(int value,CustomViewPager customViewPager) {
        TestFragment fragment = new TestFragment();

        fragment.values = value;
        fragment.viewPager = customViewPager;
        Log.d("wangly", "创建了·····");
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if ((savedInstanceState != null) && savedInstanceState.containsKey(TAG)) {
        }
        view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_main, null);
        initView();
        return view;
    }

    private void initView() {

        rl_circleView = (RelativeLayout) view.findViewById(R.id.rl_cirView);

        CircleView circleView = (CircleView) view.findViewById(R.id.id_circleview);
        circleView.setFinishStateImage(1,0,0);
        circleView.setStepValue(values);
        circleView.setOnViewTounchListener(new OnViewTounchListener() {

            @Override
            public void tofling() {

            }

            @Override
            public void click() {
                historgram.setVisibility(View.VISIBLE);
                rl_circleView.setVisibility(View.GONE);
                viewPager.setScanScroll(false);
            }
        });
        treeView.setOnViewTounchListener(new OnViewTounchListener() {

            @Override
            public void tofling() {

            }

            @Override
            public void click() {
                rl_circleView.setVisibility(View.VISIBLE);
                historgram.setVisibility(View.GONE);
                viewPager.setScanScroll(true);

            }
        });
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

}

当然如果使用PagerAdapter 的话需要重写当中的几个方法

/** * @since 2016/4/21 * @author wangly */
public class TestAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return false;
    }


    @Override
    public void destroyItem(View container, int position, Object object) {
        // TODO Auto-generated method stub
        super.destroyItem(container, position, object);
    }

    @Override
    public Object instantiateItem(View container, int position) {
        // TODO Auto-generated method stub
        return super.instantiateItem(container, position);
    }
}

你可能感兴趣的:(viewpager,Fragment)