android实现fargment

效果图

 

 

 MainLayout文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <!-- TODO: Update blank fragment layout -->
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:titleTextColor="@color/black"
        app:title="@string/app_name"
        android:background="#FAFAFA">

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

            <ImageView
                android:background="@drawable/setting"
                android:layout_margin="18dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </androidx.appcompat.widget.Toolbar>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#DADADA" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frg_layout"
        android:layout_weight="9" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#DADADA" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/group"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_weight="0">

        <RadioButton
            android:id="@+id/discover"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            style="@style/fragment"
            android:drawableTop="@drawable/search_selector"
            android:text="发现" />

        <RadioButton
            android:id="@+id/classify"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            style="@style/fragment"
            android:drawableTop="@drawable/classify_slector"
            android:text="分类" />

        <RadioButton
            android:id="@+id/user"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            style="@style/fragment"
            android:drawableTop="@drawable/user_selector"
            android:text="我的" />
    </RadioGroup>

    <View
        android:layout_width="match_parent"
        android:layout_height="8.5dp" />
</LinearLayout>

剩下三个就是普通布局内容自己写

 

 

 MainActivity文件

public class MainActivity extends AppCompatActivity {

    private View find, mine;
    private View classify;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Window window = MainActivity.this.getWindow();
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏文字颜色为暗色
        window.setStatusBarColor(Color.parseColor("#FAFAFA"));

        find = findViewById(R.id.discover);
        classify = findViewById(R.id.classify);
        mine = findViewById(R.id.user);
        change(new Search());
        find.setSelected(true);
        //mine.setSelected(false);
        //classify.setSelected(false);


        find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change(new Search());
                find.setSelected(true);
                mine.setSelected(false);
                classify.setSelected(false);
            }
        });

        classify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change(new Classify());
                find.setSelected(false);
                mine.setSelected(false);
                classify.setSelected(true);
            }
        });
        mine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change(new Mine());
                find.setSelected(false);
                classify.setSelected(false);
                mine.setSelected(true);
            }
        });

    }


    private void change(Fragment fragment) {
        FragmentManager FRAGMENT_MANAGER = getSupportFragmentManager();
        FragmentTransaction TRANSACTION = FRAGMENT_MANAGER.beginTransaction();
        TRANSACTION.replace(R.id.frg_layout, fragment);
        TRANSACTION.commit();
    }


}

后面三个class文件

public class Mine extends Fragment {
    private View root;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (root == null) {
            root = inflater.inflate(R.layout.mine, container, false);
        }

        return root;

    }


}
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import demo.fargment.MainActivity;
import demo.fargment.R;

public class Classify extends Fragment implements View.OnTouchListener {
    private View root;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (root == null) {
            root = inflater.inflate(R.layout.classify, container, false);
            Button btn = root.findViewById(R.id.btn);
            //btn.setOnClickListener((View.OnClickListener) this);
            btn.setOnTouchListener(this);
        }
        return root;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        /*按下操作*/
        ScaleAnimation animation;
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            animation = new ScaleAnimation(1.0f, 0.97f, 1.0f, 0.97f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(100);
            animation.setFillAfter(true);
            view.startAnimation(animation);
        }
        /*抬起操作*/
        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            animation = new ScaleAnimation(0.97f, 1.0f, 0.97f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(100);
            animation.setFillAfter(true);
            view.startAnimation(animation);
        }
        return false;
    }

}
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import demo.fargment.R;


public class Search extends Fragment {
    private View root;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (root == null) {
            root = inflater.inflate(R.layout.search, container, false);
        }

        return root;
    }
}

menu文件夹中创建menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_setting"
        android:orderInCategory="100"
        android:icon="@drawable/setting"
        android:title="setting" />
</menu>

GitHub地址 下载前给star

 

posted @ 2022-10-25 09:42  Z_Chan  阅读(40)  评论(0编辑  收藏  举报