Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

android - How to update fragment content from activity (viewpager)?

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.

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

Adapter :

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

Fragment :

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

XML Fragment :

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

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 :

Fragment :

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

Activity :

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

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

I already read the following questions without success :

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Check this Answer support-fragmentpageradapter-holds-reference-to-old-fragments


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...