简体   繁体   English

在ViewPager中更改片段的文本

[英]Changing the text of the Fragment in the ViewPager

@Override
public Fragment getItem(int position) {
    CharacterFragment fragment = new CharacterFragment();
    View rootView = fragment.getView();
    TextView character = (TextView) rootView.findViewById(R.id.character);
    character.setText(name[position]);
    return fragment;
}  

This is my code for changing the fragment in the ViewPager . 这是我用于更改ViewPager的片段的ViewPager The fragment just has a single text view. 该片段只有一个文本视图。 basically, I am just siwping through the alphabets in my name. 基本上,我只是在浏览我名字中的字母。 So, depending on the index, I have to set the text in the TextView of the fragment. 因此,根据索引,我必须在片段的TextView中设置文本。

With the above code, the programs blows with a NullPointerException because the layout has not been inflated yet, so I presume. 使用上面的代码,由于布局尚未膨胀,程序会爆裂NullPointerException ,所以我想。

What would be the right way to change the content of the Fragment ? 更改Fragment内容的正确方法是什么? Is there a callback method that would let me know that it has become visible ? 是否有一个回调方法让我知道它已变得可见?

There is no callback. 没有回调。 You could send the text as a parameter to the fragment when you're creating it and set the text inside of the fragment. 您可以在创建片段时将文本作为参数发送给片段,并在片段内设置文本。

Something like: 就像是:

@Override
public Fragment getItem(int position) {
    CharacterFragment fragment = CharacterFragment.newInstance(name[position]);
    View rootView = fragment.getView();
    return fragment;
}

your CharacterFragment.newInstance(String name) method would look like: 您的CharacterFragment.newInstance(String name)方法将类似于:

public static CharacterFragment.newInstance(String name) {
    CharacterFragment fragment = new CharacterFragment();
    Bundle args = new Bundle();
    args.put("NAME_ARG", name);
    fragment.setArguments(args);
    return fragment;
}

then in your onCreateView() you get the arguments through getArguments() method and you get the string with key NAME_ARG . 然后在您的onCreateView() ,通过getArguments()方法获取参数,并使用键NAME_ARG获取字符串。 And you have it! 而你拥有了! Makes sense? 说得通?

That's not the correct way to use getView() . 那不是使用getView()的正确方法。 The way to accomplish what you want is a little different. 完成所需内容的方式有所不同。 In order to do so you should pass the string (in this case name[position] ) to the method. 为此,您应该将字符串(在本例中为name[position] )传递给方法。 But you should remember that Fragments are not supposed to be instantiated with their constructor, instead create a static method, I'll show you: 但是您应该记住,片段不应该使用其构造函数实例化,而是创建一个静态方法,我将向您展示:

@Override
public Fragment getItem(int position) {
    CharacterFragment fragment = CharacterFragment.newInstance(name[position]);
    return fragment;
}  

And then, inside CharacterFragment.java 然后,在CharacterFragment.java

public static CharacterFragment newInstance(String name) {
    Bundle bundle = new Bundle();
    bundle.putString("key_name",name);

    CharacterFragment fragment = new CharacterFragment();
    CharacterFragment.setsetArguments(bundle);

    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

     View view = inflater.inflate(R.layout.fragment_xml_file, container, false);

     // AND HERE WE GO
     String name = getArguments().getString("key_name");
     TextView character= view.findViewById(R.id.character);
     character.setText(name);

     return view;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM