如何访问片段的父活动内的碎片的孩子的意见? [英] How to access Fragment's child views inside fragment's parent Activity?

查看:93
本文介绍了如何访问片段的父活动内的碎片的孩子的意见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个支持的片段活动将加载差异片段。该片段有一些的TextView ID =分数,我想获得它的句柄,但 findViewById 为得分的的TextView 返回null。为什么会这样?


TextView的放置在片段

 公共类MyActivity延伸扩展ActionBarActivity
        实现NavigationDrawerFragment.NavigationDrawerCallbacks {

   私人TextView的scoreBoardTextView = NULL;

   保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
     的setContentView(R.layout.activity_home);
     mNavigationDrawerFragment =(NavigationDrawerFragment)
                。getSupportFragmentManager()findFragmentById(R.id.navigation_drawer);
     scoreBoardTextView =(TextView中)findViewById(R.id.score); //这个返回null
  }

    @覆盖
    公共无效onNavigationDrawerItemSelected(INT位置){
      //设置片段
    }

}
 

解决方案

如果您要访问的的TextView 片段其父在活动,那么你应该定义你的片段类中的方法是这样的:

MyFragment.java

 公共类MyFragment扩展片段{

    TextView的mTextView;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.activity_main,集装箱,假);
        mTextView =(TextView中)view.findViewById(R.id.textView1);
        返回查看;
    }

    公共无效setTextViewText(字符串值){
        mTextView.setText(值);
    }


}
 

现在你可以使用这个你的活动里面是这样的:

  myFragment.setTextViewText(富);
 

在这里myFragment的类型是 MyFragment

如果您要访问的整个的TextView ,那么你可以定义在 MyFragment.java 这样的方法:

 公开的TextView getTextView1(){
    返回mTextView;
}
 

通过这一点,你可以访问的TextView 本身。

希望这有助于。 :)

I have a supported fragment activity which will load diff fragments. The fragment has some textView with id = "score" and I want to get its handle but findViewById for score's textView returns null. Why so?


textView is placed in fragment

public class MyActivity extends  extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks{

   private TextView scoreBoardTextView = null;

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_home);
     mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
     scoreBoardTextView = (TextView) findViewById(R.id.score); //this returns null
  }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
      //set fragment    
    }

}

解决方案

If you want to access the TextView of Fragment inside its parent Activity then you should define a method inside your Fragment class like this:

MyFragment.java

public class MyFragment extends Fragment {

    TextView mTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        mTextView = (TextView) view.findViewById(R.id.textView1);
        return view;
    }

    public void setTextViewText(String value){
        mTextView.setText(value);
    }


}

Now you can use this inside your Activity like this:

myFragment.setTextViewText("foo");

here myFragment is of type MyFragment.

If you want to access the whole TextView then you can define a method like this inside MyFragment.java:

public TextView getTextView1(){
    return mTextView;
}

By this you can access the TextView itself.

Hope this Helps. :)

这篇关于如何访问片段的父活动内的碎片的孩子的意见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆