findFragmentByTag()在android

时间:2016-07-08 00:53:55

标签: android android-fragments

我是android.i的初学者,在findFragmentByTag()方法中发现了类似的问题。

我有2个Fragment和1个Activity.i想要创建一个灵活的Activity,它运行纵向模式运行FragmentA,当它运行横向模式时,它会并行运行两个片段(FragmentA和FragmentB)。是添加xml,但我想添加运行时。但我想在运行时访问FragmentB对象,所以我使用findFragmentByTag()menth bt它给出null。 请帮助我...

activity_main.xml(纵向模式)
   `

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/mylayout"
    android:orientation="vertical"
    tools:context="com.example.manu.newapp.MainActivity">


</LinearLayout>

activity_main.xml(横向模式)

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.manu.newapp.MainActivity"
    android:id="@+id/mylayout">


</LinearLayout>

FragmentA.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#71d708"
android:orientation="vertical"

tools:context="com.example.manu.newapp.FragmentA">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_fragmentA" />

<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right|top">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bye"/>
</LinearLayout>

FragmentB.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#0982eb"
android:id="@+id/fragB"
tools:context="com.example.manu.newapp.FragmentB">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />        </FrameLayout>

MainActivity.java

    package com.example.manu.newapp;

import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //methods are call here
        aDD(savedInstanceState);
        rot();

        setContentView(R.layout.activity_main);
        Log.d("lenovo", "OnCreate");
    }

    public void aDD(Bundle savedInstanceState) {                         //this method add Fragments

        if (findViewById(R.id.mylayout) == null) {
            if (savedInstanceState == null) {

                FragmentA fragmentA = new FragmentA();
                FragmentB fragmentB = new FragmentB();

                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.add(R.id.mylayout, fragmentA, "A");
                transaction.add(R.id.mylayout, fragmentB, "Btag");


                transaction.commit();
            }
        }
    }

    public void rot() {                                                   // this method check rotation and hide and unhide fragmentB
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        int orientation = display.getRotation();
        FragmentB fragmentB = (FragmentB) getFragmentManager().findFragmentByTag("B");

        if ((orientation == Surface.ROTATION_90) || orientation == Surface.ROTATION_270) {
            getFragmentManager().beginTransaction().show(fragmentB).commit();

        } else if ((orientation == Surface.ROTATION_0) || orientation == Surface.ROTATION_180) {
            getFragmentManager().beginTransaction().hide(fragmentB).commit();
       }   } }

FragmentA.java

    package com.example.manu.newapp;

        import android.content.Context;
        import android.net.Uri;
        import android.os.Bundle;
        import android.app.Fragment;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;



public class FragmentA extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_a, container, false);
    }
}

FragmentB

    package com.example.manu.newapp;


import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentB extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_b, container, false);
    }

}

2 个答案:

答案 0 :(得分:0)

我同意Manoj这只是一个错字。只需更改此行:

FragmentB fragmentB = (FragmentB) getFragmentManager().findFragmentByTag("B");

到这一行:

FragmentB fragmentB = (FragmentB) getFragmentManager().findFragmentByTag("Btag");

答案 1 :(得分:0)

findFragmentByTag()

不适用于onCreat()方法。所以我使用onResume();