如何在片段中为工具栏添加功能



我知道以前也有人问过类似的问题,但由于某种原因,尽管我尝试了多种解决方案,但还是无法解决。

基本上,我在fragments XML文件中创建了一个工具栏,并将其所有组件放入items菜单文件(bottom_navigation_menu(中。然而,当我点击菜单中的项目时,它们没有得到任何响应。我试着添加一个log.d,看看是否调用了setnavigationonclick函数,但它没有。我在下面附上了我的代码。

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

public class pneumothorax_fr_flashcard_view extends Fragment {
private static final String TAG = "flashcard view";
private pneumothorax_layout_flashcard_view customView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pneumothorax_flashcard_view,container,false);
customView = (pneumothorax_layout_flashcard_view) view.findViewById(R.id.flashcardView);
Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbarBot);
((AppCompatActivity) getActivity()).getSupportActionBar();

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customView.swapColor();
}
});

return view;
}

}

这是XML,在这个文件中,我有一个工具栏和一个自定义视图,其中有一个绿色框,当执行onclick时,该框的颜色会改变。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/toolbarBot"
app:menu="@menu/bottom_navigation_menu">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</RelativeLayout>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="wrap_content">
<com.example.spidermed.pneumothorax_layout_flashcard_view
android:id="@+id/flashcardView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>


</RelativeLayout>`

您必须使用setOnMenuItemClickListener来处理菜单项。setNavigationOnClickListener仅用于左侧的导航按钮(仅当您调用setNavigationIcon()为其设置图标时才会显示(。

用户IanHannibalake发布了正确答案,这是我更新的代码


public class pneumothorax_fr_flashcard_view extends Fragment {
private static final String TAG = "flashcard view";
private pneumothorax_layout_flashcard_view customView;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pneumothorax_flashcard_view,container,false);
customView = (pneumothorax_layout_flashcard_view) view.findViewById(R.id.flashcardView);
Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbarBot);
((AppCompatActivity) getActivity()).getSupportActionBar();
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.forwardButton:
customView.swapColor();
Log.d(TAG,"this is working");
default:
break;
}
return false;
}
});

return view;
}
}

最新更新