0

我刚开始在 Android 中使用 Jetpack Navigation 组件和 SafeArgs 进行导航。但是,我不明白如何从一个目的地导航到另一个目的地(我阅读了谷歌文档,但这让我感到困惑)。所以我有一个 Menu_Fragment 类,它在 NavGraph 中有一个指向 Softdrink 类的链接。自动创建了一个 Menu_FragmentDirections 类。但是,我不知道如何在 Menu_Fragment 类中使用它来实现 OnClick 侦听器事件。在这里,我在 Menu_Fragment 类中有 onClick 方法(请参阅https://developer.android.com/guide/navigation/navigation-navigate):

@Override
public void onClick(View view) {

    if(view.getId() == R.id.imageButton_Softdrinks_en) {
        float amount = ...;
        action =
                Menu_FragmentDirections
                        .actionMenuFragmentToSoftdrinks(amount);
        Navigation.findNavController(view).navigate(action);

    }

}

在这里您可以看到 Menu_FragmentDirections 类:

package com.example.td.barapp;

import androidx.annotation.NonNull;
import androidx.navigation.ActionOnlyNavDirections;
import androidx.navigation.NavDirections;

public class Menu_FragmentDirections {
  private Menu_FragmentDirections() {
  }

  @NonNull
  public static NavDirections actionMenuFragmentToSoftdrinks() {
    return new ActionOnlyNavDirections(R.id.action_menu_Fragment_to_softdrinks);
  }
}

在这里你可以看到 NavGraph:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/menu_Fragment">

    <fragment
        android:id="@+id/menu_Fragment"
        android:name="com.example.td.barapp.Menu_Fragment"
        android:label="fragment_menu_"
        tools:layout="@layout/fragment_menu" >
        <action
            android:id="@+id/action_menu_Fragment_to_softdrinks"
            app:destination="@id/softdrinks" />
    </fragment>
    <fragment
        android:id="@+id/softdrinks"
        android:name="com.example.td.barapp.Softdrinks"
        android:label="fragment_softdrinks"
        tools:layout="@layout/fragment_softdrinks" />
</navigation>

谁能告诉我,我必须在上面发布的 onCreateMethod() 中做什么。我收到错误消息:“无法解析符号‘操作’”和“无法解析符号‘导航’”(当然还有使用错误意外的标记 '...')

我会很感激每一条评论,并非常感谢您的帮助。

这里更新的是带有 onCreateMethod 的完整 Menu_Fragment 类:

package com.example.td.barapp;

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.fragment.app.Fragment;

import com.example.td.barapp.databinding.FragmentMenuBinding;

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

    // 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;



    private Menu_Fragment() {
        // 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 Menu_Fragment.
     */
    // TODO: Rename and change types and number of parameters
    public static Menu_Fragment newInstance(String param1, String param2) {
        Menu_Fragment fragment = new Menu_Fragment();
        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);
        }


    }

    private FragmentMenuBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMenuBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.imageButtonCocktailsEn.setOnClickListener(this);
        binding.imageButtonCocktailsAlcfreeEn.setOnClickListener(this);
        binding.imageButtonLongdrinksEn.setOnClickListener(this);
        binding.imageButtonWhiskyEn.setOnClickListener(this);
        binding.imageButtonLiquorEn.setOnClickListener(this);
        binding.imageButtonBeerEn.setOnClickListener(this);
        binding.imageButtonSoftdrinksEn.setOnClickListener(this);
        binding.imageButtonHotDrinksEn.setOnClickListener(this);

    }

    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    @Override
    public void onClick(View view) {

        if(view.getId() == R.id.imageButton_Softdrinks_en) {
            float amount = 0f;
            Menu_FragmentDirections.ActionMenuFragmentToSoftdrinks  action = Menu_FragmentDirections
                    .actionMenuFragmentToSoftdrinks(amount);

        }

    }
}
4

2 回答 2

1

您在操作之前缺少对象类型。

它会是这样的:

Menu_FragmentDirections.ActionMenuFragmentToSoftdrinks action = Menu_FragmentDirections
                    .actionMenuFragmentToSoftdrinks(amount);

和错误:

错误:表达式浮点数的非法开始 = ...;

这是因为您想要一个浮点数并且您正在传递“...”。它必须是一个浮点数。

即:float amount = 0.6ffloat amount = 0f

编辑:

要向片段添加参数,请执行以下步骤:

选择目标片段并单击突出显示的加号。 在此处输入图像描述

之后,选择您想要的变量类型并为其命名 在此处输入图像描述

重建您的项目并检查它是否正在工作。

希望它有所帮助。

于 2020-08-26T09:52:35.563 回答
1

谁能告诉我,我必须在上面发布的 onCreateMethod() 中做什么。我收到错误消息:“无法解析符号‘操作’”和“无法解析符号‘导航’”(当然还有使用错误意外的标记 '...')

这就是我的想法,您的navGraph.xml存在问题,您打算将 SafeArg 从MenuFragmentto传递,SoftDrinksFragment但您没有编写代码来使其工作,这就是您action无法解决的原因。

编辑您的 navGraph.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/menu_Fragment">

    <fragment
        android:id="@+id/menu_Fragment"
        android:name="com.example.td.barapp.Menu_Fragment"
        android:label="fragment_menu_"
        tools:layout="@layout/fragment_menu" >
        <action
            android:id="@+id/action_menu_Fragment_to_softdrinks"
            app:destination="@id/softdrinks" />
    </fragment>
    <fragment
        android:id="@+id/softdrinks"
        android:name="com.example.td.barapp.Softdrinks"
        android:label="fragment_softdrinks"
        tools:layout="@layout/fragment_softdrinks">
        <argument
            android:name="amount"
            app:argType="integer" />
    </fragment>
    
</navigation>

还要解决问题,Navigation 请确保将此依赖项添加到您的 gradle 文件中

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$2.3.0"

将此行添加到 gradle 文件的顶部:

apply plugin: "androidx.navigation.safeargs"
于 2020-08-26T10:59:50.433 回答