Go to my github

android 左右翻页

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_app_bgk" >

    <RelativeLayout
        android:id="@+id/header_homeActivity"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/common_titlebar" >

        <ImageView
            android:id="@+id/icon_app"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/ic_launcher"
            android:contentDescription="@string/app_name" />

        <ImageView
            android:id="@+id/icon_phone"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:contentDescription="@string/app_name"
            android:paddingBottom="16dp"
            android:paddingLeft="24dp"
            android:paddingRight="8dp"
            android:paddingTop="16dp"
            android:src="@drawable/icon_phone" />

        <TextView
            android:id="@+id/text_app"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_margin="2dp"
            android:layout_toRightOf="@+id/icon_app"
            android:text="@string/app_name"
            android:textColor="@color/color_white"
            android:textSize="@dimen/font_large" />
    </RelativeLayout>

    <RadioGroup
        android:id="@+id/navi_switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/header_homeActivity"
        android:checkedButton="@+id/navi_switcher_item_blogPage"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/navi_switcher_item_blogPage"
            style="@style/radio_btn_style"
            android:text="@string/main_blogPage" />

        <RadioButton
            android:id="@+id/navi_switcher_item_framePage"
            style="@style/radio_btn_style"
            android:text="@string/main_framePage" />

        <RadioButton
            android:id="@+id/navi_switcher_item_worksPage"
            style="@style/radio_btn_style"
            android:text="@string/main_worksPage" />

        <RadioButton
            android:id="@+id/navi_switcher_item_servePage"
            style="@style/radio_btn_style"
            android:text="@string/main_servePage" />

        <RadioButton
            android:id="@+id/navi_switcher_item_aboutPage"
            style="@style/radio_btn_style"
            android:text="@string/main_aboutPage" />
    </RadioGroup>

    <android.support.v4.view.ViewPager
        android:id="@+id/navi_view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/navi_switcher"
        android:padding="0dip" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

Activity:

package lcl.android.ui;

import lcl.android.R;
import lcl.android.core.AppContext;
import lcl.android.core.UpdateManager;
import lcl.android.mail.MailHelper;
import lcl.android.sms.SmsHelper;
import lcl.android.utility.LogUtils;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

/**
* 应用程序首页
*/
public class Main extends FragmentActivity{
    private AppContext appContext;// 全局Context

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentViews();

        getViews();
    }

    private void setContentViews() {
        setContentView(R.layout.main);

        appContext = (AppContext) getApplication();
    }

    private RadioGroup mSwitcher;
    private ViewPager mSearchVp;

    private void getViews() {
        mSwitcher = (RadioGroup) findViewById(R.id.navi_switcher);
        mSwitcher.setOnCheckedChangeListener(mCheckedChgLitener);
        mSearchVp = (ViewPager) findViewById(R.id.navi_view_pager);
        mSearchVp.setAdapter(new FrameworkList(getSupportFragmentManager()));
        mSearchVp.setOnPageChangeListener(mPageChgListener);
        View view = this.findViewById(R.id.icon_phone);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 打电话
                Intent intent = new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:13026209315"));
                startActivity(intent);
            }
        });
    }

    private final int CB_INDEX_blog = 0;
    private final int CB_INDEX_frame = 1;
    private final int CB_INDEX_work = 2;
    private final int CB_INDEX_serve = 3;
    private final int CB_INDEX_about = 4;

    private OnCheckedChangeListener mCheckedChgLitener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup arg0, int checkedId) {

            int cur = CB_INDEX_blog;
            switch (checkedId) {
            case R.id.navi_switcher_item_blogPage:
                cur = CB_INDEX_blog;
                break;
            case R.id.navi_switcher_item_framePage:
                cur = CB_INDEX_frame;
                break;
            case R.id.navi_switcher_item_worksPage:
                cur = CB_INDEX_work;
                break;
            case R.id.navi_switcher_item_servePage:
                cur = CB_INDEX_serve;
                break;
            case R.id.navi_switcher_item_aboutPage:
                cur = CB_INDEX_about;
                break;
            }
            if (mSearchVp.getCurrentItem() != cur) {
                mSearchVp.setCurrentItem(cur);
            }
        }
    };
    private OnPageChangeListener mPageChgListener = new OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            int vpItem = mSearchVp.getCurrentItem();
            switch (vpItem) {
            case CB_INDEX_blog:
                mSwitcher.check(R.id.navi_switcher_item_blogPage);
                break;
            case CB_INDEX_frame:
                mSwitcher.check(R.id.navi_switcher_item_framePage);
                break;
            case CB_INDEX_work:
                mSwitcher.check(R.id.navi_switcher_item_worksPage);
                break;
            case CB_INDEX_serve:
                mSwitcher.check(R.id.navi_switcher_item_servePage);
                break;
            case CB_INDEX_about:
                mSwitcher.check(R.id.navi_switcher_item_aboutPage);
                break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

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

        }
    };

    private void checkAppUpdate() {
        // 网络连接判断
        if (appContext.isNetworkConnected()) {
            // 检查新版本
            if (appContext.isCheckUp()) {
                UpdateManager.getUpdateManager().checkAppUpdate(this, false);
                LogUtils.Info("Main checkAppUpdate 检查新版本....");
            }
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        // TODO 自动生成的方法存根
        super.onResume();
    }
}

FrameworkList:

package lcl.android.ui;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class FrameworkList extends FragmentPagerAdapter {

    private ArrayList<Fragment> mFragments;

    public FrameworkList(FragmentManager fm) {
        super(fm);
        mFragments = new ArrayList<Fragment>();
        mFragments.add(new Blog());
        mFragments.add(new Framework());
        mFragments.add(new Works());
        mFragments.add(new Serve());
        mFragments.add(new About());
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }
}

posted @ 2014-09-09 13:48  峡谷少爷  阅读(1542)  评论(1编辑  收藏  举报