Fragment实现Tab

来源:互联网 发布:做谱子的软件 编辑:程序博客网 时间:2024/05/24 04:33

优化布局层次

include

merge

ViewStub


top.xml

<?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="45dp"    android:background="@drawable/title_bar"    android:gravity="center"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="WeChat"        android:textSize="20sp"        android:textStyle="bold"        android:textColor="#ffffffff"/></LinearLayout>




bottom.xml

<?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:background="@drawable/bottom_bar"    android:orientation="horizontal">    <LinearLayout        android:id="@+id/id_tab_wechat"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:orientation="vertical"        android:gravity="center">        <ImageButton            android:id="@+id/id_tab_wechat_image"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:clickable="false"            android:background="#00000000"            android:src="@drawable/tab_weixin_pressed" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="#ffffff"            android:text="WeChat" />    </LinearLayout>    <LinearLayout        android:id="@+id/id_tab_friend"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:orientation="vertical"        android:gravity="center">        <ImageButton            android:id="@+id/id_tab_friend_image"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:clickable="false"            android:background="#00000000"            android:src="@drawable/tab_find_frd_normal" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="#ffffff"            android:text="Friend" />    </LinearLayout>    <LinearLayout        android:id="@+id/id_tab_address"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:orientation="vertical"        android:gravity="center">        <ImageButton            android:id="@+id/id_tab_address_image"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:clickable="false"            android:background="#00000000"            android:src="@drawable/tab_address_normal" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="#ffffff"            android:text="Address" />    </LinearLayout>    <LinearLayout        android:id="@+id/id_tab_settings"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:orientation="vertical"        android:gravity="center">        <ImageButton            android:id="@+id/id_tab_settings_image"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:clickable="false"            android:background="#00000000"            android:src="@drawable/tab_settings_normal" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="#ffffff"            android:text="Settings" />    </LinearLayout></LinearLayout>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <include layout="@layout/top" />    <FrameLayout        android:id="@+id/id_content"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1">    </FrameLayout>    <include layout="@layout/bottom" /></LinearLayout>

WeChatFragment

FriendFragment

AddressFragment

SettingsFragment


public class WeChatFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        return inflater.inflate(R.layout.tab01, container, false);    }}


其他的只需更改tab01


public class MainActivity extends FragmentActivity implements View.OnClickListener {    private LinearLayout mTabWeChat;    private LinearLayout mTabFriend;    private LinearLayout mTabAddress;    private LinearLayout mTabSettings;    private ImageButton mImgWeChat;    private ImageButton mImgFriend;    private ImageButton mImgAddress;    private ImageButton mImgSettings;    private Fragment mTab01;    private Fragment mTab02;    private Fragment mTab03;    private Fragment mTab04;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        initView();        initEvents();        setSelect(0);    }    private void initEvents() {        mTabWeChat.setOnClickListener(this);        mTabFriend.setOnClickListener(this);        mTabAddress.setOnClickListener(this);        mTabSettings.setOnClickListener(this);    }    private void initView() {        mTabWeChat = (LinearLayout) findViewById(R.id.id_tab_wechat);        mTabFriend = (LinearLayout) findViewById(R.id.id_tab_friend);        mTabAddress = (LinearLayout) findViewById(R.id.id_tab_address);        mTabSettings = (LinearLayout) findViewById(R.id.id_tab_settings);        mImgWeChat = (ImageButton) findViewById(R.id.id_tab_wechat_image);        mImgFriend = (ImageButton) findViewById(R.id.id_tab_friend_image);        mImgAddress = (ImageButton) findViewById(R.id.id_tab_address_image);        mImgSettings = (ImageButton) findViewById(R.id.id_tab_settings_image);//        mTab01 = new WeChatFragment();//        mTab02 = new FriendFragment();//        mTab01 = new AddressFragment();//        mTab01 = new SettingsFragment();    }    private void setSelect(int i) {//        android.app.FragmentManager fm = getFragmentManager();        FragmentManager fm = getSupportFragmentManager();        FragmentTransaction transaction = fm.beginTransaction();        hideFragment(transaction);        //把图片设置为亮的        //设置内容区域        switch (i) {            case 0:                if (mTab01 == null) {                    mTab01 = new WeChatFragment();                    transaction.add(R.id.id_content, mTab01);                } else {                    transaction.show(mTab01);                }                mImgWeChat.setImageResource(R.drawable.tab_weixin_pressed);                break;            case 1:                if (mTab02 == null) {                    mTab02 = new FriendFragment();                    transaction.add(R.id.id_content, mTab02);                } else {                    transaction.show(mTab02);                }                mImgFriend.setImageResource(R.drawable.tab_find_frd_pressed);                break;            case 2:                if (mTab03 == null) {                    mTab03 = new AddressFragment();                    transaction.add(R.id.id_content, mTab03);                } else {                    transaction.show(mTab03);                }                mImgAddress.setImageResource(R.drawable.tab_address_pressed);                break;            case 3:                if (mTab04 == null) {                    mTab04 = new SettingsFragment();                    transaction.add(R.id.id_content, mTab04);                } else {                    transaction.show(mTab04);                }                mImgSettings.setImageResource(R.drawable.tab_settings_pressed);                break;        }        transaction.commit();    }    private void hideFragment(FragmentTransaction transaction) {        if (mTab01 != null) {            transaction.hide(mTab01);        }        if (mTab02 != null) {            transaction.hide(mTab02);        }        if (mTab03 != null) {            transaction.hide(mTab03);        }        if (mTab04 != null) {            transaction.hide(mTab04);        }    }    @Override    public void onClick(View v) {        resetImgs();        switch (v.getId()) {            case R.id.id_tab_wechat:                setSelect(0);                break;            case R.id.id_tab_friend:                setSelect(1);                break;            case R.id.id_tab_address:                setSelect(2);                break;            case R.id.id_tab_settings:                setSelect(3);                break;        }    }    /**     * 切换图片至暗色     */    private void resetImgs() {        mImgWeChat.setImageResource(R.drawable.tab_weixin_normal);        mImgFriend.setImageResource(R.drawable.tab_find_frd_normal);        mImgAddress.setImageResource(R.drawable.tab_address_normal);        mImgSettings.setImageResource(R.drawable.tab_settings_normal);    }}


此种方法不支持左右滑动


https://github.com/LiuchangDuan/demo/tree/master/fragmenttabdemo



0 0