Android 之打造仿微信页面切换公共方法及实现

导读:本篇文章讲解 Android 之打造仿微信页面切换公共方法及实现,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

  1.切换调用:
  private void setSelect(int i){
    setTab(i);
}

private void setTab(i){
    //全部设置为默认图片
        ReSerImage();
    switch(i){
    case 0:
     //功能1.setImageResouce(R.drawable.....);
     break;
   case 1:
    //功能2.setImageResouce(R.drawable.....);
    break;
}

private void ResetImage(){
 功能1.setImageResouce(R.drawable...);   //设置为默认图标
 功能2.setImageResouce(R.drawable...);
}

//当是VerPager加滑动时,在SetSelect(i)调用VewPager.setCurrentItem(i);

//在ViewPager的onPageSelected(int ago)方法里面调用:
   int  currentItem=ViewPager.getCurrentItem();
       SetTab(currentItem);

具体实现

//我们使用的是android v4包下的fragment,这里必须要继承自FragmentActivity,而不是Activity
public class MainActivity extends FragmentActivity implements OnClickListener{
    //底部的4个导航控件
    private LinearLayout mTabWeixin;
    private LinearLayout mTabFrd;
    private LinearLayout mTabAddress;
    private LinearLayout mTabSetting;
    //底部4个导航控件中的图片按钮
    private ImageButton mImgWeixin;
    private ImageButton mImgFrd;
    private ImageButton mImgAddress;
    private ImageButton mImgSetting;
    //初始化4个Fragment
    private Fragment tab01;
    private Fragment tab02;
    private Fragment tab03;
    private Fragment tab04;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();//初始化所有的view
        initEvents();
        setSelect(0);//默认显示微信聊天界面
    }

    private void initEvents() {
        mTabWeixin.setOnClickListener(this);
        mTabFrd.setOnClickListener(this);
        mTabAddress.setOnClickListener(this);
        mTabSetting.setOnClickListener(this);

    }

    private void initView() {
        mTabWeixin = (LinearLayout)findViewById(R.id.id_tab_weixin);
        mTabFrd = (LinearLayout)findViewById(R.id.id_tab_frd);
        mTabAddress = (LinearLayout)findViewById(R.id.id_tab_address);
        mTabSetting = (LinearLayout)findViewById(R.id.id_tab_setting);
        mImgWeixin = (ImageButton)findViewById(R.id.id_tab_weixin_img);
        mImgFrd = (ImageButton)findViewById(R.id.id_tab_frd_img);
        mImgAddress = (ImageButton)findViewById(R.id.id_tab_address_img);
        mImgSetting = (ImageButton)findViewById(R.id.id_tab_setting_img);

    }



    @Override
    public void onClick(View v) {
        resetImg();
        switch (v.getId()) {
        case R.id.id_tab_weixin://当点击微信按钮时,切换图片为亮色,切换fragment为微信聊天界面
            setSelect(0);
            break;
        case R.id.id_tab_frd:
            setSelect(1);
            break;
        case R.id.id_tab_address:
            setSelect(2);
            break;
        case R.id.id_tab_setting:
            setSelect(3);
            break;

        default:
            break;
        }

    }

    /*
     * 将图片设置为亮色的;切换显示内容的fragment
     * */
    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();//创建一个事务
        hideFragment(transaction);//我们先把所有的Fragment隐藏了,然后下面再开始处理具体要显示的Fragment
        switch (i) {
        case 0:
            if (tab01 == null) {
                tab01 = new WeixinFragment();
                /*
                 * 将Fragment添加到活动中,public abstract FragmentTransaction add (int containerViewId, Fragment fragment)
                *containerViewId即为Optional identifier of the container this fragment is to be placed in. If 0, it will not be placed in a container.
                 * */
                transaction.add(R.id.id_content, tab01);//将微信聊天界面的Fragment添加到Activity中
            }else {
                transaction.show(tab01);
            }
            mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
            break;
        case 1:
            if (tab02 == null) {
                tab02 = new FrdFragment();
                transaction.add(R.id.id_content, tab02);
            }else {
                transaction.show(tab02);
            }
            mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
            break;
        case 2:
            if (tab03 == null) {
                tab03 = new AddressFragment();
                transaction.add(R.id.id_content, tab03);
            }else {
                transaction.show(tab03);
            }
            mImgAddress.setImageResource(R.drawable.tab_address_pressed);
            break;
        case 3:
            if (tab04 == null) {
                tab04 = new SettingFragment();
                transaction.add(R.id.id_content, tab04);
            }else {
                transaction.show(tab04);
            }
            mImgSetting.setImageResource(R.drawable.tab_settings_pressed);
            break;

        default:
            break;
        }
        transaction.commit();//提交事务
    }

    /*
     * 隐藏所有的Fragment
     * */
    private void hideFragment(FragmentTransaction transaction) {
        if (tab01 != null) {
            transaction.hide(tab01);
        }
        if (tab02 != null) {
            transaction.hide(tab02);
        }
        if (tab03 != null) {
            transaction.hide(tab03);
        }
        if (tab04 != null) {
            transaction.hide(tab04);
        }

    }

    private void resetImg() {
        mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
        mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
        mImgAddress.setImageResource(R.drawable.tab_address_normal);
        mImgSetting.setImageResource(R.drawable.tab_settings_normal);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/12880.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!