我需要在Fragment中将文本值设置为EditText
。在我的活动中,ViewPager
添加了5个此类片段。为了做到这一点,我需要引用ViewPager
内的片段。我使用了ViewPager
适配器。
public class MessageViewPageAdapter extends FragmentPagerAdapter {
private List<Fragment> pages;
private HashMap<Integer,Fragment> fragmentReference;
public MessageViewPageAdapter(FragmentManager fm, List<Fragment> pages) {
super(fm);
this.pages = pages;
fragmentReference = new HashMap<>();
}
@Override
public Fragment getItem(int position) {
Fragment fragment = pages.get(position);
fragmentReference.put(position,fragment);
return pages.get(position);
}
@Override
public int getCount() {
return pages.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
fragmentReference.remove(position);
}
public Fragment getFragment(int key) {
return fragmentReference.get(key);
}
}
正如您所见,fragmentReference HashMap用于存储ViewPager
内的所有片段。 getFragment(int key)
方法用于从该哈希映射中获取片段。这就是我的活动的样子。
public class MainActivity extends AppCompatActivity {
private ViewPager messageViewPage;
private Fragment message1;
private Fragment message2;
private Fragment message3;
private Fragment message4;
private Fragment message5;
private List<Fragment> fragments = new ArrayList<>();
private MessageViewPageAdapter messageViewPageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageViewPage = (ViewPager) findViewById(R.id.message_viewPage);
message1 = new Message1();
message2 = new Message2();
message3 = new Message3();
message4 = new Message4();
message5 = new Message5();
fragments.add(message1);
fragments.add(message2);
fragments.add(message3);
fragments.add(message4);
fragments.add(message5);
messageViewPageAdapter = new MessageViewPageAdapter(getSupportFragmentManager(), fragments);
messageViewPage.setAdapter(messageViewPageAdapter);
messageViewPage.setOffscreenPageLimit(5);
Message1 message1fragment = (Message1) messageViewPageAdapter.getFragment(0);
Message2 message2fragment = (Message2) messageViewPageAdapter.getFragment(1);
Message3 message3fragment = (Message3) messageViewPageAdapter.getFragment(2);
Message4 message4fragment = (Message4) messageViewPageAdapter.getFragment(3);
Message5 message5fragment = (Message5) messageViewPageAdapter.getFragment(4);
}
但是我为所有5个片段(message1fragment,message2fragment,message3fragment,message4fragment,message5fragment)
但是当我在下面的点击按钮中放入相同的代码时,它的工作正常。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message1 message1fragment = (Message1) messageViewPageAdapter.getFragment(0);
Message2 message2fragment = (Message2) messageViewPageAdapter.getFragment(1);
Message3 message3fragment = (Message3) messageViewPageAdapter.getFragment(2);
Message4 message4fragment = (Message4) messageViewPageAdapter.getFragment(3);
Message5 message5fragment = (Message5) messageViewPageAdapter.getFragment(4);
}
});
我做错了什么?我甚至尝试在onStart()
和onResume()
方法中放置相同的代码,但结果相同。此ViewPager
中的每个片段都有一个名为setMessage(String message)
的方法,我打算用它将文本设置为该片段内的EditText
。我需要在活动开始时这样做。
这是我的消息片段的样子。
public class Message1 extends Fragment {
private EditText message1;
public Message1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_message1, container, false);
message1 = (EditText) inflate.findViewById(R.id.message_1);
return inflate;
}
public String getMessageOneText(){
return String.valueOf(message1.getText());
}
public void setErrorMessage(){
message1.setError("Message 1 can not be empty");
}
public void setMessage(String message){
message1.setText(message);
}
答案 0 :(得分:0)
在onCreate中,ViewPager不会被完全初始化,结果是不会调用getItem(...),因此你的getFragment(...)实现将返回null。
我建议用以下内容替换getFragment:
public Fragment getFragment(int key) {
return pages.get(key);
}
并删除fragmentReference,因为它似乎是多余的。