Android之怎样实现滑动页面切换【Fragment】


Fragment 页面切换不能滑动 所以对于listview 能够加入的左右滑动事件 。不会有冲突比如(QQ的好友列表的删除) 

Fragment 和viewpager 的差别 
Viewpager 的事件都须要写在 MainActivity 使得 MainActivity 类很冗余 
Fragment 内部的事件则能够由其内部去处理分成多个类。

便于维护和管理 MainActivity 仅仅起到一个调度的作用 
这里先用Fragment实现非滑动页面切换,了解原理后我会在还有一篇文章使用Fragment+viewPager加上滑动效果



第一步:创建AndroidprojectFragmentPage
这时会自己主动生成一个MainActivity.java
和一个相应的activity_main.xml文件
在layout目录下创建四个xml文件:


tab1.xml

<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/imag1" android:orientation="vertical" > </LinearLayout>




tab2.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/image2"
    android:orientation="vertical" >


</LinearLayout>





tab3.xml

<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/image3" android:orientation="vertical" > </LinearLayout>





bottom.xml


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


    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="标题1"
        android:textColor="#000000"
        android:textSize="18.0dip" />


    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="标题2"
        android:textColor="#000000"
        android:textSize="18.0dip" />


    <TextView
        android:id="@+id/text3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="标题3"
        android:textColor="#000000"
        android:textSize="18.0dip" />


</LinearLayout>



在src下的自己主动生成的包下创建三个java文件:


Fragment1
注意:导入包时一定要导入:import android.support.v4.app.FragmentActivity;


这个包。

这是Android4.0之后才支持的版本号
4.0之前不支持这个包(有另外一个专门的包)


package com.example.fragmentpage;


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




public class Fragment1 extends Fragment {
	private View tab1view;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		tab1view=inflater.inflate(R.layout.tab1, container, false);	
		return tab1view;
	}


}







Fragment2


package com.example.fragmentpage;


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


public class Fragment2 extends Fragment {


	private View tabview1;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		tabview1 = inflater.inflate(R.layout.tab2, container,false);
		return tabview1;
	}


}






Fragment3




package com.example.fragmentpage;


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


public class Fragment3 extends Fragment{
	private View tab3view ;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		tab3view = inflater.inflate(R.layout.tab3, container, false);
		return tab3view;
	}


}




第四步:


在MainActivity中写入


package com.example.fragmentpage;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.method.HideReturnsTransformationMethod;
import android.view.Menu;
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 TextView t1;
	private TextView t2;
	private TextView t3;


	private Fragment tab1;
	private Fragment tab2;
	private Fragment tab3;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initView();//依据Id索引组件
		initEvent();//加入监听
		setSelect(0);//


	}
	private void initEvent() {
		t1.setOnClickListener(this);
		t2.setOnClickListener(this);
		t3.setOnClickListener(this);
	}
	private void initView() {
		t1 = (TextView) findViewById(R.id.text1);
		t2 = (TextView) findViewById(R.id.text2);
		t3 = (TextView) findViewById(R.id.text3);


	}


/*
 * 重置textView的内容
 * */
	private void reset() {
		t1.setText("标题1");
		t2.setText("标题2");
		t3.setText("标题3");
	}
	private void setSelect(int i) {
		// TODO Auto-generated method stub
		FragmentManager fm=getSupportFragmentManager();
		FragmentTransaction trs = fm.beginTransaction();
		//隐藏Fragment
		hideFragment(trs);//使所有隐藏
		switch(i){
		case 0:
			if(tab1 == null){
				tab1 = new Fragment1();//创建Fragment1的对象(一个页面)
				trs.add(R.id.id_content,tab1);
			}
			else {
				trs.show(tab1);//使当前Activity显示tab1即Fragment1页面
			}
			t1.setText("选中");
			break;
		case 1:
			if(tab2 == null){
				tab2 = new Fragment2();
				trs.add(R.id.id_content, tab2);


			}else{
				trs.show(tab2);
			}
			t2.setText("选中");
			break;
		case 2:
			if(tab3 == null){
				tab3 = new Fragment3();
				trs.add(R.id.id_content, tab3);
			}else {
				trs.show(tab3);
			}
			t3.setText("选中");
			break;
		default:
			break;
		}
		trs.commit();


	}
	@Override
	public void onClick(View v) {//监听事件
		// TODO Auto-generated method stub
		reset();
		switch(v.getId()){
		case R.id.text1:
			setSelect(0);
			break;
		case R.id.text2:
			setSelect(1);


			break;
		case R.id.text3:
			setSelect(2);
			break;


		default:
			break;


		}


	}


	private void hideFragment(FragmentTransaction trs) {
		// TODO Auto-generated method stub
		if(tab1!=null){
			trs.hide(tab1);


		}
		if(tab2!=null)
		{
			trs.hide(tab2);
		}
		if(tab3!=null)
		{
			trs.hide(tab3);
		}


	}
}








posted @ 2018-03-15 15:46  zhchoutai  阅读(3312)  评论(0编辑  收藏  举报