java - fragment 和底部NavigationView

标签 java android android-fragments

enter image description here我对 android studio 和 java 完全陌生。 我为我的应用程序创建了一个 BottomNavigation,并使用了 3 个 fragment ,它完全正常并且工作正常。 但现在我想在我的 fragment 之一上添加一个搜索栏并单击监听器,但我无法做到这一点。 我的问题是我必须在哪个生命周期 View 或函数中添加此搜索栏单击监听器? 我想添加此代码作为搜索栏监听器

  seekBar.findViewById(R.id.seekBar);
    textView.findViewById(R.id.textView3);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            textView.setText(progress+"/100");
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });





**View**
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/f2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SecondFragment">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:orientation="vertical"
            android:padding="10dp"
            android:gravity="center"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/diary_pgae_sleep" />
            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="0/24" />

            <SeekBar
                android:id="@+id/seekBar"
                style="@style/AppTheme"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:layout_weight="1"
                android:max="24"
                android:progress="0"
                android:progressDrawable="@drawable/track1"
                android:thumb="@drawable/thumb"
                android:thumbTint="@color/colorPrimaryDark"
                android:indeterminate="false"/>
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>


**Code**

    package com.example.myapplication;

    import android.graphics.Color;
    import android.graphics.PorterDuff;
    import android.os.Bundle;

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

    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.SeekBar;
    import android.widget.TextView;


    /**
     * A simple {@link Fragment} subclass.
     * Use the {@link SecondFragment#newInstance} factory method to
     * create an instance of this fragment.
     */
    public class SecondFragment extends Fragment {

        SeekBar seekBar;
        TextView textView;

        // TODO: Rename parameter arguments, choose names that match
        // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
        private static final String ARG_PARAM1 = "param1";
        private static final String ARG_PARAM2 = "param2";

        // TODO: Rename and change types of parameters
        private String mParam1;
        private String mParam2;

        public SecondFragment() {
            // Required empty public constructor
        }

        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment SecondFragment.
         */
        // TODO: Rename and change types and number of parameters
        public static SecondFragment newInstance(String param1, String param2) {
            SecondFragment fragment = new SecondFragment();
            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) {

            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_second, container, false);
        }


    }

最佳答案

为此,您实际上需要单独的 java 文件,这些文件在类声明中扩展“Fragment”。这是一个非常好的 YouTube 视频:

https://www.youtube.com/watch?v=bNpWGI_hGGg

希望这有帮助。

关于java - fragment 和底部NavigationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62469094/

相关文章:

java - 想要编写简单的收银机应用程序,在我的头上?

java - 为什么 JList 选择会出现两次?

java - JSlider Thumb 上方的标签

android - context 在 android 2.1 中出现异常

Android-findViewById(int)方法未定义类型优先(Fragment)

android - Activity.isFinishing() 的 fragment 等价物是什么?

java - 空字符串末尾有 "\u0000"

android - 两个 Viewpager 和一个选项卡布局的滑动问题

java - 加载android studio时出错,无法加载类 'org.bouncycaSTLe.jce.provider.BouncyCaSTLeProvider'

java - 使用适配器中的不同数据更新不同 ListView 中的常见帖子