KnightBubble / android-notes

android-notes
0 stars 1 forks source link

动态添加fragment #4

Open KnightBubble opened 9 years ago

KnightBubble commented 9 years ago
FragmentManager fragmentManager=getFragmentManager();
        //2.获取操作fragment的事务对象并且开始事务
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        //3.调用add()方法动态添加fragment   将MyFragment1对象放到id为linear的控件中
        //add(表示fragment所在的位置的资源id,需要存放的fragment对象)
        transaction.add(R.id.linear, new MyFragment1());
        transaction.add(R.id.linear2, new MyFragment2());
        //4.提交事务
        transaction.commit();
KnightBubble commented 9 years ago

使用v4包中创建fragment

package com.qianfeng.createfragmentv4;

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.view.Menu;
/**
 * v4包中的fragment的特点
 * 1.需要创建子类集成 android.support.v4.app.Fragment;
 * 2.需要引入fragmentactivity需要继承FragmentActivity
 * 3.创建获取FragmentManager对象时调用getSupportFragmentManager()
 */
public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.layout,new MyFragment());
        //remove(需要移除的fragment对象)
        //transaction.remove(new MyFragment());
        //replace(表示替换fragmnet所在的布局控件id,替换新的fragment对象)替换  先移除之前的fragment然后添加新的fragment
        transaction.replace(R.id.layout, new Fragment());
        transaction.commit();
    }

}```
KnightBubble commented 9 years ago

使用动态方式tab切换的内容demo

@Override
    public void onClick(View v) {
        transaction=fragmentManager.beginTransaction();
        switch (v.getId()) {
        case R.id.tv1:
            transaction.replace(R.id.layout, new Fragment1());
            break;
        case R.id.tv2:
            transaction.replace(R.id.layout, new Fragment2());
            break;
        case R.id.tv3:
            transaction.replace(R.id.layout, new Fragment3());
            break;
        }
        transaction.commit();
    }

fragment 管理器,动态方式

fragmentManager=getFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.layout, new Fragment1());
        transaction.commit();
KnightBubble commented 9 years ago

完整的demo 主activity的实现

package com.example.tabfragment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private TextView tab1,tab2,tab3;
    private FragmentTransaction transaction = null;
    FragmentManager fragmentManager = null;

    @SuppressLint({ "NewApi", "CommitTransaction" })
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tab1 = (TextView) findViewById(R.id.tab_1);
        tab2 = (TextView) findViewById(R.id.tab_2);
        tab3 = (TextView) findViewById(R.id.tab_3);

        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);

        fragmentManager = getFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.tab_content, new myFragment1());
        transaction.commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
         transaction = fragmentManager.beginTransaction();
        switch(v.getId()){
          case R.id.tab_1:
              transaction.replace(R.id.tab_content,new myFragment1());
              break;
          case R.id.tab_2:
              transaction.replace(R.id.tab_content,new myFragment2());
              break;
          case R.id.tab_3:
              transaction.replace(R.id.tab_content,new myFragment3());
              break;
         default:
             break;
        }
        transaction.commit();
    }

}
KnightBubble commented 9 years ago

完整的demo 主activity的xml 实现

<LinearLayout 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="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tab_wraper"
        android:orientation="horizontal"
        >
        <TextView
            android:id="@+id/tab_1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="tab_1" />

         <TextView
            android:id="@+id/tab_2"
             android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="tab_2" />

          <TextView
            android:id="@+id/tab_3"
             android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="tab_3" />
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:id="@+id/tab_content"
        >

    </LinearLayout>

</LinearLayout>