引导页,代码很简单,用个viewpager就搞定了,这里给出一个延时跳转的代码。我是通过pos判断当前是第几张图片,然后进行延时跳转。
MainTabActivity(主界面):
package com.apk.bilibili.activity
;
import android.os.Bundle ;
import android.support.v4.app.Fragment ;
import android.support.v4.app.FragmentActivity ;
import android.support.v4.app.FragmentManager ;
import android.support.v4.app.FragmentTransaction ;
import android.view.View ;
import android.view.Window ;
import android.widget.ImageButton ;
import android.widget.LinearLayout ;
import com.apk.bilibili.R ;
import com.apk.bilibili.fragment.HomeFragment ;
import com.apk.bilibili.fragment.MineFragment ;
import com.apk.bilibili.fragment.NoticeFragment ;
import butterknife.ButterKnife ;
import butterknife.InjectView ;
public class MainTabActivity extends FragmentActivity implements View.OnClickListener {
@InjectView(R.id. tab_home) LinearLayout mTabHome ;
@InjectView(R.id. tab_mine) LinearLayout mTabMine ;
@InjectView(R.id. tab_notice) LinearLayout mTabNotice ;
@InjectView(R.id. tab_home_img) ImageButton mImgHome ;
@InjectView(R.id. tab_notice_img) ImageButton mImgNotice ;
@InjectView(R.id. tab_mine_img) ImageButton mImgMine ;
private Fragment mHome ;
private Fragment mNotice ;
private Fragment mMine ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. activity_main_tab) ;
ButterKnife. inject( this) ;
setSelect( 0) ;
initEvent() ;
}
private void initEvent() {
mTabHome.setOnClickListener( this) ;
mTabNotice.setOnClickListener( this) ;
mTabMine.setOnClickListener( this) ;
}
private void setSelect( int i)
{
FragmentManager fm = getSupportFragmentManager() ;
FragmentTransaction transaction = fm.beginTransaction() ;
hideFragment(transaction) ;
switch (i)
{
case 0:
if ( mHome == null)
{
mHome = new HomeFragment() ;
transaction.add(R.id. id_content , mHome) ;
} else
{
transaction.show( mHome) ;
}
mImgHome.setImageResource(R.drawable. home_pressed) ;
break;
case 1:
if ( mNotice == null)
{
mNotice = new NoticeFragment() ;
transaction.add(R.id. id_content , mNotice) ;
} else
{
transaction.show( mNotice) ;
}
mImgNotice.setImageResource(R.drawable. notice_pressed) ;
break;
case 2:
if ( mMine == null)
{
mMine = new MineFragment() ;
transaction.add(R.id. id_content , mMine) ;
} else
{
transaction.show( mMine) ;
}
mImgMine.setImageResource(R.drawable. mine_pressed) ;
break;
default:
break;
}
transaction.commit() ;
}
private void hideFragment(FragmentTransaction transaction)
{
if ( mHome != null)
{
transaction.hide( mHome) ;
}
if ( mNotice != null)
{
transaction.hide( mNotice) ;
}
if ( mMine != null)
{
transaction.hide( mMine) ;
}
}
@Override
public void onClick(View v)
{
resetImgs() ;
switch (v.getId())
{
case R.id. tab_home:
setSelect( 0) ;
break;
case R.id. tab_notice:
setSelect( 1) ;
break;
case R.id. tab_mine:
setSelect( 2) ;
break;
default:
break;
}
}
/**
* 切换图片至暗色
*/
private void resetImgs() {
mImgHome.setImageResource(R.drawable. home_normal) ;
mImgNotice.setImageResource(R.drawable. notice_normal) ;
mImgMine.setImageResource(R.drawable. mine_normal) ;
}
}
import android.os.Bundle ;
import android.support.v4.app.Fragment ;
import android.support.v4.app.FragmentActivity ;
import android.support.v4.app.FragmentManager ;
import android.support.v4.app.FragmentTransaction ;
import android.view.View ;
import android.view.Window ;
import android.widget.ImageButton ;
import android.widget.LinearLayout ;
import com.apk.bilibili.R ;
import com.apk.bilibili.fragment.HomeFragment ;
import com.apk.bilibili.fragment.MineFragment ;
import com.apk.bilibili.fragment.NoticeFragment ;
import butterknife.ButterKnife ;
import butterknife.InjectView ;
public class MainTabActivity extends FragmentActivity implements View.OnClickListener {
@InjectView(R.id. tab_home) LinearLayout mTabHome ;
@InjectView(R.id. tab_mine) LinearLayout mTabMine ;
@InjectView(R.id. tab_notice) LinearLayout mTabNotice ;
@InjectView(R.id. tab_home_img) ImageButton mImgHome ;
@InjectView(R.id. tab_notice_img) ImageButton mImgNotice ;
@InjectView(R.id. tab_mine_img) ImageButton mImgMine ;
private Fragment mHome ;
private Fragment mNotice ;
private Fragment mMine ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. activity_main_tab) ;
ButterKnife. inject( this) ;
setSelect( 0) ;
initEvent() ;
}
private void initEvent() {
mTabHome.setOnClickListener( this) ;
mTabNotice.setOnClickListener( this) ;
mTabMine.setOnClickListener( this) ;
}
private void setSelect( int i)
{
FragmentManager fm = getSupportFragmentManager() ;
FragmentTransaction transaction = fm.beginTransaction() ;
hideFragment(transaction) ;
switch (i)
{
case 0:
if ( mHome == null)
{
mHome = new HomeFragment() ;
transaction.add(R.id. id_content , mHome) ;
} else
{
transaction.show( mHome) ;
}
mImgHome.setImageResource(R.drawable. home_pressed) ;
break;
case 1:
if ( mNotice == null)
{
mNotice = new NoticeFragment() ;
transaction.add(R.id. id_content , mNotice) ;
} else
{
transaction.show( mNotice) ;
}
mImgNotice.setImageResource(R.drawable. notice_pressed) ;
break;
case 2:
if ( mMine == null)
{
mMine = new MineFragment() ;
transaction.add(R.id. id_content , mMine) ;
} else
{
transaction.show( mMine) ;
}
mImgMine.setImageResource(R.drawable. mine_pressed) ;
break;
default:
break;
}
transaction.commit() ;
}
private void hideFragment(FragmentTransaction transaction)
{
if ( mHome != null)
{
transaction.hide( mHome) ;
}
if ( mNotice != null)
{
transaction.hide( mNotice) ;
}
if ( mMine != null)
{
transaction.hide( mMine) ;
}
}
@Override
public void onClick(View v)
{
resetImgs() ;
switch (v.getId())
{
case R.id. tab_home:
setSelect( 0) ;
break;
case R.id. tab_notice:
setSelect( 1) ;
break;
case R.id. tab_mine:
setSelect( 2) ;
break;
default:
break;
}
}
/**
* 切换图片至暗色
*/
private void resetImgs() {
mImgHome.setImageResource(R.drawable. home_normal) ;
mImgNotice.setImageResource(R.drawable. notice_normal) ;
mImgMine.setImageResource(R.drawable. mine_normal) ;
}
}
布局文件:
tab_button:
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "65dp"
android :orientation= "horizontal"
android :background= "#FFFFFF" >
<LinearLayout
android :id= "@+id/tab_home"
android :layout_width= "0dp"
android :layout_height= "fill_parent"
android :layout_weight= "1"
android :gravity= "center"
android :orientation= "vertical" >
<ImageButton
android :background= "#FFFFFF"
android :id= "@+id/tab_home_img"
android :layout_width= "fill_parent"
android :layout_height= "fill_parent"
android :clickable=
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "65dp"
android :orientation= "horizontal"
android :background= "#FFFFFF" >
<LinearLayout
android :id= "@+id/tab_home"
android :layout_width= "0dp"
android :layout_height= "fill_parent"
android :layout_weight= "1"
android :gravity= "center"
android :orientation= "vertical" >
<ImageButton
android :background= "#FFFFFF"
android :id= "@+id/tab_home_img"
android :layout_width= "fill_parent"
android :layout_height= "fill_parent"
android :clickable=