如何从同一个Activity中调用Fragment中的public void

时间:2015-09-02 13:02:20

标签: java android android-fragments

我想在操作栏按钮中单击更改我的片段页面textView文本。

我正在使用Activity和Fragment。在此活动中

如何在同一个Activity中调用Fragment中的public void?谢谢大家〜

代码

  

MainActivity.java

public class MainActivity extends ActionBarActivity{

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setElevation(0);
        }

        MaterialTabHost tabHost = (MaterialTabHost) findViewById(android.R.id.tabhost);
        tabHost.setType(MaterialTabHost.Type.FullScreenWidth);

        tabHost.addTab("one page");
        tabHost.addTab("two page");

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setOnPageChangeListener(tabHost);

        tabHost.setOnTabChangeListener(new MaterialTabHost.OnTabChangeListener() {
            @Override
            public void onTabSelected(int position) {
                viewPager.setCurrentItem(position);
            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.name:
                //in here call  public void setItem, set the textView Text.
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment{
        private static final String ARG_SECTION_NUMBER = "section_number";
        TextView textView;

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

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

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

        public void setItem() {
            textView.setText("Test");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要引用该片段,您可以通过FragmentManager获取该片段:

如果使用<fragment>标记将片段添加到xml文件中的布局,请定义其ID:

<fragment android:id="@+id/placeholder_fragment"
    android:name="com.your.package.PlaceholderFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

在您的活动中:

FragmentManager fm = getSupportFragmentManager();
PlaceholderFragment placeholderFragment = (PlaceholderFragment) fm.findFragmentById(R.id.placeholder_fragment);
placeholderFragment.setItem();

添加片段的其他方法是以编程方式执行:

private static final String TAG_PLACEHOLDER = "placeholder";

FragmentManager fm = getSupportFragmentManager();
placeholderFragment = (DataLoaderFragment)fm.findFragmentByTag(TAG_PLACEHOLDER);
if (placeholderFragment == null) {
    placeholderFragment = PlaceholderFragment.newInstance(sectionNumber);
    fm.beginTransaction().add(placeholderFragment, TAG_PLACEHOLDER).commit();
}
// you have the reference to fragment from calling newInstance(sectionNumber) or findFragmentByTag, so you can execute the method:
placeholderFragment.setItem();

答案 1 :(得分:0)

我认为最佳做法应该是在片段中创建选项菜单及其功能。

但是如果你有什么理由在活动中设置它,那么你可以使用这样的东西:

MEAN.IO

取决于您将片段添加到Backstack的方式。