<tfoot id='7SQGU'></tfoot>
  • <i id='7SQGU'><tr id='7SQGU'><dt id='7SQGU'><q id='7SQGU'><span id='7SQGU'><b id='7SQGU'><form id='7SQGU'><ins id='7SQGU'></ins><ul id='7SQGU'></ul><sub id='7SQGU'></sub></form><legend id='7SQGU'></legend><bdo id='7SQGU'><pre id='7SQGU'><center id='7SQGU'></center></pre></bdo></b><th id='7SQGU'></th></span></q></dt></tr></i><div id='7SQGU'><tfoot id='7SQGU'></tfoot><dl id='7SQGU'><fieldset id='7SQGU'></fieldset></dl></div>

    <small id='7SQGU'></small><noframes id='7SQGU'>

        <bdo id='7SQGU'></bdo><ul id='7SQGU'></ul>
      <legend id='7SQGU'><style id='7SQGU'><dir id='7SQGU'><q id='7SQGU'></q></dir></style></legend>

        如何从活动(viewpager)更新片段内容?

        时间:2023-10-04
        <tfoot id='nmbIW'></tfoot>
      1. <small id='nmbIW'></small><noframes id='nmbIW'>

      2. <legend id='nmbIW'><style id='nmbIW'><dir id='nmbIW'><q id='nmbIW'></q></dir></style></legend>

        1. <i id='nmbIW'><tr id='nmbIW'><dt id='nmbIW'><q id='nmbIW'><span id='nmbIW'><b id='nmbIW'><form id='nmbIW'><ins id='nmbIW'></ins><ul id='nmbIW'></ul><sub id='nmbIW'></sub></form><legend id='nmbIW'></legend><bdo id='nmbIW'><pre id='nmbIW'><center id='nmbIW'></center></pre></bdo></b><th id='nmbIW'></th></span></q></dt></tr></i><div id='nmbIW'><tfoot id='nmbIW'></tfoot><dl id='nmbIW'><fieldset id='nmbIW'></fieldset></dl></div>

            <tbody id='nmbIW'></tbody>
            • <bdo id='nmbIW'></bdo><ul id='nmbIW'></ul>

                  本文介绍了如何从活动(viewpager)更新片段内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个带有两个片段的简单 viewPager.每个片段都包含一个 TextView.我希望能够更新活动中显示的当前 textView 的文本.

                  I have a simple viewPager with two fragments. Each fragment contains a TextView. I would like to be able to update the text of the current textView displayed from the activity.

                  活动:

                  public class MainActivity extends FragmentActivity {
                  
                      @Override
                      protected void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.main_activity);
                  
                          ArrayList<Fragment> fragments = new ArrayList<Fragment>();
                          fragments.add(Fragment.instantiate(this, Live.class.getName()));
                          fragments.add(Fragment.instantiate(this, Hit.class.getName()));
                  
                          this.pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),fragments);
                  
                          ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
                          pager.setAdapter(pagerAdapter);
                      }
                  }
                  

                  适配器:

                  public class MyPagerAdapter extends FragmentPagerAdapter {
                  
                      private final ArrayList<Fragment> fragments;
                  
                      public MyPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments)
                      {
                          super(fm);
                          this.fragments = fragments;
                      }
                  
                      @Override
                      public Fragment getItem(int position) {
                          return this.fragments.get(position);
                      }
                  
                      @Override
                      public int getCount() {
                          return this.fragments.size();
                      }
                  }
                  

                  片段:

                  public class Live extends Fragment {
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                          return inflater.inflate(R.layout.live, container,false);
                      }
                  }
                  

                  XML 片段:

                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent">
                      <TextView android:layout_height="fill_parent"
                                android:layout_width="fill_parent"
                                android:text="First Fragment"
                                android:id="@+id/status"/>
                  </LinearLayout>
                  

                  每个片段都有一个 ID 为status"的 TextView.在 onCreateView 中更新文本视图有效.但是我怎样才能从活动中的任何地方更新 textView 呢?

                  Each fragment have a TextView with the id "status". Updating the text view in onCreateView works. But how can i update the textView from anywhere in the activity ?

                  我试过这个没有成功:

                  片段:

                  public class Live extends Fragment {    
                      public TextView tv;
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                          View view = inflater.inflate(R.layout.live, container,false);
                          tv = (TextView) view.findViewById(R.id.status);
                  
                          return view;
                      }
                  }
                  

                  活动:

                  @Override
                      protected void onResume() {
                          super.onResume();
                          Live frag = (Live) pagerAdapter.getItem(0);
                          frag.tv.setText("Test 2");
                      }
                  

                  但它在 tv 上给了我一个 nullPointerException,可能是因为片段尚未膨胀.

                  But it gives me a nullPointerException on tv, probably because the fragment is not yet inflated.

                  我已经阅读了以下问题但没有成功:

                  I already read the following questions without success :

                  • 从 ViewPager 中的 Activity 更新片段
                  • 从另一个 Fragment 更新 ViewPager 中的 TextView

                  推荐答案

                  在你的 MyPagerAdapter 类中,Android 仅在它想要创建时调用 getItem(...)新片段.所以在那里使用引用是有问题的,而不是你应该实例化片段.

                  In your MyPagerAdapter class, Android calls getItem(...) only if it wants to create new Fragments. So it's problematic to use references there instead you should instatiate the fragments.

                  检查此答案support-fragmentpageradapter-holds-reference-to-old-fragments

                  这篇关于如何从活动(viewpager)更新片段内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:片段没有从内存中释放 下一篇:在 iPhone X 系列上处理 Chrome 浏览器中的安全区域

                  相关文章

                  <i id='uMQu8'><tr id='uMQu8'><dt id='uMQu8'><q id='uMQu8'><span id='uMQu8'><b id='uMQu8'><form id='uMQu8'><ins id='uMQu8'></ins><ul id='uMQu8'></ul><sub id='uMQu8'></sub></form><legend id='uMQu8'></legend><bdo id='uMQu8'><pre id='uMQu8'><center id='uMQu8'></center></pre></bdo></b><th id='uMQu8'></th></span></q></dt></tr></i><div id='uMQu8'><tfoot id='uMQu8'></tfoot><dl id='uMQu8'><fieldset id='uMQu8'></fieldset></dl></div>
                    <tfoot id='uMQu8'></tfoot>
                  1. <legend id='uMQu8'><style id='uMQu8'><dir id='uMQu8'><q id='uMQu8'></q></dir></style></legend>
                      • <bdo id='uMQu8'></bdo><ul id='uMQu8'></ul>

                      <small id='uMQu8'></small><noframes id='uMQu8'>