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);
}
}
}
