FragmentManager+Fragment实现微信界面布局

比起侧边栏,个人感觉底部按钮切换界面的方式对于使用者来说,操作更方便,更直观,由此决定写个Demo备着。 
主界面
主界面xml:

<include layout="@layout/title_bar"/>

    <FrameLayout
        android:id="@+id/id_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <include layout="@layout/bottom_bar"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

首先include一个title作为不同界面标题的提示。 
其次放一个FrameLayout放需要加载的界面。 
最后include底部按钮。 
title_bar.xml就正中间一个TextView,很简单,代码省略。 
bottom_bar.xml的思路是通过LinearLayout布局,一共放四个LinearLayout,给每个LinearLayout添加点击事件,同时每个LinearLayout内有包含一个ImgButton和一个TextView,ImgButton放置图标并屏蔽点击事件,TextView显示按钮名称。

<LinearLayout
            android:id="@+id/TabBtnWeChat"
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:descendantFocusability="beforeDescendants"
            android:layout_height="match_parent">
            <ImageButton
                android:id="@+id/imgWeChat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:clickable="false"
                android:src="@drawable/tab_weixin_pressed"/>
            <TextView
                android:text="WeChat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"/>
        </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

android:descendantFocusability=”beforeDescendants”会覆盖子类控件而直接获得焦点。 
剩余三个以此类推设置。 
MainActivity.java

package com.example.zhan.mfragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MainWeChat mWeChat;
    private MainFriend mFriend;
    private MainAddress mAddress;
    private MainSettings mSettings;

    private LinearLayout mTabBtnWeChat;
    private LinearLayout mTabBtnFriend;
    private LinearLayout mTabBtnAddress;
    private LinearLayout mTabBtnSettings;

    private FragmentManager fragmentManager;

    TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        fragmentManager = getFragmentManager();
        setTabSelection(0);
        t = (TextView) findViewById(R.id.Title1);
        t.setText("WeChat");
    }

    private void initViews()
    {

        mTabBtnWeChat = (LinearLayout) findViewById(R.id.TabBtnWeChat);
        mTabBtnFriend = (LinearLayout) findViewById(R.id.TabBtnFriend);
        mTabBtnAddress = (LinearLayout) findViewById(R.id.TabBtnAddress);
        mTabBtnSettings = (LinearLayout) findViewById(R.id.TabBtnSettings);

        mTabBtnWeChat.setOnClickListener(this);
        mTabBtnFriend.setOnClickListener(this);
        mTabBtnAddress.setOnClickListener(this);
        mTabBtnSettings.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.TabBtnWeChat:
                t.setText("WeChat");
                setTabSelection(0);
                break;
            case R.id.TabBtnFriend:
                t.setText("Friend");
                setTabSelection(1);
                break;
            case R.id.TabBtnAddress:
                t.setText("Address");
                setTabSelection(2);
                break;
            case R.id.TabBtnSettings:
                t.setText("Settings");
                setTabSelection(3);
                break;
            default:
                break;
        }
    }

    /**
     * 根据传入的index参数来设置选中的tab页。
     */
    private void setTabSelection(int index)
    {
        // 重置按钮
        resetBtn();
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0:
                // 当点击了消息tab时,改变控件的图片和文字颜色
                ((ImageButton) mTabBtnWeChat.findViewById(R.id.imgWeChat)).setImageResource(R.drawable.tab_weixin_pressed);
                if (mWeChat == null) {
                    // 如果MessageFragment为空,则创建一个并添加到界面上
                    mWeChat = new MainWeChat();
                    transaction.add(R.id.id_content, mWeChat);
                } else {
                    // 如果MessageFragment不为空,则直接将它显示出来
                    transaction.show(mWeChat);
                }
                break;
            case 1:
                ((ImageButton) mTabBtnFriend.findViewById(R.id.imgFriend)).setImageResource(R.drawable.tab_find_frd_pressed);
                if (mFriend == null) {
                    mFriend = new MainFriend();
                    transaction.add(R.id.id_content, mFriend);
                } else {
                    transaction.show(mFriend);
                }
                break;
            case 2:
                ((ImageButton) mTabBtnAddress.findViewById(R.id.imgAddress)).setImageResource(R.drawable.tab_address_pressed);
                if (mAddress == null) {
                    mAddress = new MainAddress();
                    transaction.add(R.id.id_content, mAddress);
                } else {
                    transaction.show(mAddress);
                }
                break;
            case 3:
                ((ImageButton) mTabBtnSettings.findViewById(R.id.imgSettings)).setImageResource(R.drawable.tab_settings_pressed);
                if (mSettings == null) {
                    mSettings = new MainSettings();
                    transaction.add(R.id.id_content, mSettings);
                } else {
                    transaction.show(mSettings);
                }
                break;
        }transaction.commit();
    }
    private void resetBtn()
    {
        ((ImageButton) mTabBtnWeChat.findViewById(R.id.imgWeChat))
                .setImageResource(R.drawable.tab_weixin_normal);
        ((ImageButton) mTabBtnFriend.findViewById(R.id.imgFriend))
                .setImageResource(R.drawable.tab_find_frd_normal);
        ((ImageButton) mTabBtnAddress.findViewById(R.id.imgAddress))
                .setImageResource(R.drawable.tab_address_normal);
        ((ImageButton) mTabBtnSettings.findViewById(R.id.imgSettings))
                .setImageResource(R.drawable.tab_settings_normal);
    }
    /**
     * 将所有的Fragment都置为隐藏状态。
     * 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction)
    {
        if (mWeChat != null)
        {
            transaction.hide(mWeChat);
        }
        if (mFriend != null)
        {
            transaction.hide(mFriend);
        }
        if (mAddress != null)
        {
            transaction.hide(mAddress);
        }
        if (mSettings != null)
        {
            transaction.hide(mSettings);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165

这部分其实没什么复杂的,就是初始化,添加界面,没有的创建,有的显示(或隐藏)就好了。 
MainWeChat.java

package com.example.zhan.mfragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


public class MainWeChat extends Fragment implements View.OnClickListener
{
    int i = 0;
    View v;
    TextView tv ;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        v = inflater.inflate(R.layout.wechat_view,container,false);
        Button btn = (Button)v.findViewById(R.id.fsbtn);
        tv = (TextView) v.findViewById(R.id.fstv);
        btn.setOnClickListener(this);
        return v;
    }
    @Override
    public void onClick(View v)
    {
        switch (v.getId()){
            case R.id.fsbtn:
                if(i == 0){
                tv.setText("Click");
                i++;
                }else if(i == 1){
                    tv.setText("WeChat");
                    i--;
                }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

这一部分主要是继承Fragment,然后把要添加的xml在onCreateView中返回就好了。 
到这里,回过头来看一下整个流程,其实就是在MainActivity关联的xml上的FrameLayout里添加一个xml,所以想要对wechat_view.xml中的控件操作,不是简简单单的findViewById就好了。所以需要

View v = inflater.inflate(R.layout.wechat_view,container,false);
  • 1

将xml布局转换为view对象,再利用view对象,找到布局中的组件

Button btn = (Button)v.findViewById(R.id.fsbtn);
tv = (TextView) v.findViewById(R.id.fstv);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ViewPage Fragment 是 Android 中的一个工具类,用于在一个 Activity 中显示多个 Fragment,并且可以通过滑动来切换不同的 Fragment。仿微信的话,可以使用 ViewPage Fragment实现类似微信的主界面,其中每个 Fragment 分别对应微信的不同功能模块。 首先,我们可以创建一个主界面的 Activity,该 Activity 包含一个 ViewPage,用于显示不同的 Fragment。然后,创建多个 Fragment,每个 Fragment 分别负责显示微信的不同功能模块,比如聊天列表、联系人列表、发现等。 在每个 Fragment 中,可以使用 RecyclerView 来展示列表数据,并根据不同的业务需求进行相应的逻辑处理。比如,在聊天列表中,可以显示每个聊天会话的头像、昵称、最近一条消息等信息,并通过点击监听实现跳转到聊天界面的功能。 另外,可以为每个 Fragment 添加相应的菜单选项,仿微信的底部导航栏,用于在不同的 Fragment 之间进行切换。通过监听导航栏的点击事件,并配合 ViewPage 的滑动,可以实现类似微信的切换效果。 此外,可以通过 FragmentManager 来管理 Fragment 的生命周期,并实现 Fragment 之间的通信。比如,在聊天界面中发送一条消息后,可以通过调用 FragmentManager 的方法刷新聊天列表界面,并更新最近一条消息的显示。 总而言之,使用 ViewPage Fragment 可以很方便地实现一个仿微信界面,通过不同的 Fragment 展示不同的功能模块,使用户可以在同一个 Activity 中进行多个功能的切换和操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值