Fragment的切换

//主布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.zm.yuetwo.MainActivity">
 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="页面一"
            android:gravity="center"
            android:textSize="30sp"
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
        <TextView
            android:text="页面二"
            android:gravity="center"
            android:textSize="30sp"
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
    </LinearLayout>
    <FrameLayout
        android:id="@+id/conntent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ></FrameLayout>
</LinearLayout>

//代码操作的切换

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private FrameLayout conntent;
private Fragment01 fragment01;
private Fragment02 fragment02;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }
    //初始化界面
    private void initViews() {
        TextView one=findViewById(R.id.one);
        TextView two=findViewById(R.id.two);
      conntent=findViewById(R.id.conntent);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        fragment01=new Fragment01();
        fragment02=new Fragment02();

        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.conntent,fragment01);
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
     switch (v.getId()){
         case R.id.one:
             FragmentManager fragmentManager=getSupportFragmentManager();
             FragmentTransaction transaction=fragmentManager.beginTransaction();
             transaction.replace(R.id.conntent,fragment01);
             transaction.commit();
             break;
         case R.id.two:
             FragmentManager fragmentManager2=getSupportFragmentManager();
             FragmentTransaction transaction2=fragmentManager2.beginTransaction();
             transaction2.replace(R.id.conntent,fragment02);
             transaction2.commit();
             break;
     }
    }
}

View view=inflater.inflate(R.layout.fragment01,container,false);
return view;

猜你喜欢

转载自blog.csdn.net/air_show/article/details/80491270