转载请注明出处:http://blog.youkuaiyun.com/droyon/article/details/8847489
首先来看一下效果
资源layout
1,activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v4.view.PagerTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center">
<RadioGroup android:id="@+id/ra_group" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioButton android:id="@+id/ra01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true"/>
<RadioButton android:id="@+id/ra02" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/ra03" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
2,fragment.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent">
<ImageView android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:gravity="center"
>
<TextView android:id="@+id/tv"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
/>
</LinearLayout>
</FrameLayout>
3,fragment03.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent">
<ImageView android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent">
<RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button android:id="@+id/btn_start"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
/>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
java代码:
1,MainActivity.java
package com.android.zhuoyang.pagefling;
import java.util.ArrayList;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.ViewFlipper;
@SuppressLint("ValidFragment")
public class MainActivity extends FragmentActivity {
public static ArrayList<Drawable> mList = new ArrayList<Drawable>();
private ViewPager mViewPager;
private PagerTabStrip mViewFlipper;
private FragmentPagerAdapter mAdapter;
private RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList.add(getResources().getDrawable(R.drawable.png01));
mList.add(getResources().getDrawable(R.drawable.png02));
mList.add(getResources().getDrawable(R.drawable.png03));
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewFlipper = (PagerTabStrip) findViewById(R.id.tabs);
mAdapter = new MyViewPage(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(viewPagerOnChanged);
mRadioGroup = (RadioGroup) findViewById(R.id.ra_group);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class MyViewPage extends FragmentPagerAdapter{
public MyViewPage(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
System.out.println("position is:"+position);
return MyFragment.newInstance(position);
}
@Override
public int getCount() {
return mList.size();
}
}
ViewPager.OnPageChangeListener viewPagerOnChanged = new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
RadioButton button = (RadioButton) mRadioGroup.getChildAt(position);
button.setChecked(true);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
};
static class MyFragment extends Fragment{
int mPosition;
public static Fragment newInstance(int position){
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt("position", position);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPosition = (getArguments()!=null)?getArguments().getInt("position"):0;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
if(mPosition == mList.size()-1){
view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment03, container,false);
Button btn = (Button) view.findViewById(R.id.btn_start);
btn.setText(R.string.third_tv);
}else if(mPosition == mList.size()-3){
view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment, container,false);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText(R.string.first_tv);
}else if(mPosition == mList.size()-2){
view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment, container,false);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText(R.string.second_tv);
}
ImageView imgView = (ImageView) view.findViewById(R.id.img);
imgView.setImageDrawable(mList.get(mPosition));
return view;
}
}
}