把基础的都快忘了 汗
有时候一些需求需要很多的页面 一般的处理是
1.一些页面放到一个activitiy 缺点:业务逻辑和页面处理逻辑会在乱不堪
2.一个页面一个activitiy 缺点:增大activitiy的开销
也是在api 11以上有了fragment
具体做法如下:
在布局中放一个盛放fragment的容器 这里这容器是framlayout
<?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:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="a"android:text="Button" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="b"android:text="Button" /></LinearLayout><FrameLayoutandroid:id="@+id/myframe"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ></FrameLayout></LinearLayout>
然后
在fragmentTransaction处理替换这个View 以达到显示多个页面的目的 而处理逻辑交个FragmentA b c d就OK
package com.example.testfragment;import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;public class MainActivity extends FragmentActivity {private FragmentTransaction fragmentTransaction;private FragmentManager fragmentManager;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fragmentManager = getSupportFragmentManager();}public void a(View view) {AFragment fragment = new AFragment();fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.myframe, fragment);fragmentTransaction.commit();}public void b(View view) {BFragment fragment = new BFragment();fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.myframe, fragment);fragmentTransaction.commit();}}
package com.example.testfragment;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;public class AFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {TextView textView = new TextView(getActivity());textView.setText("Fragment A");return textView;}
}
package com.example.testfragment;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;public class BFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {TextView textView = new TextView(getActivity());textView.setText("Fragment B");return textView;}
}
890.62 KB, 下载次数: 0, 下载积分: e币 -2 元