繁体   English   中英

无法从FragmentActivity更改片段内的TextView值

[英]Cannot Change TextView Value Inside Fragment From FragmentActivity

我正在开发一个基于viewPager的简单应用程序。 我正在尝试从mainActivity的片段中更改textView中的文本。 我试图在init()方法(实例化片段的位置)之后立即调用textV.setText(String),但是OnCreateView的执行要晚得多,因此在那一刻对textV的引用为null。 textV textView放置在tab0layout.xml中,当布局文​​件膨胀时变为!= 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创建公共方法,并通过活动从中获取/设置TextView

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

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

您应该能够处理onActivityCreated ,然后使用FragmentManager获取参考,您可以在其中根据需要设置/获取文本。 一旦Fragment活动实例化并准备就绪,将调用此方法。 如果需要,您可以在以后存储参考使用。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Fragment fragment = (Fragment)(getSupportFragmentManager().findFragmentById(R.id.thefragmentId));

}

当主要活动的创建函数返回时,然后片段管理器调用为所有附加片段创建的活动。 那时创建了片段视图。 因此,您需要做的是在活动恢复时输入文字。 您想要做的事情就是在片段上调用将文本设置为字符串变量的函数,然后在片段的onresume函数中调用该值

暂无
暂无

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

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