MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener, ViewPager.OnPageChangeListener {    private RadioButton rb_my, rb_withdrawals;    private MyFragment mMyFragment;    private WithdrawalsFragment mWithdrawalsFragment;    private ViewPager vp_main;    private ArrayList mFragmentList;    private MyViewPagerAdapter mViewPagerAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);        findViews();        setListensers();        InitView();    }    protected void findViews() {        rb_withdrawals = (RadioButton) findViewById(R.id.rb_withdrawals);        rb_my = (RadioButton) findViewById(R.id.rb_my);        vp_main = (ViewPager) findViewById(R.id.vp_main);    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.rb_withdrawals:                vp_main.setCurrentItem(0);                break;            case R.id.rb_my:                vp_main.setCurrentItem(1);                break;            default:                break;        }    }    protected void setListensers() {        rb_withdrawals.setOnClickListener(this);        rb_my.setOnClickListener(this);        vp_main.setOnPageChangeListener(this);    }    @Override    public void onPageScrollStateChanged(int arg0) {    }    @Override    public void onPageScrolled(int arg0, float arg1, int arg2) {    }    @Override    public void onPageSelected(int id) {        switch (id) {            case 0:                rb_withdrawals.setChecked(true);                break;            case 1:                rb_my.setChecked(true);                break;            default:                break;        }    }    private void addFragment() {        if (mWithdrawalsFragment == null) {            mWithdrawalsFragment = new WithdrawalsFragment();        }        if (mMyFragment == null) {            mMyFragment = new MyFragment();        }        if (mFragmentList == null) {            mFragmentList = new ArrayList();// 初始化数据            mFragmentList.add(mWithdrawalsFragment);            mFragmentList.add(mMyFragment);        }    }    private void setViewPager() {        if (mViewPagerAdapter == null) {            mViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), mFragmentList);            vp_main.setAdapter(mViewPagerAdapter);            vp_main.setOffscreenPageLimit(3);        }    }    private void InitView() {        addFragment();        setViewPager();    }}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/screen_bg"    tools:context=".activity.MainActivity">    <RadioGroup        android:id="@+id/rg_control_button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:background="@color/white"        android:orientation="horizontal"        android:paddingBottom="10dp"        android:paddingTop="10dp">        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@drawable/radio_tab_home"            android:button="@null"            android:textColor="@color/light_gray"            android:textSize="12dp"            android:visibility="gone"/>        <RadioButton            android:id="@+id/rb_withdrawals"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@drawable/get_red_package_select"            android:button="@null"            android:textColor="@color/light_gray"            android:textSize="12dp" />        <RadioButton            android:id="@+id/rb_my"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@drawable/radio_tab_my"            android:button="@null"            android:textColor="@color/light_gray"            android:textSize="12dp" />    RadioGroup>    <android.support.v4.view.ViewPager        android:id="@+id/vp_main"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@id/rg_control_button"        android:flipInterval="30"        android:persistentDrawingCache="animation" />    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:layout_below="@id/vp_main"        android:background="#60777777" />RelativeLayout>

更多相关文章

  1. android > SMS 短信数据库访问
  2. JNI基本数据类型转换
  3. Android摄像头视频帧数据测试代码
  4. android 如何在对话框中获取edittext中的数据
  5. android sqilte3数据库大小的测试
  6. [导入]Android平台上四种保存数据的方法
  7. android HttpURLConnection 连接网络 读取返回数据
  8. Android 取出 图片以字节形式存放到数据库的数据,并将图片显示
  9. Android的关键的持久数据应该在onPause()方法中保存

随机推荐

  1. 实例16--心理测试
  2. 谈Android模拟点击的价值和实现
  3. Android——基于Android Studio开发备忘
  4. Android Basic-xml的数据读取及保存
  5. Android之Adapter(适配器)
  6. 下一代Android或官方支持“App2sd”
  7. android文件系统及其制作
  8. android socket简单编程(java在PC本地创建
  9. Android(安卓)binder from Top to Bottom
  10. Mac环境下Android APK包反编译详解