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

时间:2013-02-28 14:02:01

标签: android android-fragments android-viewpager

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

活动:

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?

我没有成功地试过这个:

片段:

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,可能是因为片段还没有膨胀。

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

3 个答案:

答案 0 :(得分:7)

MyPagerAdapter课程中,只有在想要创建新片段时,Android才会调用getItem(...)。因此,在那里使用引用代替你应该对片段进行实例化是有问题的。

选中此答案support-fragmentpageradapter-holds-reference-to-old-fragments

答案 1 :(得分:2)

我像这样解决了这个问题,像这样创建适配器

package com.yubraj.sample.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.yubaraj.sample.activities.RashiFragment;
import java.util.List;


public class PageSliderAdapter extends FragmentStatePagerAdapter {
    static String[] titleList = new String[] { "today",
            "this month", "this year" };
    List<String> rashi;
    // private static String TAG = "pagerscreenslider";

    public PageSliderAdapter(FragmentManager fm, List<String> rashiList) {
        super(fm);
        this.rashi = rashiList;
    }
    public void updateData(List<String> rashisList){
        rashi.clear();
        this.rashi = rashisList;
        notifyDataSetChanged();
    }
    @Override
    public int getItemPosition(Object object) {
        // TODO Auto-generated method stub
        return POSITION_NONE;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new RashiFragment();
        Bundle bundle = new Bundle();
        bundle.putString("RASHI", rashi.get(position));
        bundle.putInt("RASHI_TYPE", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getCount() {
        return titleList.length;
    }

    @Override
    public String getPageTitle(int position) {
        return titleList[position];
    }


}

然后来自您的活动

List<String> rashiCollection = getJSONFromDb(Dates);
adapter.updateData(rashiCollection);

答案 2 :(得分:1)

@Override
    public int getItemPosition(Object object) {
    XXXFragment fragment = (XXXFragment) object;
        if(…condition){
    fragment.refresh(…);
    }
        return super.getItemPosition(object);
    }

覆盖以上内容。当你调用notifyDataSetChanged()时,上面的方法被触发了。尽可能地进行刷新。