繁体   English   中英

Android中的片段导航

[英]Fragments Navigation in android

Hai我是android中这个片段开发的新手。 我的问题是片段之间的导航。 我在片段1中有一个按钮,在片段2中有一个文本视图,我有活动activity1,其中我在xml中声明了这两个片段。 我的问题是,当我按fragment1中的按钮时,它必须携带一些文本并显示在fragment2的文本视图中,然后才能检查fragment2是否具有相同的布局?

代码对我会非常有帮助。 提前致谢..........

首先,将充气机声明为onCreateView(在2ndFragmentClass中),如下所示:

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

    return view;
}

请考虑fragment_content必须至少在其内部具有TextView(以便我们在片段内部设置其值)。 然后,我们必须从第一个片段更改此文本的值。 因此,我们将此构造器添加到第二个片段(包含TextView的片段)中,如下所示:

public void setText(String name) {
    TextView txt= (TextView ) getView().findViewById(R.id.textView1);
    txt.setNewText(name);
  }

简而言之,如下所示:

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

    return view;
}
public void setText(String name) {
    TextView txt= (TextView ) getView().findViewById(R.id.textView1);
    txt.setNewText(name);
  }

然后,我们必须定义必须从1stFragmentClass设置为第二个片段的文本。 然后我们按如下所示在第一个片段中按下按钮来设置第二个片段的文本:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    String url = "Hello, This is the text from 1st Fragment:)";
         //Here we try to declare 2nd fragment.
    2ndFragmentClass fragment = (2ndFragmentClass) getFragmentManager()
            .findFragmentById(R.id.detailFragment);
        fragment.setNewText(url);
}

暂无
暂无

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

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