gpt4 book ai didi

java - 无法从 FragmentActivity 更改 Fragment 内的 TextView 值

转载 作者:行者123 更新时间:2023-12-01 04:19:43 24 4
gpt4 key购买 nike

我正在开发一个基于 viewPager 的简单应用程序。我正在尝试更改 mainActivity 中 fragment 内的 textView 中的文本。我尝试在 init() 方法(实例化 fragment 的地方)之后立即调用 textV.setText(String),但 OnCreateView 的执行时间要晚得多,因此此时对 textV 的引用为 null。 textV textView放置在tab0layout.xml中,当布局文​​件inflate时变为!= null,并在Fragment0的OnCreateView方法中引用。如何在 MainActivity 中获取 textV 的有效引用?从 OnCreateView 中设置文本效果很好,但我需要它在 mainActivity 中可用。

这是整个 java 文件:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

SectionsPagerAdapter mSectionsPagerAdapter;

ViewPager mViewPager;


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

init();//fragment is instantiated

Fragment0.textV.setText("new text");//doesn't work, now textV is null!!
}

private void init() {

List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment0.class.getName()));

this.mSectionsPagerAdapter = new SectionsPagerAdapter(super.getSupportFragmentManager(), fragments);
mViewPager = (ViewPager) super.findViewById(R.id.pager);
mViewPager.setAdapter(this.mSectionsPagerAdapter);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;

public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}

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

/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size();
}
}


public static class Fragment0 extends Fragment {

static TextView textV;

public Fragment0() {

}

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

textV = (TextView) rootView.findViewById(R.id.text);

return rootView;
}
}

}

最佳答案

您可以在 Fragment 中创建公共(public)方法,并通过这种方式从 Activity 获取/设置 TextView

您可以通过以下方式获取当前 fragment :

FragmentManager fm = this.getSupportFragmentManager();
YourFragment fragment = (YourFragment ) fm.getFragments().get(mViewPager.getCurrentItem());

关于java - 无法从 FragmentActivity 更改 Fragment 内的 TextView 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19059434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com