学习日记--fragment界面切换测试

效果如下:




一.java代码(创建四个类)

1、MainActivity.java

package com.hxzy.fragment1211;

import java.util.ArrayList;

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

public class MainActivity extends Activity {
	private TextView title;
	private ArrayList<TextView> textViews = new ArrayList<TextView>();
	private ArrayList<Fragment> fragments = new ArrayList<Fragment>();<span style="white-space:pre">
</span><pre name="code" class="java"><span style="white-space:pre">	</span>private<span style="white-space:pre">	</span>int POSITION = -1;
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);title = (TextView) findViewById(R.id.title);fragments.add(0, new FragmentMessage());fragments.add(1, new FragmentContact());fragments.add(2, new FragmentOwns());textViews.add(0, (TextView) findViewById(R.id.message));textViews.add(1, (TextView) findViewById(R.id.contact));textViews.add(2, (TextView) findViewById(R.id.owns));chooseText(0);textSetListener(textViews.get(0), 0);textSetListener(textViews.get(1), 1);textSetListener(textViews.get(2), 2);}private void textSetListener(TextView text, final int pos) {text.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubchooseText(pos);}});}public void chooseText(int pos) {if (POSITION == pos) {// 避免两次点击出现错误return;}for (int i = 0; i < textViews.size(); i++) {if (i == pos) {textViews.get(i).setBackgroundColor(Color.GRAY);textViews.get(i).setTextColor(Color.RED);chooseFragment(fragments.get(i));switch (i) {case 0: {title.setText("信息");break;}case 1: {title.setText("通信录");break;}case 2: {title.setText("我");break;}default:break;}} else {textViews.get(i).setBackgroundColor(Color.WHITE);textViews.get(i).setTextColor(Color.BLACK);}}POSITION = pos;}public void chooseFragment(Fragment f) {FragmentManager fm = getFragmentManager();FragmentTransaction ft = fm.beginTransaction();ft.replace(R.id.content, f);ft.commit();}}
 


2、FragmentMessage.java

package com.hxzy.fragment1211;

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 FragmentMessage 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 tv=(TextView) view.findViewById(android.R.id.text1);
		tv.setText("消息界面");
	
		
	}
}


3、FragmentContact.java

package com.hxzy.fragment1211;


import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;


public class FragmentContact extends ListFragment {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		String []data=new String[50];
		for (int i = 0; i < 50; i++) {
			data[i]="联系人"+i;
		}
		
		ArrayAdapter< String> adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, data);
		this.setListAdapter(adapter);
	}
	
}


4、FragmentOwns.java

package com.hxzy.fragment1211;

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 FragmentOwns 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) {
		super.onViewCreated(view, savedInstanceState);

		TextView tv = (TextView) view.findViewById(android.R.id.text1);
		tv.setText("我的页面");

	}
}


二、layout布局文件

<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.hxzy.fragment1211.MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_light"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="消息"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/contact"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="联系人"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/owns"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="我"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值