Android之如何實現滑動頁面切換【Fragment】
阿新 • • 發佈:2019-02-06
Fragment 頁面切換不能滑動 所以對於listview 可以新增的左右滑動事件 ,不會有衝突例如(QQ的好友列表的刪除)
Fragment 和viewpager 的區別Viewpager 的事件都需要寫在 MainActivity 使得 MainActivity 類非常冗餘
Fragment 內部的事件則可以由其內部去處理分成多個類。便於維護和管理 MainActivity 只起到一個排程的作用
這裡先用Fragment實現非滑動頁面切換,瞭解原理後我會在另一篇文章使用Fragment+viewPager加上滑動效果
第一步:建立Android工程FragmentPage
這時會自動生成一個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);
}
}
}