如何从活动(viewpager)更新片段内容?

How to update fragment content from activity (viewpager)?(如何从活动(viewpager)更新片段内容?)

本文介绍了如何从活动(viewpager)更新片段内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个片段的简单 viewPager.每个片段都包含一个 TextView.我希望能够更新活动中显示的当前 textView 的文本.

I have a simple viewPager with two fragments. Each fragment contains a TextView. I would like to be able to update the text of the current textView displayed from the activity.

活动:

public class MainActivity extends FragmentActivity {

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

        ArrayList<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(Fragment.instantiate(this, Live.class.getName()));
        fragments.add(Fragment.instantiate(this, Hit.class.getName()));

        this.pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),fragments);

        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(pagerAdapter);
    }
}

适配器:

public class MyPagerAdapter extends FragmentPagerAdapter {

    private final ArrayList<Fragment> fragments;

    public MyPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments)
    {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

片段:

public class Live extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.live, container,false);
    }
}

XML 片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <TextView android:layout_height="fill_parent"
              android:layout_width="fill_parent"
              android:text="First Fragment"
              android:id="@+id/status"/>
</LinearLayout>

每个片段都有一个 ID 为status"的 TextView.在 onCreateView 中更新文本视图有效.但是我怎样才能从活动中的任何地方更新 textView 呢?

Each fragment have a TextView with the id "status". Updating the text view in onCreateView works. But how can i update the textView from anywhere in the activity ?

我试过这个没有成功:

片段:

public class Live extends Fragment {    
    public TextView tv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.live, container,false);
        tv = (TextView) view.findViewById(R.id.status);

        return view;
    }
}

活动:

@Override
    protected void onResume() {
        super.onResume();
        Live frag = (Live) pagerAdapter.getItem(0);
        frag.tv.setText("Test 2");
    }

但它在 tv 上给了我一个 nullPointerException,可能是因为片段尚未膨胀.

But it gives me a nullPointerException on tv, probably because the fragment is not yet inflated.

我已经阅读了以下问题但没有成功:

I already read the following questions without success :

  • 从 ViewPager 中的 Activity 更新片段
  • 从另一个 Fragment 更新 ViewPager 中的 TextView

推荐答案

在你的 MyPagerAdapter 类中,Android 仅在它想要创建时调用 getItem(...)新片段.所以在那里使用引用是有问题的,而不是你应该实例化片段.

In your MyPagerAdapter class, Android calls getItem(...) only if it wants to create new Fragments. So it's problematic to use references there instead you should instatiate the fragments.

检查此答案support-fragmentpageradapter-holds-reference-to-old-fragments

这篇关于如何从活动(viewpager)更新片段内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从活动(viewpager)更新片段内容?