fragment 的简单用法

来源:互联网 发布:oracle数据库日志清理 编辑:程序博客网 时间:2024/06/04 18:48

1.left_fragment.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:orientation="vertical"              android:background="#00fff0">    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="button"/></LinearLayout>
2..right_fragment.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:orientation="vertical"             android:background="#00ff00">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="This is right fragment"                android:textSize="20sp"                android:layout_gravity="center_horizontal"/></LinearLayout>
3. LeftFragment.class  新建一个类

  

public class LeftFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    {        View view = inflater.inflate(R.layout.left_fragment,container,false);        return view;    }}

 4.RightFragment.class  新建一个类

<?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"             android:background="#00ff00">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="This is right fragment"                android:textSize="20sp"                android:layout_gravity="center_horizontal"/></LinearLayout>

5.activity_main.xml 主布局文件 

<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:id="@+id/activity_main"    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.sh.fragmenttest.MainActivity">     <fragment        android:id="@+id/left_fragment"        android:name="com.sh.fragmenttest.LeftFragment"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"/>    <FrameLayout        android:id="@+id/right_fragment"        android:name="com.sh.fragmenttest.RightFragment"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1">    </FrameLayout></LinearLayout>

6.another_right_fragment.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:orientation="vertical"    android:background="#ff0000">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="This is another right fragment"        android:textSize="20sp"        android:layout_gravity="center_horizontal"/></LinearLayout>
7.AnotherRightFragment.class  新建一个类

public class AnotherRightFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         View view = inflater.inflate(R.layout.another_right_fragment,container,false);        return view;    }}
8.MainAcivity.class 主文件 

public class MainActivity extends AppCompatActivity implements View.OnClickListener{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button button = (Button)findViewById(R.id.button);        button.setOnClickListener(this);        replaceFragment(new RightFragment());    }    @Override    public void onClick(View v) {           switch (v.getId())           {               case  R.id.button:                     replaceFragment(new AnotherRightFragment());               default:                   break;           }    }    private   void replaceFragment(Fragment fragment)    {        FragmentManager fragmentManager = getSupportFragmentManager();        FragmentTransaction transaction = fragmentManager.beginTransaction();        transaction.replace(R.id.right_fragment,fragment);
        transaction.addToBackStack(null);
transaction.commit(); }}