1. 程式人生 > >仿QQ聊天介面基本的Fragment用法。

仿QQ聊天介面基本的Fragment用法。

先寫好佈局,上面用一個FrameLayout佈局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.xh.tx.fragmenapp.MainActivity" >

    <FrameLayout 
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="9"></FrameLayout>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="15sp"
            android:text="訊息" />

        <TextView
            android:id="@+id/contact"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="15sp"
            android:text="聯絡人" />
        <TextView
            android:id="@+id/dongtai"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="15sp"
            android:text="動態" />
        
    </LinearLayout>

</LinearLayout>
佈局顯示如下圖:

接下來寫3個訊息,聯絡人,動態類繼承Fragment。

訊息類:

package com.xh.tx.fragmenapp;

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

public class MessageFragment extends Fragment{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view=inflater.inflate(android.R.layout.simple_list_item_1,	null);
		TextView text=(TextView) view.findViewById(android.R.id.text1);
		text.setText("訊息介面");
		
		return	view;
	}
	
}
聯絡人類:
package com.xh.tx.fragmenapp;

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

public class ContactFragment extends ListFragment{

//	@Override
//	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		View view=inflater.inflate(android.R.layout.simple_list_item_1,	null);
//		TextView text=(TextView) view.findViewById(android.R.id.text1);
//		text.setText("聯絡人介面");
//		
//		return	view;
//	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		String[] date=new String[50];
		for(int i=0;i<date.length;i++){
			date[i]="聯絡人"+i;
		}
		ArrayAdapter adapter=new ArrayAdapter(getActivity(), android.R.layout.simple_expandable_list_item_1, date);
		this.setListAdapter(adapter);
	}
}
動態類:
package com.xh.tx.fragmenapp;

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

public class dongtaiFragment extends Fragment{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view=inflater.inflate(android.R.layout.simple_list_item_1,	null);
		TextView text=(TextView) view.findViewById(android.R.id.text1);
		text.setText("動態更新");
		
		return	view;
	}
	
}
最後在主程式碼裡寫實現功能:
package com.xh.tx.fragmenapp;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {
	private	TextView message,contact,dongtai;
	
	private	MessageFragment messageFragment;
	private	ContactFragment contactFragment;
	private	dongtaiFragment dongtaiFragment;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		message=(TextView) findViewById(R.id.message);
		contact=(TextView) findViewById(R.id.contact);
		dongtai=(TextView) findViewById(R.id.dongtai);
		
		messageFragment=new MessageFragment();
		contactFragment=new ContactFragment();
		dongtaiFragment=new dongtaiFragment();
		choose(0);
		message.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				choose(0);
				
			}
		});
		contact.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				choose(1);
				
			}
		});
		dongtai.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				choose(2);
				
			}
		});
	}
	private void choose(int pos){
		if(pos==0){
			message.setTextColor(Color.RED);
			message.setBackgroundColor(Color.LTGRAY);
			
			contact.setTextColor(Color.DKGRAY);
			contact.setBackgroundColor(Color.WHITE);
			
			dongtai.setTextColor(Color.DKGRAY);
			dongtai.setBackgroundColor(Color.WHITE);
		}
		if(pos==1){
			contact.setTextColor(Color.RED);
			contact.setBackgroundColor(Color.LTGRAY);	
			
			message.setTextColor(Color.DKGRAY);
			message.setBackgroundColor(Color.WHITE);
			
			dongtai.setTextColor(Color.DKGRAY);
			dongtai.setBackgroundColor(Color.WHITE);
		}
		if(pos==2){
			dongtai.setTextColor(Color.RED);
			dongtai.setBackgroundColor(Color.LTGRAY);	
			
			message.setTextColor(Color.DKGRAY);
			message.setBackgroundColor(Color.WHITE);
			
			contact.setTextColor(Color.DKGRAY);
			contact.setBackgroundColor(Color.WHITE);
		}
		if(pos==0){
			loadFragment(messageFragment);
		}
		
		if(pos==1){
			loadFragment(contactFragment);
		}
		if(pos==2){
			loadFragment(dongtaiFragment);
		}
	}
	private	void loadFragment(Fragment f){
		FragmentManager fm=this.getFragmentManager();
		FragmentTransaction ft=fm.beginTransaction();
		ft.replace(R.id.content, f);
		ft.commit();
	}

}
結果顯示如下圖: