从ViewPager Activity中访问Fragment的方法

7

我有一个Fragment,其中有一个setName()方法,通过setText函数改变EditText的文本。

从承载该片段的活动中,通过ViewPager调用该方法的最佳方式是什么?

换句话说,如何从通过ViewPager托管该片段的活动中访问片段的方法(例如更改该片段的布局)?

我之所以问这个问题,是因为我尝试了几种方法,但总是出现错误。


您可以尝试在 Activity 类中使用 setName() 方法创建 Fragment 的实例变量。在实例化 Fragment 时将变量分配给它。然后,您可以从 Activity 中调用 fragment.setName() - Scott
我有这个问题,但通过存储的引用调用fragment.setName()不是问题所在。问题在于从片段中,getActivity()返回null,因此无法访问视图。虽然可以从活动中访问视图,并且片段可以在配置时将信息放入属于片段的视图中。但是后来,getActivity()返回null,可能是viewPager控制上下文导致getActivity实际上无效吗?因此,需要从Viewpager请求视图?我正在询问。 - carl
4个回答

12

最好的方法是直接拨打电话

CallingFragmentName fragment = (CallingFragmentName) viewPager
                    .getAdapter()
                    .instantiateItem(viewPager, viewPager.getCurrentItem());

它将重新实例化您调用的Fragment,这样它就不会抛出空指针异常。


这应该是被接受的答案。非常感谢。你真是个救命恩人。 - Ron Daulagupu
这对我很有帮助,谢谢。 - Bipin Bharti
你节省了我的时间。谢谢。解决方案完美运行。 - Satyam Gondhale

7

我知道可能有点晚了,但我遇到了同样的问题,如果你已经解决了这个问题,也许我的经验可以帮助其他人。

我在使用ViewPager时发现的第一个问题是,几乎不可能获取到Fragment的引用。Fragment在getItem()中动态创建,因此您无法设置ID,并且它们会被切换器自动重新标记,因此您也无法通过标记找到它们。有一些方法可以解决这个问题,但都是绕弯路。(Update data in ListFragment as part of ViewPager)

我解决这个问题的方式是使用基本上是双重回调。 Fragment A具有由主Activity实现的接口,主Activity具有由Fragment B实现的接口。例如,在Fragment A中点击按钮时,将调用Main Activity中的回调函数,然后再调用Fragment B中的回调函数。请查看下面的代码。我希望我发布的所有内容都能帮助到你。顺便说一下,我只尝试过在ViewPager中使用这种方法,但我认为它适用于任何类型的Fragment通信。

Main Avtivity java:

import android.os.Bundle;
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.ViewPager;

public class MainActivity extends FragmentActivity implements FragmentA.Caller {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    PassCallToB passOnToB = null;
    FragmentManager myManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
            MyManager = fm;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            if(position == 0) {
                fragment = new FragmentA();
            } else if (position == 1) {
                fragment = new FragmentB();
                passOnToB = (PassCallToB)fragment;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return "Frag A";
            case 1:
                return "Frag B";
            }
            return null;
        }

        public void setCallback() {
            List<Fragment> frags = myManager.getFragments();
            for(Fragment fragment : frags) {
                if(fragment instanceof FragmentB){
                    passOnToB = (PassCallToB)fragment;
                }
            }
        }
    }

    public interface PassCallToB {
        public void passItOn();
    }

    @Override
    public void CallB() {
        if(passOnToB instanceof Fragment)
            passOnToB.passItOn();
        else {
            mSectionsPagerAdapter.setCallback();
            passOnToB.passItOn();
        }
    }
}

主活动xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

</android.support.v4.view.ViewPager>

碎片 A java:

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

public class FragmentA extends Fragment {

    Button btnCallB = null;

    Caller listener = null;

    public FragmentA() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle inState) {
        View rootView = inflater.inflate(R.layout.fragment_a, container, false);

        btnCallB = (Button)rootView.findViewById(R.id.btnCallB);
        btnCallB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                listener.CallB();
            }

        });

        return rootView;
    }

    public interface Caller {
        public void CallB();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof FragmentActivity) {
          listener = (Caller) activity;
        } else {
          throw new ClassCastException(activity.toString() + " must implemenet listener");
        }
    }

     @Override
      public void onDetach() {
        super.onDetach();
        listener = null;
      }
}

Fragment A xml:

<RelativeLayout 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" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="This is Fragment A" />

    <Button
        android:id="@+id/btnCallB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:text="Call Fragment B" />

</RelativeLayout>

Java 代码片段 B:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class FragmentB extends Fragment implements MainActivity.PassCallToB {

    public FragmentB() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle inState) {
        View rootView = inflater.inflate(R.layout.fragment_b, container, false);
        return rootView;
    }

    @Override
    public void passItOn() {
        Toast.makeText(getActivity(), "Hello from B", Toast.LENGTH_SHORT).show();

    }
}

片段 B 的 XML:

<RelativeLayout 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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="This is Fragment B" />

</RelativeLayout>

当您旋转时,此方法无法正常工作。因为旋转活动和片段会被重新创建,这也会重新创建您的SectionsPagerAdapter,但它从未调用getItem(Android在不调用getItem的情况下重新创建片段),这意味着passOnToB仍然为空。 - rve
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - ckn
有什么想法吗?http://stackoverflow.com/questions/39907625/why-is-accessing-textview-of-a-fragment-inside-activity-throwing-an-error - Si8

3

您可以访问由您的ViewPager持有的片段中的公共方法。您需要(1)在创建Fragment并将其添加到将支持您的页面适配器的列表时存储对其的引用,或者(2)需要从页面适配器本身获取对片段的引用。例如:

Fragment fragmentA = null; //instance variable

fragmenA = new Fragment(); //whereever you instantiate your fragment

如果你的方法是:
public void setName(String args){
    //do something
}

你只需要从你的 ViewPager 引用持有的片段引用中调用该方法。

fragmentA.setName(args);

您可以像调用常规方法一样传递所需的参数。请注意,只有在从其包含的ViewPagerFragmentActivity中调用片段内的方法时,此方法才有效。如果您想要反向实现,即片段到活动的通信,则需要使用接口。


是的,我已经知道了。但是如果我尝试使用类似Frag1 fragment =(Frag1)mAppSectionsAdapter.getItem(0);这样的东西来实例化它呢?那么我该如何在不出现空指针异常的情况下使用该方法呢? - JZweige
就好像 fragment.getView() 总是 null 一样! - JZweige
你想从你的适配器中获取引用吗?如果这是问题,我现在不在电脑旁边,但稍后可以发布代码。 - Rarw
请帮我解决这个问题... http://stackoverflow.com/questions/39907625/why-is-accessing-textview-of-a-fragment-inside-activity-throwing-an-error - Si8
你的答案在ViewPager中总是对我有用,谢谢你,伙计。只为了你,我收藏了这个问题。谢谢,伙计 :) - Sumit Kumar
无法工作。试图在空对象引用上调用虚拟方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)' - Yaroslav Dukal

2

片段

private static FragmentName instance;

public static synchronized FragmentName getInstance()
{
    return instance;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance=this;
....
}

public void methodName()
{...}

活动

FragmentName.getInstance().methodName();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接