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

2023-09-04 12:49:58 作者:演绎、只属于我们旳未来つ

我有一个简单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为状态一个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 ?

我已经试过这一点没有更迭:

I have tried this without succes :

片段:

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");
    }

但它给我的电视一个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 :

Updating在ViewPager 从活动片段 Update从另一个片段在ViewPager一个TextView Updating fragments from Activity in a ViewPager Update a TextView in a ViewPager from another Fragment

推荐答案

在你的 MyPagerAdapter 类,Android把的getItem(...),只有当它要创造新的片段。所以这是有问题的使用有引用,而不是你应该instatiate的片段。

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