通过按钮重新创建片段视图

时间:2013-07-01 11:55:11

标签: android fragment reload android-fragmentactivity recreate

我有一个使用ViewPager显示正确片段的FragmentActivity。我在ViewPager中添加了一个Fragment StatePagerAdapter。如果我按下片段上的按钮,则应该重新创建它(应该再次调用onCreateView)。片段是文章,如果用户离线,我会告诉他“请检查你的网络连接”和一个重新加载的按钮。该按钮在FragmentActivity中调用了一个方法,但我不知道如何实现它。也许我可以摧毁并重建当前的片段?

这里是FragmentActivity的重要部分:

private ArrayList<Fragment> fragments;
protected void onCreate(Bundle savedInstanceState)
{
    context = this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_article_page_slider);
    ...
    pages = datahandler.getCount(...);
    viewPager = (ViewPager) findViewById(R.id.articlePager);
    PagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
    viewPager.setAdapter(pagerAdapter);
    viewPager.setCurrentItem(startPosition);
    fragments = new ArrayList<Fragment>(pages);
}
public void reloadData(View v)
{
    // TODO
    ((ArticlePageFragment)fragments.get(viewPager.getCurrentItem())).refresh();
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
    public ScreenSlidePagerAdapter(FragmentManager fm)
    {
        super(fm);
    }
    @Override
    public Fragment getItem(int position)
    {
        Fragment fragment = new ArticlePageFragment();
        Bundle args = new Bundle();
        ...
        fragment.setArguments(args);
        fragments.add(position, fragment);
        return fragment;
    }
    public int getCount()
    {
        return pages;
    }
}

和Fragment本身:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    activity = getActivity();
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_article_page, container, false);

    CacheDataHandler cdh = new CacheDataHandler(activity);
    cdh.open();
    String htmlData = cdh.loadData();
    rootView.findViewById(R.id.btn_reload_article_data).setVisibility(View.GONE);
    webView = ((WebView) rootView.findViewById(R.id.articleFragment));
    refresh();
    ...
    return rootView;
}
public void refresh()
{
    ...
    if (htmlData == null)
    {
        rootView.findViewById(R.id.btn_reload_article_data).setVisibility(View.VISIBLE);
        webView.loadData(defaultdata,"text/html", "UTF-8");
    }
    else
    {
        webView.loadData(htmlData,"text/html", "UTF-8");
    }
}

和fragment_article_page.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/articleFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/btn_reload_article_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="reloadData"
        android:text="@string/btn_txt_reload_data"
        android:visibility="gone" />

</RelativeLayout>

修改 在FragmentActivity中添加了private ArrayList<Fragment> fragments并传输了用于在额外的refresh方法中操作视图的代码,展望未来。如果我只点击第一个或第二个片段(索引0和1),一切正常。否则我得到IndexOutOfBoundsException。如何初始化和填充ArrayList?

0 个答案:

没有答案