玩转Fragment。。。

OneFragment

public class OneFragment extends Fragment {
    private View view;
    private Context mContext;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_main4, container, false);
        return view;
    }
    public static OneFragment newInstance(Context context) {
        OneFragment fragment = new OneFragment();
        fragment.mContext = context;
        return fragment;
    }
    public OneFragment(){}


}

TwoFragment

public class TwoFragment extends Fragment {
    private View view;
    private Context mContext;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.two_fragment, container, false);

        return view;
    }
    public static TwoFragment newInstance(Context context) {
        TwoFragment fragment = new TwoFragment();
        fragment.mContext = context;
        return fragment;
    }
        public TwoFragment(){}

}

OneFragment.xml

<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"
    android:id="@+id/one"
  >

    <include
        android:id="@+id/ssss"
        layout="@layout/action_bar_home" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F7F3F7"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="1dp"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="134dp">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@mipmap/tool_tool" />

                <RelativeLayout
                    android:id="@+id/search"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/standard_83px"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="@dimen/standard_20px"
                    android:layout_marginLeft="@dimen/standard_83px"
                    android:layout_marginRight="@dimen/standard_83px"
                    android:alpha="0.7"
                    android:background="@drawable/linearlayout_radius">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="15dp"
                        android:drawableRight="@mipmap/laws_home_search_icon"
                        android:text="法规" />

                    <EditText
                        android:id="@+id/edit_word"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="70dp"
                        android:background="@null"
                        android:hint="请输入关键词" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="25dp"
                        android:background="@mipmap/find" />
                </RelativeLayout>

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_margin="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:orientation="vertical"
                    android:padding="@dimen/standard_50px">

                    <ImageView
                        android:id="@+id/home1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@mipmap/home_law_icon" />

                    <TextView
                        android:id="@+id/txt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="20dp"
                        android:text="法律法规全库" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:orientation="vertical"
                   android:layout_marginTop="10dp"
                    android:layout_marginRight="10dp"
                    android:padding="@dimen/standard_50px">

                    <ImageView
                        android:id="@+id/home2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                       android:layout_gravity="center"
                        android:background="@mipmap/home_judge_icon" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="20dp"
                        android:text="审批规则精选库" />

                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_margin="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:orientation="vertical"
                    android:padding="@dimen/standard_50px">

                    <ImageView
                        android:id="@+id/home3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@mipmap/home_guidecase_icon" />

                    <TextView
                        android:id="@+id/txt2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="20dp"
                        android:text="指导性案例全库"
                        android:maxLines="1"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:orientation="vertical"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="10dp"
                    android:padding="@dimen/standard_50px">

                    <ImageView
                        android:id="@+id/home4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@mipmap/home_instrument_icon" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="20dp"
                        android:text="法律文书全库" />

                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_margin="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:orientation="vertical"
                    android:padding="@dimen/standard_50px">

                    <ImageView
                        android:id="@+id/home6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@mipmap/home_contract_icon" />

                    <TextView
                        android:id="@+id/txt3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="20dp"
                        android:text="合同范本全库"
                        android:maxLines="1"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:orientation="vertical"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="10dp"
                    android:padding="@dimen/standard_50px">

                    <ImageView
                        android:id="@+id/home7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@mipmap/home_tool_icon" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="20dp"
                        android:text="法律工具库" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

TwoFragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F2F2F2"
    android:id="@+id/two">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第二个Fragment"
        android:layout_gravity="center"
        />

</LinearLayout>

MainFragment

public class Main5Activity extends FragmentActivity implements View.OnClickListener {
    private Button button1;
    private Button button2;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private FragmentManager manager;
    private int State=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.father_layout);
        manager=getSupportFragmentManager();
        initview();
        State = getIntent().getIntExtra("State", 0);
        if (State == 2)
        {
            getSelect(3);
        }
        else
        {
            getSelect(1);
        }
    }

    private void getSelect(int i){
      FragmentTransaction transaction=manager.beginTransaction();
      hideFragment(transaction);
        if(i==1){
            if(oneFragment==null)
            {
                oneFragment = OneFragment.newInstance(this);
                transaction.add(R.id.father_f ,oneFragment);
            }
            else
            {
                transaction.show(oneFragment);
            }
        }
        if (i==2){
            if(twoFragment==null){
                twoFragment=TwoFragment.newInstance(this);
                transaction.add(R.id.father_f ,twoFragment);
            }
            else {
                transaction.show(twoFragment);
            }
            }
        transaction.commit();
        }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    }
    private void initview(){
        button1=(Button)findViewById(R.id.but1);
        button2=(Button)findViewById(R.id.but2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }
    public void hideFragment(FragmentTransaction transaction)
    {
        if(oneFragment!=null)
        {
            transaction.hide(oneFragment);
        }
        if(twoFragment!=null)
        {
            transaction.hide(twoFragment);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but1:
                getSelect(1);
                break;
            case R.id.but2:
                getSelect(2);
                break;
        }
    }

MainFragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <FrameLayout
            android:id="@+id/father_f"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        </FrameLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:orientation="horizontal">
            <Button
                android:background="@drawable/button_radius_blue"
                android:id="@+id/but1"
                android:textColor="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Fragment1"
                android:layout_weight="1"/>
            <Button
                android:id="@+id/but2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Fragment2"
                android:background="@drawable/button_radius_yellow"
                android:textColor="@color/white"
                android:layout_weight="1"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

这里写图片描述
这里写图片描述

最后一定记得导包,并且得导一样的包!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值