本文介绍了Android-如何管理具有不同内容的单个片段的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我希望能够设置各个片段视图的setText和getText.现在,当我对Framgent的TextView进行setText设置时,它会在所有片段中更改该视图的文本.

I want to be able to setText and getText of Views of individual Fragments. As it is now, when I setText of a Framgent's TextView it changes the text of that View in all Fragments.

我一直在尝试通过移动一些东西,但是到目前为止,这是我的代码:

I've been experimenting by moving things around, but here is my code as of this moment:

片段类

public class TestFragment extends Fragment{

    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.test_fragment, container, false);
        TextView tv = (TextView) view.findViewById(R.id.huh);
        //tv.setText("AAAAAAAAAAAAAAAAAAA");

        return view;
    }

    public void setText(String asdf) {
        TextView test = (TextView) view.findViewById(R.id.huh);
        test.setText(asdf);
    }


}

活动分类

    public class Manage extends BaseActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manage);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        TestFragment fragment = new TestFragment();
        //fragment.setText("ASDF");
        fragmentTransaction.add(R.id.test_fragment, fragment, "testtag");
        fragmentTransaction.commit();
    }
}

framgent.xml很简单;只是一个TextView.

The framgent.xml is pretty plain; just a single TextView.

推荐答案

使用名为tag的参数将片段添加到堆栈中.在您的情况下,您已将片段添加为"testtag".

Fragments are added to stack with a parameter named tag. In your case you've added your fragment with "testtag".

fragmentTransaction.add(R.id.test_fragment, fragment, "testtag");

如果您创建相同片段的多个实例,并为其添加唯一标签,则可以使用该唯一标签获取它们.当您得到一个片段时,您就可以达到其内容.

If you create multiple instances of same fragment and add them with unique tags, then you are able to get them with that unique tags. When you get a fragment then you can reach its content.

FragmentManager fm = this.getSupportFragmentManager();
Fragment testtagFragment = fm.findFragmentByTag("testtag");
View targetView = testtagFragment.getView().findViewById(R.id.anyViewInsideContentOfYourFragment);

此问题分为2部分.

  1. 要在初始化时使用setText,您必须传递您的首字母创建其实例时为片段添加参数.我建议为此,您可以使用静态newInstance方法. 查看示例在这里
  2. 要获取Text,请阅读上面的答案.请注意,您可以在执行片段的onCreateView方法后获取片段的内容.因此,如果您尝试在活动的onCreate方法上调用片段的getView方法(在添加片段之后),那么它将返回null.您可以在click事件下成功获取其内容以对其进行测试,并对该片段的内容使用任何视图的get或set操作.
  1. To setText while initializing you have to pass your initialparameters to your fragment while creating its instance. I suggestyou to use a static newInstance method for this. See samplehere
  2. To getText read my answer above. Note that, you can get the content of a fragment after its onCreateView method is executed. So If you try to call getView method of a fragment at your activities onCreate method (after you add the fragment), that will return null. You can get its content successfully under a click event to test that, and use get or set operations of any view on that fragment's content.

这篇关于Android-如何管理具有不同内容的单个片段的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:00