如何从活动中调用片段中的方法?

时间:2015-08-23 22:56:09

标签: android android-fragments

我有主要的活动,我想调用片段里面的方法我使用getSupportFragmentManager来获取片段但是总是向我返回null你能帮我吗

mainActivity:

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity implements OnItemPressListener {

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


    }
   public void A()
   {
       FragmentManager fragManager = this.getSupportFragmentManager();
      MainFragment fragment= (MainFragment)fragManager.findFragmentById(R.id.mainfragment);///always Null
       fragment.B();
   }.
}

main_fragment:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mainfragment"
    tools:context=".mainFragment">
    <GridView
        android:id="@+id/gridview_movie"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></GridView>
</FrameLayout>

MainFragmet:

import android.support.v4.app.Fragment;;

public class MainFragment extends Fragment {
 public void B() {
///do some thing
}
}

4 个答案:

答案 0 :(得分:1)

你只需要从主要活动

中的片段中获取一个实例
MainFragment fragment=new MainFragment();

答案 1 :(得分:0)

我没有在您的问题中看到任何代码表明您实例化Fragment并将其加载到R.id.mainfragment中。此外,您的main_fragment.xml文件包含GridView个参数match_parent。如果您打算将片段加载到R.id.mainfragment,这是毫无意义的,因为GridView只会模糊它。

考虑到这一点,我会对您的代码进行以下更正。

  1. GridView文件中删除layout

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainfragment"
        tools:context=".mainFragment" />
    
  2. onCreate()的{​​{1}}中添加以下内容:

    MainActivity

答案 2 :(得分:0)

如果你想使用findFragmentById ......你必须这样写:

    <fragment
    android:id="@+id/mainfragment"
    class="com.oldfeel.base.BaseFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

答案 3 :(得分:0)

首先,您还没有在主要活动中实例化您的片段,请尝试以下代码:

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            HeadlinesFragment firstFragment = new HeadlinesFragment();

            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment, "myAwesonFragment").commit();
        }
    }

然后如果您的方法A执行以下操作

public void A()
   {
       BrowseFragment fragment = (BrowseFragment) getSupportFragmentManager().findFragmentByTag("myAwesonFragment");
        //Check if the Bottom Sheet is Expanded
        fragment.B();
   }

这一次,它不会是空的。当我想找到一个片段时,我个人总是使用findFragmentByTag。

PD:我们使用了getSupportFragmentManager(),因为如果使用android.app.Fragment使用getFragmentManager()

,则使用android.support.v4.app.Fragment