稳扎稳打_Android开发课[34]_用户界面之Fragment

步步为营_Android开发课[34]_用户界面之Fragment

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305


  • 主题:用户界面之Fragment
    -自从Android在3.0版本加入Fragment,Fragment越来越被大家广泛使用。

Fragment(实例):
实现效果:(我不知道怎么弄动态图,借用好友:光仔December的博客图片一用)
稳扎稳打_Android开发课[34]_用户界面之Fragment
上面是1个Activity里有4个Fragment,这4个Fragment实现视图切换。

实例源码:
activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#336699" >

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/message_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/aa" />

                <TextView
                    android:id="@+id/message_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="消息"
                    android:textColor="#82858b" />
            </LinearLayout>
        </RelativeLayout>

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/contacts_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/bb" />

                <TextView
                    android:id="@+id/contacts_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="联系人"
                    android:textColor="#82858b" />
            </LinearLayout>
        </RelativeLayout>

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/news_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/cc" />

                <TextView
                    android:id="@+id/news_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="动态"
                    android:textColor="#82858b" />
            </LinearLayout>
        </RelativeLayout>

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/setting_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/dd" />

                <TextView
                    android:id="@+id/setting_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="设置"
                    android:textColor="#82858b" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

fragment_message.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/myimage" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="10dp"
            android:text="这是消息界面"
            android:textSize="20sp" />
    </LinearLayout>

</RelativeLayout>

其他三个fragment_contacts.xml,fragment_news.xml,fragment_setting.xml都是一样的写法,这里就省略了。

MessageFragment.java:

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

public class MessageFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View messageLayout = inflater.inflate(R.layout.message_layout,
                container, false);
        return messageLayout;
    }

}

其他三个contactsFragment.java,newsFragment.java,settingFragment.java也都是一样的写法,这里就省略了。

MainActivity.java:

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;

public class MainActivity extends Activity implements OnClickListener {

    private MessageFragment messageFragment;
    private ContactsFragment contactsFragment;
    private NewsFragment newsFragment;
    private SettingFragment settingFragment;
    private View messageLayout;
    private View contactsLayout;
    private View newsLayout;
    private View settingLayout;
    private ImageView messageImage;
    private ImageView contactsImage;
    private ImageView newsImage;
    private ImageView settingImage;
    private TextView messageText;
    private TextView contactsText;
    private TextView newsText;
    private TextView settingText;
    private FragmentManager fragmentManager;

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

        initViews();
        fragmentManager = getFragmentManager();
        setTabSelection(0);
    }


    private void initViews() {
        messageLayout = findViewById(R.id.message_layout);
        contactsLayout = findViewById(R.id.contacts_layout);
        newsLayout = findViewById(R.id.news_layout);
        settingLayout = findViewById(R.id.setting_layout);

        messageImage = (ImageView) findViewById(R.id.message_image);
        contactsImage = (ImageView) findViewById(R.id.contacts_image);
        newsImage = (ImageView) findViewById(R.id.news_image);
        settingImage = (ImageView) findViewById(R.id.setting_image);

        messageText = (TextView) findViewById(R.id.message_text);
        contactsText = (TextView) findViewById(R.id.contacts_text);
        newsText = (TextView) findViewById(R.id.news_text);
        settingText = (TextView) findViewById(R.id.setting_text);

        messageLayout.setOnClickListener(this);
        contactsLayout.setOnClickListener(this);
        newsLayout.setOnClickListener(this);
        settingLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.message_layout:

            setTabSelection(0);
            break;
        case R.id.contacts_layout:

            setTabSelection(1);
            break;
        case R.id.news_layout:

            setTabSelection(2);
            break;
        case R.id.setting_layout:

            setTabSelection(3);
            break;
        default:
            break;
        }
    }


    private void setTabSelection(int index) {

        clearSelection();

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        hideFragments(transaction);
        switch (index) {
        case 0:

            messageImage.setImageResource(R.drawable.a);
            messageText.setTextColor(Color.WHITE);
            if (messageFragment == null) {

                messageFragment = new MessageFragment();
                transaction.add(R.id.content, messageFragment);
            } else {

                transaction.show(messageFragment);
            }
            break;
        case 1:

            contactsImage.setImageResource(R.drawable.b);
            contactsText.setTextColor(Color.WHITE);
            if (contactsFragment == null) {

                contactsFragment = new ContactsFragment();
                transaction.add(R.id.content, contactsFragment);
            } else {

                transaction.show(contactsFragment);
            }
            break;
        case 2:

            newsImage.setImageResource(R.drawable.c);
            newsText.setTextColor(Color.WHITE);
            if (newsFragment == null) {

                newsFragment = new NewsFragment();
                transaction.add(R.id.content, newsFragment);
            } else {

                transaction.show(newsFragment);
            }
            break;
        case 3:
        default:

            settingImage.setImageResource(R.drawable.d);
            settingText.setTextColor(Color.WHITE);
            if (settingFragment == null) {

                settingFragment = new SettingFragment();
                transaction.add(R.id.content, settingFragment);
            } else {

                transaction.show(settingFragment);
            }
            break;
        }
        transaction.commit();
    }


    private void clearSelection() {
        messageImage.setImageResource(R.drawable.aa);
        messageText.setTextColor(Color.parseColor("#82858b"));
        contactsImage.setImageResource(R.drawable.bb);
        contactsText.setTextColor(Color.parseColor("#82858b"));
        newsImage.setImageResource(R.drawable.cc);
        newsText.setTextColor(Color.parseColor("#82858b"));
        settingImage.setImageResource(R.drawable.dd);
        settingText.setTextColor(Color.parseColor("#82858b"));
    }

    //隐藏Fragment
    private void hideFragments(FragmentTransaction transaction) {
        if (messageFragment != null) {
            transaction.hide(messageFragment);
        }
        if (contactsFragment != null) {
            transaction.hide(contactsFragment);
        }
        if (newsFragment != null) {
            transaction.hide(newsFragment);
        }
        if (settingFragment != null) {
            transaction.hide(settingFragment);
        }
    }
}

图片资源:

a稳扎稳打_Android开发课[34]_用户界面之Fragmentaa稳扎稳打_Android开发课[34]_用户界面之Fragmentb稳扎稳打_Android开发课[34]_用户界面之Fragmentbb稳扎稳打_Android开发课[34]_用户界面之Fragmentc稳扎稳打_Android开发课[34]_用户界面之Fragmentcc稳扎稳打_Android开发课[34]_用户界面之Fragmentd稳扎稳打_Android开发课[34]_用户界面之Fragmentdd稳扎稳打_Android开发课[34]_用户界面之Fragment

运行结果:

稳扎稳打_Android开发课[34]_用户界面之Fragment

源码下载:

FragmentDemo

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305