小言_互联网的博客

Andriod实现简单的微信界面跳转

425人阅读  评论(0)

Andriod实现简单的微信界面跳转
运行结果如下:

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener {

    private LinearLayout mTabWechat;
    private LinearLayout mTabFriend;
    private LinearLayout mTabContact;
    private LinearLayout mTabSettings;

    private ImageButton mImgWechat;
    private ImageButton mImgFriend;
    private ImageButton mImgContact;
    private ImageButton mImgSettings;


    private Fragment TabWechat=new wechatFragment();
    private Fragment TabFriend=new friendFragment();
    private Fragment TabContact=new contactFragment();
    private Fragment TabSettings=new settingsFragment();

    private FragmentManager fm;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initFragment();
        initEvent();
        SetSelect(0);
    }
    private void initFragment(){
        fm=getSupportFragmentManager();
        FragmentTransaction trans=fm.beginTransaction();
        trans.add(R.id.id_content,TabWechat);
        trans.add(R.id.id_content,TabFriend);
        trans.add(R.id.id_content,TabContact);
        trans.add(R.id.id_content,TabSettings);
        trans.commit();
    }

    private void initView(){
        mTabWechat=(LinearLayout) findViewById(R.id.id_tab_lay_wechat);
        mTabFriend=(LinearLayout) findViewById(R.id.id_tab_lay_friend);
        mTabContact=(LinearLayout) findViewById(R.id.id_tab_lay_contact);
        mTabSettings=(LinearLayout) findViewById(R.id.id_tab_lay_settings);

        mImgWechat=(ImageButton) findViewById(R.id.id_tab_weixin);
        mImgFriend=(ImageButton) findViewById(R.id.id_tab_frd);
        mImgContact=(ImageButton) findViewById(R.id.id_tab_address);
        mImgSettings=(ImageButton) findViewById(R.id.id_tab_settings);
    }
    private void SetSelect(int i){
        FragmentTransaction trans=fm.beginTransaction();
        hideFragment(trans);
        switch (i){
            case 0:
                trans.show(TabWechat);
                mImgWechat.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 1:
                trans.show(TabFriend);
                mImgFriend.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                trans.show(TabContact);
                mImgContact.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                trans.show(TabSettings);
                mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                    break;
        }
        trans.commit();
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(TabWechat);
        transaction.hide(TabFriend);
        transaction.hide(TabContact);
        transaction.hide(TabSettings);
    }

    @Override
    public void onClick(View view) {
        resetimg();
        switch (view.getId()){
            case R.id.id_tab_lay_wechat:
                SetSelect(0);
                break;
            case R.id.id_tab_lay_friend:
                SetSelect(1);
                break;
            case R.id.id_tab_lay_contact:
                SetSelect(2);
                break;
            case R.id.id_tab_lay_settings:
                SetSelect(3);
                break;
             default:
                 break;
        }
    }

    private void resetimg(){
        mImgWechat.setImageResource(R.drawable.tab_weixin_normal);
        mImgFriend.setImageResource(R.drawable.tab_find_frd_normal);
        mImgContact.setImageResource(R.drawable.tab_address_normal);
        mImgSettings.setImageResource(R.drawable.tab_settings_normal);
    }

    private void initEvent(){
        mTabWechat.setOnClickListener(this);
        mTabFriend.setOnClickListener(this);
        mTabContact.setOnClickListener(this);
        mTabSettings.setOnClickListener(this);
    }
}

核心代码已放在码云里面
https://gitee.com/snowsummer9/android


转载:https://blog.csdn.net/qq_41927432/article/details/104985808
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场