ViewPager实现Fragment切换

activity_main.xml: 



    

        

        

        
    



MainActivity:

public class MainActivity extends AppCompatActivity {


    private ViewPager mViewPager;
    private ArrayList fragments;
    private TextView view1, view2, view3;
    private int currIndex;
    private static int bmpW;//横线图片宽度
    private static int offset;//图片移动的偏移量


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initViewPager();
        InitTextView();
    }

    private void InitTextView() {
        view1 = (TextView) findViewById(R.id.tv_one);
        view2 = (TextView) findViewById(R.id.tv_two);
        view3 = (TextView) findViewById(R.id.tv_thr);
        view1.setOnClickListener(new txtListener(0));
        view2.setOnClickListener(new txtListener(1));
        view3.setOnClickListener(new txtListener(2));
    }

    //内部类 重写TextView点击事件
    public class txtListener implements View.OnClickListener {
        private int index = 0;

        public txtListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(index);
        }
    }

    private void initViewPager() {
        mViewPager = (ViewPager) findViewById(R.id.myViewPager);
        fragments = new ArrayList();
        Fragment fragmentHot = new FragmentOne();
        Fragment fragmentNews = new FragmentTwo();
        Fragment fragmentFav = new FragmentThree();
        fragments.add(fragmentHot);
        fragments.add(fragmentNews);
        fragments.add(fragmentFav);
        mViewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), fragments));
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new myOnPageChangeListener());
    }

    public class myOnPageChangeListener implements ViewPager.OnPageChangeListener {
        private int one = offset * 2 + bmpW;//两个相邻页面的偏移量

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            Log.i("aaaaaaaaaaaaa", String.valueOf(arg0));
            Log.i("one", String.valueOf(one));
            Animation animation = new TranslateAnimation(currIndex * one, arg0 * one, 0, 0);//平移动画
            currIndex = arg0;
            animation.setFillAfter(true);//动画终止时停留在最后一帧,不然会回到没有执行前的状态
            animation.setDuration(200);//动画持续时间0.2秒
            // image.startAnimation(animation);//是用ImageView来显示动画的
            //int i = currIndex + 1;
            // Toast.makeText(MainActivity.this, "您选择了第"+i+"个页卡", Toast.LENGTH_SHORT).show();
        }
    }
}

 
   
FragmentOne:
 public class FragmentOne extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one, container, false); return view; }} 
   
 
   
 
   

 
   
 
   
MyFragmentAdapter:
 
   
public class MyFragmentAdapter extends FragmentPagerAdapter { ArrayList list; public MyFragmentAdapter(FragmentManager fm, ArrayList list) { super(fm); this.list = list; } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); }}
 
   
 
   
 
   
 
   



你可能感兴趣的:(ViewPager实现Fragment切换)