FragmentApp界面(2)

51 篇文章 0 订阅

FragmentApp界面(2)

效果图:


代码如下:

package com.example.fragmentapp;

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.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private static	int POSITION=-1;

	private	TextView[] texts=new TextView[3];
	private	Fragment[] fragments=new Fragment[3];
	
	private	TextView title;

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

		title = (TextView) findViewById(R.id.title);

		texts[0]=(TextView) findViewById(R.id.message);
		texts[1]=(TextView) findViewById(R.id.contact);
		texts[2]=(TextView) findViewById(R.id.start);
		
		
		fragments[0] = new MessageFragment();
		fragments[1]= new ContactFragment();
		fragments[2] = new StartFragment();

		choose(0);
		
		addTextViewListener(texts[0], 0);
		addTextViewListener(texts[1], 1);
		addTextViewListener(texts[2], 2);
	}
	
	private void addTextViewListener(TextView text, final int pos) {
		text.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				choose(pos);
				
			}
		});
		
	}
		
//		messageText.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				choose(0);
//			}
//		});
//
//		contactText.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				choose(1);
//			}
//		});
//
//		startText.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				choose(2);
//			}
//		});
//	}

	

	private void choose(int pos) {
		//重复的点击
		if(pos == POSITION)
			return;
		
		for(int i=0;i<texts.length;i++){
			//选中
			if(pos == i){
				texts[i].setTextColor(Color.RED);
				texts[i].setBackgroundColor(Color.LTGRAY);
				
				title.setText(texts[i].getText() + "");
				
				loadFragment(fragments[i]);
			}
			//未被选中
			else{
				texts[i].setTextColor(Color.DKGRAY);
				texts[i].setBackgroundColor(Color.WHITE);
			}	
		}
		POSITION = pos;
	}
		
//		if (pos == 0) {
//			messageText.setTextColor(Color.RED);
//			messageText.setBackgroundColor(Color.LTGRAY);
//
//			contactText.setTextColor(Color.DKGRAY);
//			contactText.setBackgroundColor(Color.WHITE);
//
//			startText.setTextColor(Color.DKGRAY);
//			startText.setBackgroundColor(Color.WHITE);
//
//			loadFragment(messageFragment);
//
//		}
//		if (pos == 1) {
//			contactText.setTextColor(Color.RED);
//			contactText.setBackgroundColor(Color.LTGRAY);
//
//			messageText.setTextColor(Color.DKGRAY);
//			messageText.setBackgroundColor(Color.WHITE);
//
//			startText.setTextColor(Color.DKGRAY);
//			startText.setBackgroundColor(Color.WHITE);
//
//			loadFragment(contactFragment);
//		}
//
//		if (pos == 2) {
//			startText.setTextColor(Color.RED);
//			startText.setBackgroundColor(Color.LTGRAY);
//
//			messageText.setTextColor(Color.DKGRAY);
//			messageText.setBackgroundColor(Color.WHITE);
//
//			contactText.setTextColor(Color.DKGRAY);
//			contactText.setBackgroundColor(Color.WHITE);
//
//			loadFragment(startFragment);
//		}

	
	private void loadFragment(Fragment f) {

		FragmentManager fm = this.getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();

		ft.replace(R.id.content, f);
		ft.commit();

	}
}

package com.example.fragmentapp;

import android.app.Fragment;
import android.graphics.Color;
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);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		TextView text = (TextView) view.findViewById(android.R.id.text1);
		text.setText("消息界面");
		text.setBackgroundColor(Color.YELLOW);
		
	}
}

package com.example.fragmentapp;

//import android.app.Fragment;
import android.app.ListFragment;
//import android.graphics.Color;
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 void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		
		String[] data=new String[66];
		for(int i=0;i<data.length;i++){
			data[i]="联系人:"+i;
		}
		
		ArrayAdapter adapter=new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,data);
		this.setListAdapter(adapter);
	}
	
	
//	@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("联系人界面");
//		text.setBackgroundColor(Color.GREEN);
//		
//		return	view;
//	}

}

package com.example.fragmentapp;

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

public class StartFragment extends Fragment {
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		
		View view=inflater.inflate(android.R.layout.simple_list_item_1,	null);
		return	view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		TextView text=(TextView) view.findViewById(android.R.id.text1);
		text.setText("动态界面");
		text.setBackgroundColor(Color.RED);
	}

}

xml布局:

<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.example.fragmentapp.MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:gravity="center"
        android:textSize="24sp"
        android:padding="10dip"
        android:layout_weight="1"
        android:text="标题" />
    
   <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="8" />
    
    <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="25sp"
            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="25sp"
            android:text="联系人" />
        
        <TextView
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="25sp"
            android:text="动态" />
      
    </LinearLayout>

</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值