Android Fragment 实战(一)

Fragment主要是要解决Android屏幕适配问题。同一个Activity内可包含N(>=0)个Fragment。

回顾了一下Fragment,做了个小例子。如图:

Android Fragment 实战(一)_第1张图片

用一个Activity来操作三个Fragment。

MainActivity代码如下:

package com.imeiren.qqfragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {
	private static final boolean D = true;
	private TextView mMessage;
	private TextView mContact;
	private TextView mNews;
	private int mIndex = 0;
	private final int COMMON_COLOR = Color.parseColor("#343434");
	private final int SELECT_COLOR = Color.parseColor("#18B4ED");

	private MessageFragment messageFragment;
	private ContactFragment contactFragment;
	private NewsFragment newsFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		mMessage = (TextView) findViewById(R.id.message);
		mMessage.setOnClickListener(this);
		mContact = (TextView) findViewById(R.id.contact);
		mContact.setOnClickListener(this);
		mNews = (TextView) findViewById(R.id.news);
		mNews.setOnClickListener(this);

		mIndex = mMessage.getId();
		setFragment(mIndex);
	}

	private void setFragment(int index) {
		if (D) {
			System.out.println("setFragment(int index)");
		}
		switch (index) {
		case R.id.message:
			if (messageFragment == null) {
				messageFragment = new MessageFragment();
			}
			getFragmentManager().beginTransaction()
					.replace(R.id.container, messageFragment).commit();
			break;
		case R.id.contact:
			if (contactFragment == null) {
				contactFragment = new ContactFragment();
			}
			getFragmentManager().beginTransaction()
					.replace(R.id.container, contactFragment).commit();
			break;
		case R.id.news:
			if (newsFragment == null) {
				newsFragment = new NewsFragment();
			}
			getFragmentManager().beginTransaction()
					.replace(R.id.container, newsFragment).commit();
			break;
		default:
			break;
		}
	}

	private void resetFooter() {
		mMessage.setTextColor(COMMON_COLOR);
		mContact.setTextColor(COMMON_COLOR);
		mNews.setTextColor(COMMON_COLOR);
	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		switch (viewId) {
		case R.id.message:
			if (mIndex != R.id.message) {
				mIndex = R.id.message;
				resetFooter();
				mMessage.setTextColor(SELECT_COLOR);
				setFragment(R.id.message);
			}
			break;
		case R.id.contact:
			if (mIndex != R.id.contact) {
				mIndex = R.id.contact;
				resetFooter();
				mContact.setTextColor(SELECT_COLOR);
				setFragment(R.id.contact);
			}
			break;
		case R.id.news:
			if (mIndex != R.id.news) {
				mIndex = R.id.news;
				resetFooter();
				mNews.setTextColor(SELECT_COLOR);
				setFragment(R.id.news);
			}
			break;
		default:
			break;
		}
	}
}
主要完成三个View的点击处理逻辑。

其布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${packageName}.${activityClass}" >

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/skin_header_bar_bg" >

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

    </FrameLayout>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/skin_slidetab_bg"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="消息"
                android:textColor="#18B4ED"
                android:textSize="18sp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/contact"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="联系人"
                android:textSize="18sp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/news"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="动态"
                android:textSize="18sp" />

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>
本来是想仿手机QQ用TabWidget来切换三个Fragment的,但是它的PNG文件名混淆得都很难搜索出想要的图片,肉眼找了半天也没找出来!果断用个TextView来代替。

例子源码下载地址

你可能感兴趣的:(Android Fragment 实战(一))