Fragment

注解 fragment 全部是在V4包下面的。

fragment replace和add的区别

把fragment看做一个容器,replace就是去替换掉容器内的东西,而add却是不断的往容器内添加东西。

activity_main.xml
<RelativeLayout 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"  tools:context=".MainActivity">
    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></FrameLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center">
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fragment1"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fragment2"/>
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fragment3"/>
    </LinearLayout>


</RelativeLayout>
activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="碎片测试"/>

</LinearLayout>
activity_fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="碎片"/>

</LinearLayout>
activity_fragment3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<EditText
    android:id="@+id/ediText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="测试Fragment"/>
</LinearLayout>

package

fragment
MyFragment
public class MyFragment extends Fragment {
    private Button mButton;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_fragment,null);
        mButton= (Button) view.findViewById(R.id.button_fragment);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "我点击了", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}
MySecondFragment
public class MySecondFragment extends Fragment {
    private TextView mTextView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_fragment2,null);
        mTextView= (TextView) view.findViewById(R.id.textview);
        return view;
    }
    public void setText(String text){
        if(mTextView!=null){
            mTextView.setText(text);
        }
    }
}
MyThirdFrament
public class MyThridFragment  extends Fragment{
    private EditText mEditText;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_fragment3,null);
        mEditText= (EditText) view.findViewById(R.id.ediText);
        return view;
    }
    public String getText(){
        String s=null;
        if(mEditText!=null){
           s= mEditText.getText().toString();
        }
        return s;
    }
}

MainActivity

ublic class MainActivity extends FragmentActivity implements View.OnClickListener{
    private FrameLayout mFrameLayout;
    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private MyFragment myFragment;
    private MySecondFragment mySecondFragment;
    private MyThridFragment myThridFragment;
    private FragmentManager mFragmentManager;
    private  FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFrameLayout= (FrameLayout) findViewById(R.id.framelayout);
        mButton1= (Button) findViewById(R.id.button1);
        mButton2= (Button) findViewById(R.id.button2);
        mButton3= (Button) findViewById(R.id.button3);
        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);
        mButton3.setOnClickListener(this);
        mFragmentManager=getSupportFragmentManager();
        myFragment=new MyFragment();
        mySecondFragment=new MySecondFragment();
        myThridFragment=new MyThridFragment();
        transaction=mFragmentManager.beginTransaction();
        transaction.add(R.id.framelayout,myFragment );
        transaction.add(R.id.framelayout,mySecondFragment);
        transaction.add(R.id.framelayout,myThridFragment);
        transaction.hide(myFragment);
        transaction.hide(mySecondFragment);
        transaction.hide(myThridFragment);
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button1:
                transaction=mFragmentManager.beginTransaction();
                transaction.show(myFragment);
                transaction.hide(mySecondFragment);
                transaction.hide(myThridFragment);
                transaction.commit();
            break;
            case R.id.button2:
                transaction=mFragmentManager.beginTransaction();
                transaction.hide(myFragment);
                transaction.show(mySecondFragment);
                String text=myThridFragment.getText().toString();
                mySecondFragment.setText(text);
                transaction.hide(myThridFragment);
                transaction.commit();
            break;
            case R.id.button3:
                transaction=mFragmentManager.beginTransaction();
                transaction.hide(myFragment);
                transaction.hide(mySecondFragment);
                transaction.show(myThridFragment);

                transaction.commit();
            break;
            default:
                break;
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值