2013年(92)
分类: 信息化
2013-04-25 05:35:45
首要来看一下效果 源文件下载(资源服务器上载为照应,稍后上载) 资源layout 1,activity_main.xml 2,fragment.xml 3,fragment03.xml 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 mList = new ArrayList(); 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; } } }