本文介绍了无法替代/导航片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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



fragment_view1.xml

 <?xml version =1.0encoding =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 = 第一视图
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 =点击这里/>

< 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.0encoding =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_centerHorizo​​ntal =true
android:layout_marginTop =174dp
android:text =自定义片段
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;
私人按钮btn;

public View onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState)
{


查看视图= inflater.inflate(R.layout.fragment_view1 ,容器,FALSE);


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

btn.setOnClickListener(new ButtonEvent());
返回视图;

}

私有类ButtonEvent实现OnClickListener
{

@Override
public void onClick(View v)
{
//创建新的片段和事务
片段newFragment = new CustomView();
FragmentTransaction transaction = getFragmentManager()。beginTransaction();

//使用此片段替换fragment_container视图中的任何内容,
//并将事务添加到后端栈
transaction.replace(R.id.fragment_view,newFragment );
transaction.addToBackStack(null);

//提交事务
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容器,Bundle savedInstanceState)
{
查看视图= inflater.inflate(R.layout.custom_view,container,false);

返回视图;

}


}

FirstView.java 中,当我点击按钮时,我需要移动到片段 CustomView 。而是移动,它只是替换 CustomView FirstView 之上的所有内容。



FirstView 单独





CustomView 单独





当按钮为 FirstView 点击





为什么这样发生?请帮助

解决方案

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



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



您的布局中没有包含

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

新的片段将替换之前添加到容器的现有片段。



查看。


I am tryng to navigate from one fragment to another. These 2 fragments are 100% different, there is no ViewPager or something like that to connect them. Here is the code.

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 (uses 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 (uses 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;

    }


}

In FirstView.java, when I click on the button, I need to move to the Fragment CustomView. But instead moving, it simply replaces everything in CustomView on top of FirstView. Below images will explain it better.

FirstView alone

CustomView alone

When the button is FirstView clicked

Why it is happening like this? Please help

解决方案

You are trying to replace firstView with the new fragment, but firstView is not a fragment, its' a RelativeLayout.

You cannot replace a fragment defined statically in the layout file. You can only replace fragments that you added dynamically via a FragmentTransaction.

None of your Layout contains fragment in layout.

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

A new Fragment will replace an existing Fragment that was previously added to the container.

Take look on this.

这篇关于无法替代/导航片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:28