Fragment+ViewPager

MainActivity

package com.example.whq_day04_viewpager;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

private ViewPager pager;
private RadioGroup radio;
private TextView text1;
private TextView text2;
private TextView text3;
private ArrayList fragments;

@SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    //获取资源id
    pager = (ViewPager) findViewById(R.id.pager);
    radio = (RadioGroup) findViewById(R.id.radio);
    
    text1 = (TextView) findViewById(R.id.text1);
    text2 = (TextView) findViewById(R.id.text2);
    text3 = (TextView) findViewById(R.id.text3);
    
    fragments = new ArrayList();
    fragments.add(new AFragment());
    fragments.add(new BFragment());
    fragments.add(new CFragment());
    
   // viewpager 和 fragment结合,需要使用FragmentPagerAdapter
    // 需要传递Manager
    
    pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
		
		@Override
		public int getCount() {
			return fragments.size();
		}
		
		@Override
		public Fragment getItem(int arg0) {
			return fragments.get(arg0);
		}
	});
    
    radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.rd1:
				pager.setCurrentItem(0);
				break;

			case R.id.rd2:
				pager.setCurrentItem(1);
				break;
			case R.id.rd3:
				pager.setCurrentItem(2);
				break;
			}
		}
	});
    
    pager.setOnPageChangeListener(new OnPageChangeListener() {
    	// 滑动后,底部radiobutton改变颜色;
		@Override
		public void onPageSelected(int arg0) {
			switch (arg0) {
			case 0:
				radio.check(R.id.rd1);
				text1.setBackground(getResources().getDrawable(R.color.black));
				text2.setBackground(getResources().getDrawable(R.color.white));
				text3.setBackground(getResources().getDrawable(R.color.white));
				break;

			case 1:
				radio.check(R.id.rd2);
				text1.setBackground(getResources().getDrawable(R.color.white));
				text2.setBackground(getResources().getDrawable(R.color.black));
				text3.setBackground(getResources().getDrawable(R.color.white));
				
				break;
			case 2:
				radio.check(R.id.rd3);
				text1.setBackground(getResources().getDrawable(R.color.white));
				text2.setBackground(getResources().getDrawable(R.color.white));
				text3.setBackground(getResources().getDrawable(R.color.black));
				break;
			}
		}
		
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			
		}
		
		@Override
		public void onPageScrollStateChanged(int arg0) {
			
		}
	});
}

}

AFragment

package com.example.whq_day04_viewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,
		@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
	View view=View.inflate(getActivity(), R.layout.afragment, null);
	return view;
}
}

BFragment

package com.example.whq_day04_viewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class BFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,
		@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
	View view=View.inflate(getActivity(), R.layout.bfragment, null);
	return view;
}
}

CFragment

package com.example.whq_day04_viewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CFragment extends Fragment{
@Override

public View onCreateView(LayoutInflater inflater,
		@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
	View view=View.inflate(getActivity(), R.layout.cfragment, null);
	return view;
}
}

activity_main.xml

package com.example.whq_day04_viewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,
		@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
	View view=View.inflate(getActivity(), R.layout.cfragment, null);
	return view;
}
}

你可能感兴趣的:(Fragment+ViewPager)