【问题标题】:Unable to replace/navigate fragments无法替换/导航片段
【发布时间】:2013-12-04 04:23:36
【问题描述】:

我正在尝试从一个片段导航到另一个片段。这两个片段是 100% 不同的,没有 ViewPager 或类似的东西来连接它们。这是代码。

fragment_view1.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/firstView">

    <TextView
        android:id="@+id/viewOneText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_alignParentBottom="true"/>


   <FrameLayout
                android:id="@+id/fragment_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
</RelativeLayout>

custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/customFragment" >

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

</RelativeLayout>

FirstView.java(使用 fragment_view1.xml)

    package com.example.fragmenttest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
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
    {

        @Override
        public void onClick(View v)
        {
            // Create new fragment and transaction
            Fragment newFragment =  new CustomView();
            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_view, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }

    }


}

CustomView.java(使用 custom_view.xml)

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;

    }


}

FirstView.java,当我点击按钮时,我需要移动到片段CustomView。但它只是移动,它只是替换了CustomView 中的所有内容,位于FirstView 之上。下面的图片会更好地解释它。

FirstView 一个人

CustomView 一个人

当按钮被FirstView点击时

为什么会这样?

【问题讨论】:

  • 需要在 FragmentActivity 内部交换 Fragment。

标签: java android eclipse user-interface android-fragments


【解决方案1】:

您正在尝试用新片段替换firstView,但firstView 不是片段,它是RelativeLayout

您不能替换布局文件中静态定义的片段。您只能替换通过 FragmentTransaction 动态添加的片段。

您的布局中均不包含布局中的片段。

<fragment
        android:id="@+id/fragment1"
        android:layout_width="march_parent"
        android:layout_height="match_parent"></fragment>

新的 Fragment 将替换之前添加到容器中的现有 Fragment。

关注this

【讨论】:

  • 感谢您的回复。是的,我看了但没看懂。那我该怎么办?代码示例?请问可以修改我的演示代码吗?
  • 虽然您知道,但请修改片段的工作原理,(developer.android.com/guide/components/fragments.html) 更改您要查找的内容。
  • 我编辑了代码。请查看 FirstView.java 和 fragment_view1.xml。问题依然存在
  • 您已在 Layout.xml 文件中添加了FrameLayout,您需要添加fragment。还有一件更重要的事情 - 您需要首先在片段上附加第一个视图,然后在按钮 onClick 上您需要将其替换为自定义片段。希望你明白我的意思。
【解决方案2】:

如果您需要用另一个片段替换一个片段,那么下面提到:

TalkDetail fragment = new TalkDetail(); 
// TalkDetail is name of fragment which you need to put while replacing older one.
Bundle bundle = new Bundle();

                    bundle.putString("title", m_ArrayList.get(arg2).title);
                    bundle.putString("largeimg", m_ArrayList.get(arg2).largeimg);
                    bundle.putString("excert", m_ArrayList.get(arg2).excert);
                    bundle.putString("description", m_ArrayList.get(arg2).description);
                    bundle.putString("cat", m_ArrayList.get(arg2).cat);
                    bundle.putString("header_title", "Talk");
                    //bundle.putInt("postid", m_ArrayList.get(arg2).postid);

                    fragment.setArguments(bundle);                  ((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);

这是你的 BaseContainerFragment.java 类,它将为你管理很多东西。

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import app.drugs.talksooner.R;

public class BaseContainerFragment extends Fragment {

    public void replaceFragment(Fragment fragment, boolean addToBackStack) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.replace(R.id.container_framelayout, fragment);
        transaction.commit();
        getChildFragmentManager().executePendingTransactions();
    }

    public boolean popFragment() {
        Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
        boolean isPop = false;
        if (getChildFragmentManager().getBackStackEntryCount() > 0) {
            isPop = true;
            getChildFragmentManager().popBackStack();
        }
        return isPop;
    }

}

有关更多具体细节,请在此处找到我的完整帖子..

Dynamically changing the fragments inside a fragment tab host?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    相关资源
    最近更新 更多