Fragment的使用

FragmeLayout的使用

xml布局

























Activity内展示fragment


 
public class MainActivity extends FragmentActivity {
 
 
    private FrameLayout zhan;
    private RadioGroup gr;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        initView();
        //设置初始fragment页面
        getSupportFragmentManager().beginTransaction().
                replace(R.id.zhan,new Fragment1()).commit();
 
        gr.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.shouye:
                        getSupportFragmentManager().beginTransaction().
                                replace(R.id.zhan,new Fragment1()).commit();
                        break;
                    case R.id.xigua:
                        getSupportFragmentManager().beginTransaction().
                                replace(R.id.zhan,new Fragment2()).commit();
                        break;
                    case R.id.wei:
                        getSupportFragmentManager().beginTransaction().
                                replace(R.id.zhan,new Fragment3()).commit();
                        break;
                    case R.id.wode:
                        getSupportFragmentManager().beginTransaction().
                                replace(R.id.zhan,new Fragment4()).commit();
                        break;
                }
            }
        });
    }
 
    private void initView() {
        zhan = findViewById(R.id.zhan);
        gr = findViewById(R.id.gr);
    }
 
}

fragment的显示与隐藏

public class MainActivity extends FragmentActivity {
 
    private Fragment2 fragment2;
    private Fragment1 fragment1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragment2 = new Fragment2();
        fragment1 = new Fragment1();
        // 如果想要做显示和隐藏的操作,,,必须在Fragment的栈里面添加Fragment
        getSupportFragmentManager().beginTransaction()
                .add(R.id.framelayout, fragment1).commit();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.framelayout, fragment2).commit();
    }
 
    /**
     * 显示Fragment1
     */
    public void show(View view) {
        // 创建Fragment管理者,,,并开启事务
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.show(fragment1);
        transaction.hide(fragment2);
        transaction.commit();
    }
 
    /**
     * 隐藏Fragment1
     */
    public void hide(View view) {
        // 创建Fragment管理者,,,并开启事务
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.hide(fragment1);
        transaction.show(fragment2);
        transaction.commit();
    }
 
}


fragment传值



            Bundle b=new Bundle();
            String urls = myTabs.get(position).getUrls();
            b.putString("key",urls);
            b.putString("pageIndex","1");
            v_fragment v_fragment=new v_fragment();
            v_fragment.setArguments(b);
 
fragment接收值

Bundle  bundle = getArguments();
        if (bundle !=null){
              key_url = bundle.getString("key");
           
        }














<RelativeLayout 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"
    tools:context="com.example.wangwenda20171201_week1.MainActivity">
 
    <FrameLayout
        android:id="@+id/zhan"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@id/gr">
 
    </FrameLayout>
 
    <RadioGroup
        android:id="@+id/gr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
 
        <RadioButton
            android:id="@+id/shouye"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="URLconnection" />
 
        <RadioButton
            android:id="@+id/xigua"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="HttpClient" />
 
        <RadioButton
            android:id="@+id/wei"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="WIFI状态" />
 
        <RadioButton
            android:id="@+id/wode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="蓝牙状态" />
 
    </RadioGroup>
 
</RelativeLayout>




            Bundle b=new Bundle();
            String urls = myTabs.get(position).getUrls();
            b.putString("key",urls);
            b.putString("pageIndex","1");
            v_fragment v_fragment=new v_fragment();
            v_fragment.setArguments(b);
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值