Fragment - 用法+demo

Fragment参考

好的博客,时内容太多:

Fragment理解上           Fragment理解下   

Fragment详解1            Fragment详解2                          

小demo:

需求:

MainActivity作为Fragment的载体,点击下面的首页、理财和账户切换自己的Fragment。

图:


代码:

MainActivity代码:这里使用了FragmentManager的hide和show方法,没有使用replace

package baofu.wallet;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import baofu.wallet.widget.ui.fragment.IndexFragment;
import baofu.wallet.widget.ui.fragment.LiCaiFragment;
import baofu.wallet.widget.ui.fragment.MineFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout ll_index;
    private LinearLayout ll_licai;
    private LinearLayout ll_mine;

    private IndexFragment indexFragment;
    private LiCaiFragment liCaiFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll_index = (LinearLayout) findViewById(R.id.ll_index);
        ll_licai = (LinearLayout) findViewById(R.id.ll_licai);
        ll_mine = (LinearLayout) findViewById(R.id.ll_mine);

        indexFragment = new IndexFragment();
        liCaiFragment = new LiCaiFragment();
        mineFragment = new MineFragment();

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fl_top, indexFragment, "index");
        ft.add(R.id.fl_top, liCaiFragment, "licai");
        ft.add(R.id.fl_top, mineFragment, "mine");
        ft.show(indexFragment);
        ft.commit();

        setClickListener();
    }

    //注册点击监听
    private void setClickListener() {
        ll_index.setOnClickListener(this);
        ll_licai.setOnClickListener(this);
        ll_mine.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        switch (v.getId()) {
            case R.id.ll_index:
                indexFragment = (IndexFragment) fm.findFragmentByTag("index");
                ft.hide(liCaiFragment);
                ft.hide(mineFragment);
                ft.show(indexFragment).commit();
                break;
            case R.id.ll_licai:
                liCaiFragment = (LiCaiFragment) fm.findFragmentByTag("licai");
                ft.hide(indexFragment);
                ft.hide(mineFragment);
                ft.show(liCaiFragment).commit();
                break;
            case R.id.ll_mine:
                mineFragment = (MineFragment) fm.findFragmentByTag("mine");
                ft.hide(indexFragment);
                ft.hide(liCaiFragment);
                ft.show(mineFragment).commit();
                break;
        }
    }
}

activity_main.xml 这里使用FragmentLayout来存放三个Fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_main"
    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:background="@color/colorBody"
    android:orientation="vertical"
    tools:context="baofu.wallet.MainActivity">

    <FrameLayout
        android:id="@+id/fl_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="3dp"
        android:background="@color/colorWhite"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:id="@+id/ll_index"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/ic_shouye_normal"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_gravity="center_horizontal"
                android:text="首页"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_licai"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/ic_hsq_normal"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_gravity="center_horizontal"
                android:text="理财"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_mine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/ic_mine_normal"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_gravity="center_horizontal"
                android:text="账户"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Fragment,这里只列出了MineFragment,其它两个雷同

MineFragment代码:

package baofu.wallet.widget.ui.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import baofu.wallet.R;

/**
 * 我的
 * Created by zst on 2017/3/2.
 */

public class MineFragment extends Fragment {
    private List<HashMap<String, Object>> dataSourceList = new ArrayList<HashMap<String, Object>>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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


        return view;
    }
}

fragment_mine.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    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:background="@color/colorBody"
    tools:context="baofu.wallet.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="我的页面"/>

</RelativeLayout>

简单实现完成。

发布了388 篇原创文章 · 获赞 287 · 访问量 129万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览