导航在点击时未跳转到下一个片段。

huangapple go评论82阅读模式
英文:

Navigation doesnt proceed to next fragment on click

问题

在按钮点击时,奇怪的是它不转到其他片段。

activity_main.xml:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:logo="@android:drawable/btn_star_big_on" />

        <fragment
            android:id="@+id/fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/main_nav" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

fragment_first.xml:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:elevation="20dp"
    tools:context=".first">

    <Button
        android:id="@+id/button_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="20dp"
        android:text="First"
        android:textColor="@color/colorPrimary"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

fragment_second.xml:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C13584"
    tools:context=".second">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second"
        android:textColor="#C13584"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

main_nav.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/main_nav"
    app:startDestination="@id/first">

    <fragment
        android:id="@+id/first"
        android:name="com.example.fragments.first"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first">
        <action
            android:id="@+id/navigateToSecondFragment"
            app:destination="@id/second" />
    </fragment>
    <fragment
        android:id="@+id/second"
        android:name="com.example.fragments.second"
        android:label="fragment_second"
        tools:layout="@layout/fragment_second">
        <action
            android:id="@+id/navigateToFirstFragment"
            app:destination="@id/first" />
    </fragment>
</navigation>

first.java:

package com.example.fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class first extends Fragment {

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

    private String mParam1;
    private String mParam2;

    private Button button;

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

    public static first newInstance(String param1, String param2) {
        first fragment = new first();
        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) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);

        button = (Button) view.findViewById(R.id.button_first);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Navigation.findNavController(view).navigate(R.id.navigateToSecondFragment);
            }
        });

        return view;
    }
}

second.java:

package com.example.fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class second extends Fragment {

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

    private String mParam1;
    private String mParam2;

    private Button button;

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

    public static second newInstance(String param1, String param2) {
        second fragment = new second();
        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_PARAM

<details>
<summary>英文:</summary>

As the title describes on button click it doesnt go to the other fragment for some odd reason.

activity_main.xml:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        xmlns:tools=&quot;http://schemas.android.com/tools&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        tools:context=&quot;.MainActivity&quot;&gt;
    
        &lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:orientation=&quot;vertical&quot;&gt;
    
            &lt;androidx.appcompat.widget.Toolbar
                android:id=&quot;@+id/toolbar&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:background=&quot;?attr/colorPrimary&quot;
                android:minHeight=&quot;?attr/actionBarSize&quot;
                android:theme=&quot;?attr/actionBarTheme&quot;
                app:logo=&quot;@android:drawable/btn_star_big_on&quot; /&gt;
    
            &lt;fragment
                android:id=&quot;@+id/fragment&quot;
                android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                app:defaultNavHost=&quot;true&quot;
                app:navGraph=&quot;@navigation/main_nav&quot; /&gt;
    
        &lt;/LinearLayout&gt;
    
    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

fragment_first.xml:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        xmlns:tools=&quot;http://schemas.android.com/tools&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;@color/colorPrimary&quot;
        android:elevation=&quot;20dp&quot;
        tools:context=&quot;.first&quot;&gt;
    
        &lt;Button
            android:id=&quot;@+id/button_first&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:elevation=&quot;20dp&quot;
            android:text=&quot;First&quot;
            android:textColor=&quot;@color/colorPrimary&quot;
            android:textStyle=&quot;bold&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintHorizontal_bias=&quot;0.5&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

fragment_second.xml:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        xmlns:tools=&quot;http://schemas.android.com/tools&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;#C13584&quot;
        tools:context=&quot;.second&quot;&gt;
    
        &lt;Button
            android:id=&quot;@+id/button2&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;Second&quot;
            android:textColor=&quot;#C13584&quot;
            android:textStyle=&quot;bold&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintHorizontal_bias=&quot;0.5&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

main_nav.xml:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        xmlns:tools=&quot;http://schemas.android.com/tools&quot;
        android:id=&quot;@+id/main_nav&quot;
        app:startDestination=&quot;@id/first&quot;&gt;
    
        &lt;fragment
            android:id=&quot;@+id/first&quot;
            android:name=&quot;com.example.fragments.first&quot;
            android:label=&quot;fragment_first&quot;
            tools:layout=&quot;@layout/fragment_first&quot; &gt;
            &lt;action
                android:id=&quot;@+id/navigateToSecondFragment&quot;
                app:destination=&quot;@id/second&quot; /&gt;
    
        &lt;/fragment&gt;
        &lt;fragment
            android:id=&quot;@+id/second&quot;
            android:name=&quot;com.example.fragments.second&quot;
            android:label=&quot;fragment_second&quot;
            tools:layout=&quot;@layout/fragment_second&quot; &gt;
            &lt;action
                android:id=&quot;@+id/navigateToFirstFragment&quot;
                app:destination=&quot;@id/first&quot; /&gt;
        &lt;/fragment&gt;
    &lt;/navigation&gt;

first.java:

    package com.example.fragments;
    
    import android.os.Bundle;
    
    import androidx.fragment.app.Fragment;
    import androidx.navigation.Navigation;
    
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    /**
     * A simple {@link Fragment} subclass.
     * Use the {@link first#newInstance} factory method to
     * create an instance of this fragment.
     */
    public class first extends Fragment {
    
        // TODO: Rename parameter arguments, choose names that match
        // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
        private static final String ARG_PARAM1 = &quot;param1&quot;;
        private static final String ARG_PARAM2 = &quot;param2&quot;;
    
        // TODO: Rename and change types of parameters
        private String mParam1;
        private String mParam2;
    
        private Button button;
    
        public first() {
            // 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 first.
         */
        // TODO: Rename and change types and number of parameters
        public static first newInstance(String param1, String param2) {
            first fragment = new first();
            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
    
            View view =  inflater.inflate(R.layout.fragment_first, container, false);
    
            button = (Button) view.findViewById(R.id.button_first);
    
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    //Start second view
                    //Navigation.findNavController(view).navigate(R.id.);
                    Navigation.findNavController(view).navigate(R.id.navigateToSecondFragment);
    
                }
            });
    
    
            return view;
        }
    }

second.java:

    package com.example.fragments;
    
    import android.os.Bundle;
    
    import androidx.fragment.app.Fragment;
    import androidx.navigation.Navigation;
    
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    /**
     * A simple {@link Fragment} subclass.
     * Use the {@link second#newInstance} factory method to
     * create an instance of this fragment.
     */
    public class second extends Fragment {
    
        // TODO: Rename parameter arguments, choose names that match
        // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
        private static final String ARG_PARAM1 = &quot;param1&quot;;
        private static final String ARG_PARAM2 = &quot;param2&quot;;
    
        // TODO: Rename and change types of parameters
        private String mParam1;
        private String mParam2;
    
        private Button button;
    
        public second() {
            // 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 second.
         */
        // TODO: Rename and change types and number of parameters
        public static second newInstance(String param1, String param2) {
            second fragment = new second();
            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
            View view =  inflater.inflate(R.layout.fragment_first, container, false);
    
            button = (Button) view.findViewById(R.id.button_first);
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    //Start first view
                    Navigation.findNavController(view).navigate(R.id.navigateToFirstFragment);
                }
            });
            return view;
        }
    }

</details>


# 答案1
**得分**: 1

对于您的第二个片段您正在填充用于第一个片段的布局`R.layout.fragment_first`。您的导航可能实际上运行良好我没有发现任何问题),只是两个片段的布局相同因此您不会注意到转换因为没有定义转换动画检查当您点击按钮时是否导航栏的标题会更改fragment_firstfragment_second”,如标签中所定义的那样),如果是则问题不在于导航

<details>
<summary>英文:</summary>

For your `second` fragment, you are inflating layout that is meant to be for the `first` fragment, i.e. `R.layout.fragment_first`. Your navigation may actually be working fine (I did not find any issue), it is just that the layout for both fragments are the same, so you would not notice the transition, as no transition animations were defined. Check if the navigationBar&#39;s title changes when you click the button (from `fragment_first` to `fragment_second`, as defined by the labels), if yes, then the issue is not in navigation.

</details>



huangapple
  • 本文由 发表于 2020年8月18日 14:32:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63463038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定