gpt4 book ai didi

java - fragment 和底部NavigationView

转载 作者:行者123 更新时间:2023-12-01 16:13:31 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com