import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
LinearLayout layout;
FragmentManager fg;
FragmentTransaction ft;
TextView tv1,tv2,tv3,tv4;
Fragement1 f1 = new Fragement1();
Fragement2 f2 = new Fragement2();
Fragement3 f3 = new Fragement3();
Fragement4 f4 = new Fragement4();
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout)findViewById(R.id.page2);
tv1 = (TextView)findViewById(R.id.tab1);
tv2 = (TextView)findViewById(R.id.tab2);
tv3 = (TextView)findViewById(R.id.tab3);
tv4 = (TextView)findViewById(R.id.tab4);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
tv4.setOnClickListener(this);
fg = getFragmentManager();//此处用于设置首页和获取权限
ft = fg.beginTransaction();
ft.replace(R.id.page2, f1);
ft.commit();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onClick(View arg0) {
// TODO Auto-generated method stub
ft = fg.beginTransaction();
switch (arg0.getId()) {
case R.id.tab1:
ft.replace(R.id.page2, f1);
break;
case R.id.tab2:
ft.replace(R.id.page2, f2);
break;
case R.id.tab3:
ft.replace(R.id.page2, f3);
break;
case R.id.tab4:
ft.replace(R.id.page2, f4);
break;
default:
break;
}
ft.commit();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="0dp"
android:id="@+id/page1"
android:layout_weight="1"
android:background="#FFAEB9"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:text="页面1" />
<TextView
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="页面2" />
<TextView
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="页面3" />
<TextView
android:id="@+id/tab4"
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="页面4" />
</LinearLayout>
<LinearLayout
android:id="@+id/page2"
android:layout_width="wrap_content"
android:background="#FFE4B5"
android:layout_weight="4"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
实现页面监听和跳转步骤:
1.先复写几个继承Fragment的类并关联相应的布局
2.在主布局中添加几个相应的组件(通过这些组件进行监听)在本布局中写一个对应的LinearLayout用于盛放fragment;
3.接下来就完成主类了:
在MainActivity中
fg = getFragmentManager();//此处用于设置首页和获取权限
ft = fg.beginTransaction();
ft.replace(R.id.page2, f1);
ft.commit();
pager2对应的是不及文件中的linearLayout用于盛放fragment
让MainActivity继承OnClickListener实现监听