Android fragment切换的demo

1.文件准备:

①MainActivity.java activity_layout.xml;

②3-4个Fragmentxx.java   fragmentxx.xml;

③一个颜色资源文件,用于按钮切换的展示。

④style文件,同一管理按钮的样式。

2.MainActivity

public class MainActivity extends AppCompatActivity {

    private TextView marketBar;
    private TextView hatBar;
    private TextView messageBar;
    private TextView centerBar;

    Fragment1 giftFragment;
    Fragment2 hatFragment;
    Fragment3 messageFragment;
    Fragment4 centerFragment;

    FragmentManager fragmentManager;

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

        marketBar = (TextView) findViewById(R.id.bar_market);
        hatBar = (TextView) findViewById(R.id.bar_hat);
        messageBar = (TextView) findViewById(R.id.bar_message);
        centerBar = (TextView) findViewById(R.id.bar_center);

        marketBar.setClickable(true);
        hatBar.setClickable(true);
        messageBar.setClickable(true);
        centerBar.setClickable(true);

        setAllNotSelected();
        marketBar.setSelected(true);

        giftFragment = new Fragment1();
        hatFragment = new Fragment2();
        messageFragment = new Fragment3();
        centerFragment = new Fragment4();

        fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.main_fragment, giftFragment).commit();

    }


    private void setAllNotSelected() {
        marketBar.setSelected(false);
        hatBar.setSelected(false);
        messageBar.setSelected(false);
        centerBar.setSelected(false);
    }

    public void tabSelect(View v) {
        setAllNotSelected();
        switch (v.getId()) {
            case R.id.bar_market:
                marketBar.setSelected(true);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, giftFragment).commit();
                break;
            case R.id.bar_hat:
                hatBar.setSelected(true);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, hatFragment).commit();
                break;
            case R.id.bar_message:
                messageBar.setSelected(true);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, messageFragment).commit();
                break;
            case R.id.bar_center:
                centerBar.setSelected(true);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, centerFragment).commit();
                break;
            default:
                break;
        }
    }

}

3.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eeeeee">


    <LinearLayout
        android:id="@+id/menu_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="#ddd"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/bar_market"
            style="@style/MainBar"
            android:onClick="tabSelect"
            android:text="选项1" />

        <TextView
            android:id="@+id/bar_hat"
            style="@style/MainBar"
            android:onClick="tabSelect"
            android:text="选项2" />

        <TextView
            android:id="@+id/bar_message"
            style="@style/MainBar"
            android:onClick="tabSelect"
            android:text="选项3" />

        <TextView
            android:id="@+id/bar_center"
            style="@style/MainBar"
            android:onClick="tabSelect"
            android:text="选项4" />

    </LinearLayout>


    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/menu_bar"></FrameLayout>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ddd" />


</RelativeLayout>

4.其中一个Fragment举例

public class Fragment1 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment1,container,false);
        return v;
    }
}

5.style文件

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="MainBar" >
        <item name="android:layout_weight">1</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:textSize">20dp</item>
        <item name="android:textColor">@color/tab_color_selector</item>
        <item name="android:gravity">center</item>
    </style>

</resources>
6.color文件  tab_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#118bfc" android:state_selected="true"/>
    <item android:color="#333" />
</selector>

猜你喜欢

转载自blog.csdn.net/crab0314/article/details/79725241