如何在ViewPager Fragment中更改视图

时间:2015-11-06 02:20:28

标签: java android android-fragments android-viewpager adapter

我的活动中有ViewPager个片段,每个页面都有一个textview,我想要更改textviewonCreate方法的文字包含viewpager的活动。我应该怎么做呢?

下面的

是返回空对象异常的代码:

片段:

public class MyFragment extends Fragment {

private TextView textView;

public static MyFragment getInstance(){
    return new MyFragment();
}

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

    textView = (TextView) rootView.findViewById(R.id.textView);

    return rootView;
}

public void setText(String text){
    textView.setText(text);
}
}

适配器:

public class ViewPagerAdapter extends FragmentPagerAdapter {

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return MyFragment.getInstance();
}

@Override
public int getCount() {
    return 3;
}
}

活动onCreate:

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

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);

    MyFragment fragment = (MyFragment) adapter.getItem(0);
    fragment.setText("Altered");        // and this returns null pointer exception
}

谢谢!

2 个答案:

答案 0 :(得分:1)

大多数FragmentPagerAdapter将使用addFragment方法,并会跟踪通过内部对象添加到其中的各种片段:

public class MyPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragments.add(fragment);
        mFragmentTitles.add(title);
    }

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

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

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitles.get(position);
    }
}

按照目前的情况,getItem的实现每次调用时都会返回MyFragment的新实例。因此,MyFragment fragment = (MyFragment) adapter.getItem(0);是一个永远不会加载到ViewPager中的新实例,永远不会被夸大,因此具有空textView引用。

另一个问题是,在您的onCreate活动中,片段尚未被夸大,因此除非您延迟通话,否则textView将为空:

final MyFragment fragment = (MyFragment) mPagerAdapter.getItem(0);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        fragment.setText("Altered");
    }
}, 100);

但实际上,为什么不在初始化时将其设置为正确的值?

答案 1 :(得分:0)

试试这个 片段:

public class MyFragment extends Fragment {

private TextView textView;
private String Text;
public MyFragment (String Text){
    this.Text=Text;
}

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

    textView = (TextView) rootView.findViewById(R.id.textView);
    textView.setText(Text); 
    return rootView;
}

}

适配器:

public class ViewPagerAdapter extends FragmentPagerAdapter {

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
    public Fragment getItem(int position) {
    return new MyFragment("Altered");
}

@Override
    public int getCount() {
    return 3;
}
}

活动onCreate:

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

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
}