首页 > 其他分享 >今日共同报告-13-app底部导航栏的实现,fragment传参

今日共同报告-13-app底部导航栏的实现,fragment传参

时间:2023-03-25 21:46:30浏览次数:49  
标签:传参 13 fragment menu app import SearchFragment id android

今日共同报告-13

今天时间比较充沛,学了许多东西,主要是fragment传参,同时把底部导航栏完成了。

目前进度的页面展示:

能够实现点击切换和选中的选项颜色变化

 

 

部分代码:

package com.example.dataapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dataapplication.fragment.SearchFragment;

public class MenuActivity extends AppCompatActivity implements View.OnClickListener{

    private LinearLayout lSearch,lSubway,lMap;
    private ImageView    ivHome,ivFind,ivMine;
    private TextView     tvHome,tvFind,tvMine;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        initView();
        initEvent();
    }

    private void initEvent() {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        SearchFragment fragment = SearchFragment.newInstance("这是搜索页面","");
        fragmentTransaction.replace(R.id.menu_frag,fragment).commit();

        lSearch.setOnClickListener(this);
        lSubway.setOnClickListener(this);
        lMap.setOnClickListener(this);
    }

    private void initView() {
        lSearch = findViewById(R.id.menu_ll_1);
        lSubway = findViewById(R.id.menu_ll_2);
        lMap    = findViewById(R.id.menu_ll_3);

        ivHome = findViewById(R.id.menu_iv_1);
        ivFind = findViewById(R.id.menu_iv_2);
        ivMine = findViewById(R.id.menu_iv_3);

        tvHome = findViewById(R.id.menu_tv_1);
        tvFind = findViewById(R.id.menu_tv_1);
        tvMine = findViewById(R.id.menu_tv_1);
    }
    public void resetButton(){

        ivHome.setSelected(false);
        ivFind.setSelected(false);
        ivMine.setSelected(false);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.menu_ll_1:
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                SearchFragment fragment1 = SearchFragment.newInstance("这是搜索页面","");
                fragmentTransaction.replace(R.id.menu_frag,fragment1).commit();
                resetButton();
                ivHome.setSelected(true);
                break;
            case R.id.menu_ll_2:

                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                SearchFragment fragment2 = SearchFragment.newInstance("这是站点页面","");
                fragmentTransaction.replace(R.id.menu_frag,fragment2).commit();
                resetButton();
                ivFind.setSelected(true);
                break;
            case R.id.menu_ll_3:
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                SearchFragment fragment3 = SearchFragment.newInstance("这是地图页面","");
                fragmentTransaction.replace(R.id.menu_frag,fragment3).commit();
                resetButton();
                ivMine.setSelected(true);
                break;
        }
    }
}

 

package com.example.dataapplication.fragment;

import android.os.Bundle;

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

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.dataapplication.R;


public class SearchFragment extends Fragment {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1,mParam2;
    private TextView tvContent1;
    public SearchFragment() {

    }

    public static SearchFragment newInstance(String param1, String param2) {
        SearchFragment fragment = new SearchFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

        return inflater.inflate(R.layout.fragment_search, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tvContent1 = view.findViewById(R.id.menu_search_frag_1);

        if (!TextUtils.isEmpty(mParam1)){
            tvContent1.setText(mParam1);
        }
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MenuActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/menu_frag"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        tools:layout="@layout/fragment_blank1"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/gray"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal"
        android:layout_marginTop="5dp">

        <LinearLayout
            android:id="@+id/menu_ll_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center_horizontal">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:id="@+id/menu_iv_1"
                android:src="@drawable/selector_search"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/menu_tv_1"
                android:textSize="20sp"
                android:text="查询"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/menu_ll_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center_horizontal">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:id="@+id/menu_iv_2"
                android:src="@drawable/selector_subway"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/menu_tv_2"
                android:textSize="20sp"
                android:text="站点"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/menu_ll_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center_horizontal">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:id="@+id/menu_iv_3"
                android:src="@drawable/selector_map"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/menu_tv_3"
                android:textSize="20sp"
                android:text="地图"/>
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

 

标签:传参,13,fragment,menu,app,import,SearchFragment,id,android
From: https://www.cnblogs.com/rsy-bxf150/p/17255668.html

相关文章