博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 仿QQ消息界面
阅读量:7042 次
发布时间:2019-06-28

本文共 6106 字,大约阅读时间需要 20 分钟。

values 下面

dimens.xml

16dp
16dp

 

主布局

activity_switch.xml

两个fragment

fragment_message.xml

fragment_call.xml

主页面

SwitchActivity.java

package com.example.switchutils;import android.graphics.Color;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.View.OnClickListener;import android.view.Window;import android.widget.Button;public class SwitchActivity extends FragmentActivity {    private Button btn_message,btn_call;        private CallFragment callFragment;    private MessageFragment messageFragment;        public static final int MESSAGE_FRAGMENT_TYPE = 1;    public static final int CALL_FRAGMENT_TYPE = 2;    public int currentFragmentType = -1;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_switch);                btn_message = (Button)findViewById(R.id.btn_message);        btn_call = (Button)findViewById(R.id.btn_call);        btn_message.setOnClickListener(onClicker);        btn_call.setOnClickListener(onClicker);                FragmentManager fragmentManager = getSupportFragmentManager();        if (savedInstanceState != null) {            int type = savedInstanceState.getInt("currentFragmentType");            messageFragment = (MessageFragment)fragmentManager.findFragmentByTag("message");            callFragment = (CallFragment)fragmentManager.findFragmentByTag("call");            if(type > 0)                loadFragment(type);        } else {            FragmentTransaction transaction = fragmentManager                    .beginTransaction();            Fragment mainFragment = fragmentManager.findFragmentByTag("message");            if (mainFragment != null) {                transaction.replace(R.id.fl_content, mainFragment);                transaction.commit();            } else {                loadFragment(MESSAGE_FRAGMENT_TYPE);            }        }            }        @Override    protected void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        outState.putInt("lastFragmentTag", currentFragmentType);    }        private void switchFragment(int type) {        switch (type) {        case MESSAGE_FRAGMENT_TYPE:            loadFragment(MESSAGE_FRAGMENT_TYPE);            break;        case CALL_FRAGMENT_TYPE:            loadFragment(CALL_FRAGMENT_TYPE);            break;        }            }    private void loadFragment(int type) {        FragmentManager fragmentManager = getSupportFragmentManager();        FragmentTransaction transaction = fragmentManager.beginTransaction();        if (type == CALL_FRAGMENT_TYPE) {            if (callFragment == null) {                callFragment = new CallFragment();                transaction.add(R.id.fl_content, callFragment, "zhishi");            } else {                transaction.show(callFragment);            }            if (messageFragment != null) {                transaction.hide(messageFragment);            }            currentFragmentType = MESSAGE_FRAGMENT_TYPE;        } else {            if (messageFragment == null) {                messageFragment = new MessageFragment();                transaction.add(R.id.fl_content, messageFragment, "wenda");            } else {                transaction.show(messageFragment);            }            if (callFragment != null) {                transaction.hide(callFragment);            }            currentFragmentType = CALL_FRAGMENT_TYPE;        }        transaction.commitAllowingStateLoss();    }        private OnClickListener onClicker = new OnClickListener() {        @Override        public void onClick(View v) {            switch (v.getId()) {            case R.id.btn_message:                btn_message.setTextColor(Color.parseColor("#df3031"));                btn_call.setTextColor(Color.WHITE);                btn_message                        .setBackgroundResource(R.drawable.baike_btn_pink_left_f_96);                btn_call                        .setBackgroundResource(R.drawable.baike_btn_trans_right_f_96);                switchFragment(MESSAGE_FRAGMENT_TYPE);                                break;            case R.id.btn_call:                                btn_message.setTextColor(Color.WHITE);                btn_call.setTextColor(Color.parseColor("#df3031"));                btn_message                        .setBackgroundResource(R.drawable.baike_btn_trans_left_f_96);                btn_call                        .setBackgroundResource(R.drawable.baike_btn_pink_right_f_96);                switchFragment(CALL_FRAGMENT_TYPE);                                break;                        }        }    };}

两个fragment

MessageFragment.java

package com.example.switchutils;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class MessageFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater,             ViewGroup container, Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_message, null);    }}

CallFragment.java

package com.example.switchutils;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class CallFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater,             ViewGroup container, Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_call, null);    }}

 

转载于:https://www.cnblogs.com/1426837364qqcom/p/5443069.html

你可能感兴趣的文章
Redux入门学习
查看>>
我的友情链接
查看>>
利用AWS boto实现EC2 存储卷的自动快照
查看>>
微软私有云解决方案专家认证之路
查看>>
曾经的痛啊 关于 becomeFirstResponder
查看>>
Android Service
查看>>
解决iphone safari上的圆角问题
查看>>
zabbix源码安装
查看>>
phpcms笔记
查看>>
查看系统用户登录信息命令
查看>>
CMS之图片管理(2)
查看>>
php 魔术方法总结(持续更新)
查看>>
利用ADMT进行Exchange跨域迁移之一:配置域信任
查看>>
javascript获取系统当前时间
查看>>
【java解惑】java中那些反常识的小知识
查看>>
bash内部命令变量
查看>>
python3.4 之sqlite3,pymysql
查看>>
网络接口
查看>>
centos下Extmail的搭建
查看>>
我的友情链接
查看>>