仿b站demo(代码篇)

引导页,代码很简单,用个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) ;
    }
}
布局文件:

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=
  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值