天天看點

PagerAdapter、FragmentPagerAdapter的使用

前言: PagerAdapter 是在 android.v4包下的,FragmentPagerAdapter 是PagerAdapter的子類,PagerAdapter 是用來裝載一些View。而FragmentPagerAdapter 在更多的時候是用來裝載Fragment的!當我們使用ViewPager+Fragment 動态的添加Fragment的時候,在此我們可以借助FragmentPagerAdapter來實作!當然使用PagerAddapter 也可以實作!

在這裡例舉FragmentPagerAdapter 的使用,而不需要重複的建立Fragment執行個體!

PagerAdapter、FragmentPagerAdapter的使用

圖 一

PagerAdapter、FragmentPagerAdapter的使用

圖 二

由圖一滑動切換到圖二,在這裡我們發現在界面上沒有任何的變動而隻是資料發生了變化。

/**
 * @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();
        datas.add();
        CustomViewPager viewPager = (CustomViewPager) findViewById(R.id.viewPager);
        TestFragmentAdapter adapter = new TestFragmentAdapter(getSupportFragmentManager());
        adapter.setViewCount(datas);
        adapter.setViewPagerScroll(viewPager);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem();
    }


}
           
/**
 * @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(,,);
        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 ;
    }

    @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);
    }
}