如何从一个片段导航到另一个片段?

Posted

技术标签:

【中文标题】如何从一个片段导航到另一个片段?【英文标题】:How to navigate from one Fragment to another? 【发布时间】:2013-12-19 10:44:27 【问题描述】:

这是我的代码:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >



    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_
        android:layout_
        >
       </android.support.v4.view.ViewPager>



</RelativeLayout>

fragment_view1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical" >

    <TextView
        android:id="@+id/viewOneText"
        android:layout_
        android:layout_
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="92dp"
        android:layout_marginTop="182dp"
        android:text="First View"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/viewOneBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_
        android:layout_
        android:layout_alignRight="@+id/viewOneText"
        android:layout_below="@+id/viewOneText"
        android:layout_marginTop="17dp"
        android:text="Click Here" />

  <include layout = "@layout/drop_down"
            android:layout_
        android:layout_
        android:layout_marginTop="15dp"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_
        android:layout_
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="174dp"
        android:text="Custom Fragment"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

MainActivity.java

package com.example.fragmenttest;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends  FragmentActivity  

    private ViewPager viewPager;
    private MyAdapter pageAdapter;
    private static final int ITEMS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager)findViewById(R.id.pager);
        pageAdapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pageAdapter);

    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    

    public static class MyAdapter extends FragmentPagerAdapter 


        public MyAdapter(FragmentManager fragmentManager) 
            super(fragmentManager);
        

        @Override
        public int getCount() 
            return ITEMS;
        

        @Override
        public Fragment getItem(int position) 
            if(position==0)
            
                return new FirstView();
            
            else
            
                return new SecondView();
            
        
    

    public void setCurrentItem (int item, boolean smoothScroll) 
        viewPager.setCurrentItem(item, smoothScroll);
    

public void onMenuItemClicked(View view) 
        Toast.makeText(this, "LOL", Toast.LENGTH_LONG).show();
    


FirstView.java

package com.example.fragmenttest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FirstView extends DropDownMenu

    private TextView firstText;
    private Button btn;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    


        View view = inflater.inflate(R.layout.fragment_view1,container,false);


        firstText = (TextView)view.findViewById(R.id.viewOneText);
        btn = (Button)view.findViewById(R.id.viewOneBtn);

        btn.setOnClickListener(new ButtonEvent());
        return view;

    

    private class ButtonEvent implements OnClickListener
    


    



CustomView.java

package com.example.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class CustomView extends Fragment

    private TextView secondText;
    private Button secondViewBtn;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    
        View view = inflater.inflate(R.layout.custom_view,container,false);

        return view;

    



当我单击fragment_view1.xml 中的按钮时,我需要转到custom_view.xml 屏幕。这是一个完全不同的片段。我该怎么做?

【问题讨论】:

【参考方案1】:

将此添加到您的OnClickListener

CustomView cv = new CustomView();
FragmentManager fm= getFragmentManager();
FragmentTransaction ft= fm.beginTransaction();
ft.replace(R.id.custom_view, cv);
ft.commit();

您需要将其添加到您的

中的 RelativeLayout

custom_view.xml

android:id="@+id/custom_view"

【讨论】:

@Artificial_Intelligence ***.com/questions/7445437/… 我刚刚注意到您正在使用 ViewPager。我相信这个问题与您所问的类似?希望这有帮助。顺便说一句,尝试使用 FragmentStatePagerAdapter 而不是 FragmentPagerAdapter。 ViewPager 对此没有影响。【参考方案2】:

试试这个...放在监听器中

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

【讨论】:

嗨,这个 R.id.fragment_container 是什么? Artificial_Intelligence:参考这个tutorialspoint.com/android/android_fragments.htm它的布局id 我的自定义视图 ID 是“自定义视图”。当我添加这个 ID 时发生了错误,所以我添加了当前可见片段布局的 ID。现在一切都在当前可见的项目之上!【参考方案3】:

//创建一个FrameLayout并在你的fragment_view1.xml中给id作为fragment_view 并像下面那样做

在你的布局中添加这一行fragment_view1.xml

 <FrameLayout
                android:id="@+id/fragment_view"
                android:layout_
                android:layout_ />



private class ButtonEvent implements OnClickListener
    

CustomView custFra = new CustomView();
fragmentTransaction.replace(R.id.fragment_view, custFra).commit();


  

【讨论】:

err.I didn't understand 在你的布局 fragment_view1.xml 中添加一个 FrameLayout。 感谢您的回复。我做到了,不是他们相互崩溃。我开了一个新问题,请看一下 - ***.com/questions/20366804/…

以上是关于如何从一个片段导航到另一个片段?的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何在选项卡内从一个片段导航到另一个片段? [关闭]

使用导航从一个片段导航到另一个片段后,防止后按工作

使用 NavController 从片段导航到另一个片段

从片段导航到另一个片段时隐藏键盘

如何使用 JetpackNavigation 组件从 BottomSheetDialogFragment 导航到另一个 Fragment

想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉