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

      <bdo id='dQKtB'></bdo><ul id='dQKtB'></ul>

      1. <tfoot id='dQKtB'></tfoot>

      2. <small id='dQKtB'></small><noframes id='dQKtB'>

        <legend id='dQKtB'><style id='dQKtB'><dir id='dQKtB'><q id='dQKtB'></q></dir></style></legend>

        从另一个 Fragment 更新 ViewPager 中的 TextView

        时间:2023-10-04
          <tbody id='9YNXo'></tbody>

          1. <small id='9YNXo'></small><noframes id='9YNXo'>

              <legend id='9YNXo'><style id='9YNXo'><dir id='9YNXo'><q id='9YNXo'></q></dir></style></legend>
                <i id='9YNXo'><tr id='9YNXo'><dt id='9YNXo'><q id='9YNXo'><span id='9YNXo'><b id='9YNXo'><form id='9YNXo'><ins id='9YNXo'></ins><ul id='9YNXo'></ul><sub id='9YNXo'></sub></form><legend id='9YNXo'></legend><bdo id='9YNXo'><pre id='9YNXo'><center id='9YNXo'></center></pre></bdo></b><th id='9YNXo'></th></span></q></dt></tr></i><div id='9YNXo'><tfoot id='9YNXo'></tfoot><dl id='9YNXo'><fieldset id='9YNXo'></fieldset></dl></div>
                <tfoot id='9YNXo'></tfoot>
                • <bdo id='9YNXo'></bdo><ul id='9YNXo'></ul>
                  本文介绍了从另一个 Fragment 更新 ViewPager 中的 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试通过单击另一个 Fragment 上的按钮来更新 Fragment 中的 TextView.

                  I am trying to update a TextView in a Fragment clicking on a button on another Fragment.

                  实际上我实现了 Activity 的回调函数并且它确实有效,因为 Logcat 报告 TextView 中的文本已更改.问题是第一个片段中显示的 Textview 没有更新为新值!这就像 Fragment 需要更新什么的......

                  Actually I implemented the callback function to the Activity and it does work, since the Logcat reports that the text in the TextView has been changed. The problem is the Textview shown in the first fragment is not updated to the new value! It's like the Fragment needs to be updated or something...

                  这是 ActionBarTabsPager 活动代码:

                  Here's the ActionBarTabsPager activity code:

                  import java.util.ArrayList;
                  import android.app.Activity;
                  import android.app.FragmentManager;
                  import android.app.FragmentManager.OnBackStackChangedListener;
                  import android.content.Context;
                  import android.os.Build;
                  import android.os.Bundle;
                  import android.support.v4.app.ActionBar;
                  import android.support.v4.app.Fragment;
                  import android.support.v4.app.FragmentActivity;
                  import android.support.v4.app.FragmentPagerAdapter;
                  import android.support.v4.app.FragmentTransaction;
                  import android.support.v4.app.ActionBar.Tab;
                  import android.support.v4.app.SupportActivity;
                  import android.support.v4.view.ViewPager;
                  import android.util.Log;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.ViewGroup;
                  import android.view.View.OnClickListener;
                  import android.widget.TextView;
                  
                  /**
                   * Demonstrates combining the action bar with a ViewPager to implement a tab UI
                   * that switches between tabs and also allows the user to perform horizontal
                   * flicks to move between the tabs.
                   */
                  public class ActionBarTabsPager extends FragmentActivity implements SecondFragment.OnButtonClickedXListener{
                      ViewPager  mViewPager;
                      TabsAdapter mTabsAdapter;
                      FragmentManager fm;
                  
                  
                  
                      @Override
                      protected void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                  
                          setContentView(R.layout.actionbar_tabs_pager);
                  
                  
                  
                          if (savedInstanceState == null) {
                               Fragment newFragment = new FirstFragment();
                               FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                               ft.add(R.id.abs__custom, newFragment, "FirstFragment").commit();
                  
                               Fragment newFragment2 = new FirstFragment();
                               FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
                               ft2.add(R.id.abs__custom, newFragment2, "SecondFragment").commit();
                          }
                          getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                  
                          ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Fragment 1");
                          ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Fragment 2");
                          //ActionBar.Tab tab3 = getSupportActionBar().newTab().setText("Fragment 1");
                          //ActionBar.Tab tab4 = getSupportActionBar().newTab().setText("Fragment 2");
                  
                          mViewPager = (ViewPager)findViewById(R.id.pager);
                          mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager);
                  
                              mTabsAdapter.addTab(tab1, FirstFragment.class);
                              mTabsAdapter.addTab(tab2, SecondFragment.class);//LoaderCursorSupport.CursorLoaderListFragment.class);
                              //mTabsAdapter.addTab(tab3, FirstFragment.class);//LoaderCustomSupport.AppListFragment.class);
                              //mTabsAdapter.addTab(tab4, SecondFragment.class);//LoaderThrottleSupport.ThrottledLoaderListFragment.class);
                  
                  
                          if (savedInstanceState != null) {
                              getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("index"));
                          }
                  
                  
                  
                      }
                  
                      @Override
                      protected void onSaveInstanceState(Bundle outState) {
                          super.onSaveInstanceState(outState);
                          outState.putInt("index", getSupportActionBar().getSelectedNavigationIndex());
                      }
                  
                      /**
                       * This is a helper class that implements the management of tabs and all
                       * details of connecting a ViewPager with associated TabHost.  It relies on a
                       * trick.  Normally a tab host has a simple API for supplying a View or
                       * Intent that each tab will show.  This is not sufficient for switching
                       * between pages.  So instead we make the content part of the tab host
                       * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
                       * view to show as the tab content.  It listens to changes in tabs, and takes
                       * care of switch to the correct paged in the ViewPager whenever the selected
                       * tab changes.
                       */
                      public class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
                          private final Context mContext;
                          private final ActionBar mActionBar;
                          private final ViewPager mViewPager;
                          private final ArrayList<String> mTabs = new ArrayList<String>();
                  
                          public TabsAdapter(FragmentActivity activity, ActionBar actionBar, ViewPager pager) {
                              super(activity.getSupportFragmentManager());
                              mContext = activity;
                              mActionBar = actionBar;
                              mViewPager = pager;
                              mViewPager.setAdapter(this);
                              mViewPager.setOnPageChangeListener(this);
                          }
                  
                          public void addTab(ActionBar.Tab tab, Class<?> clss) {
                              mTabs.add(clss.getName());
                              mActionBar.addTab(tab.setTabListener(this));
                              notifyDataSetChanged();
                          }
                  
                          @Override
                          public int getCount() {
                              return mTabs.size();
                          }
                  
                          @Override
                          public Fragment getItem(int position) {
                              return Fragment.instantiate(mContext, mTabs.get(position), null);
                          }
                  
                  
                          @Override
                          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                          }
                  
                          @Override
                          public void onPageSelected(int position) {
                              mActionBar.setSelectedNavigationItem(position);
                          }
                  
                          @Override
                          public void onPageScrollStateChanged(int state) {
                          }
                  
                          @Override
                          public void onTabSelected(Tab tab, FragmentTransaction ft) {
                              mViewPager.setCurrentItem(tab.getPosition());
                  
                          }
                  
                          @Override
                          public void onTabReselected(Tab tab, FragmentTransaction ft) {
                          }
                  
                          @Override
                          public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                          }
                      }
                  
                      @Override
                      public void OnButtonClickedX(View v) {
                  
                          if (v==findViewById(R.id.button1)){
                              Log.i("TRIGGERED","TRIGGERED");
                  
                              FirstFragment ff = (FirstFragment) getSupportFragmentManager().findFragmentByTag("FirstFragment");
                  
                              View root = ff.getView();
                              TextView tv = (TextView) root.findViewById(R.id.textView1);
                              Log.i("Text before Edit",""+tv.getText());
                              tv.setText("MODIFIED");
                              Log.i("Text after Edit",""+tv.getText());
                  
                  
                  
                  
                          }
                          // TODO Auto-generated method stub
                  
                      }
                  
                  
                  
                  
                  
                  }
                  

                  第一个片段:

                  import android.os.Bundle;
                  import android.support.v4.app.Fragment;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.ViewGroup;
                  
                  public class FirstFragment extends Fragment {
                      int mNum;
                  
                  
                      /**
                       * Create a new instance of FirstFragment, providing "num"
                       * as an argument.
                       */
                      static FirstFragment newInstance(int num) {
                  
                          FirstFragment f = new FirstFragment();
                  
                          // Supply num input as an argument.
                          Bundle args = new Bundle();
                          args.putInt("num", num);
                          f.setArguments(args);
                  
                          return f;
                      }
                  
                      /**
                       * When creating, retrieve this instance's number from its arguments.
                       */
                      @Override
                      public void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          mNum = getArguments() != null ? getArguments().getInt("num") : 1;
                      }
                  
                      /**
                       * The Fragment's UI is just a simple text view showing its
                       * instance number.
                       */
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                          View v = inflater.inflate(R.layout.myfrag1, container, false);
                          return v;
                      }
                  
                  
                  
                  
                  }
                  

                  SecondFragment:

                  import android.os.Bundle;
                  import android.support.v4.app.Fragment;
                  import android.support.v4.app.SupportActivity;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.View.OnClickListener;
                  import android.view.ViewGroup;
                  
                  public  class SecondFragment extends Fragment {
                      int mNum;
                      OnButtonClickedXListener mListener;
                  
                      /**
                       * Create a new instance of CountingFragment, providing "num"
                       * as an argument.
                       */
                      static SecondFragment newInstance(int num) {
                          SecondFragment f = new SecondFragment();
                  
                          // Supply num input as an argument.
                          Bundle args = new Bundle();
                          args.putInt("num", num);
                          f.setArguments(args);
                  
                          return f;
                      }
                  
                      /**
                       * When creating, retrieve this instance's number from its arguments.
                       */
                      @Override
                      public void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          mNum = getArguments() != null ? getArguments().getInt("num") : 1;
                      }
                  
                      /**
                       * The Fragment's UI is just a simple text view showing its
                       * instance number.
                       */
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                          View v = inflater.inflate(R.layout.myfrag2, container, false);
                          View button1 = v.findViewById(R.id.button1);
                          button1.setOnClickListener(new OnClickListener() {
                  
                              @Override
                              public void onClick(View v) {
                                  mListener.OnButtonClickedX(v);
                                  // TODO Auto-generated method stub
                  
                              }
                          });
                  
                  
                  
                          return v;
                      }
                  
                      public interface OnButtonClickedXListener{
                          public void OnButtonClickedX(View v);
                      }
                  
                      @Override
                      public void onAttach(SupportActivity activity) {
                          // TODO Auto-generated method stub
                          super.onAttach(activity);
                          try {
                              mListener = (OnButtonClickedXListener) activity;
                          } catch (ClassCastException e) {
                              throw new ClassCastException(activity.toString() + " must implement OnButtonClickedXListener");
                          }
                      }
                  
                  }
                  

                  myfrag1.xml:

                  <?xml version="1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical" >
                  
                  
                  
                      <TextView
                          android:id="@+id/textView1"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"
                          android:gravity="center"
                          android:text="No String"
                          android:textAppearance="?android:attr/textAppearanceLarge" />
                  
                  </LinearLayout>
                  

                  myfrag2.xml:

                  <?xml version="1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:orientation="vertical" >
                  
                  
                      <Button
                          android:id="@+id/button1"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_gravity="center"
                          android:gravity="center"
                          android:text="Button" />
                  
                  </LinearLayout>
                  

                  即使将 ft.add() 中的 ContainerViewId 更改为 0,也不会影响最终渲染.所以我猜渲染是由

                  Even changing to 0 the ContainerViewId in ft.add() doesn't impact at all in final rendering. SO I guess the rendering is managed by

                  mTabsAdapter.addTab(tab1, FirstFragment.class);
                  mTabsAdapter.addTab(tab2, SecondFragment.class);
                  

                  无论如何,问题还是一样.

                  The issue is still the same, anyway.

                  这里是 actionbar_tabs_pager.xml

                  <?xml version="1.0" encoding="utf-8"?>
                  
                  <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/pager"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                  />
                  

                  推荐答案

                  已解决!

                  在 TabsAdapter 中覆盖 instantiateItem() 并将 ViewPager 添加为 FragmentTransaction 中的 ContainerViewID 成功了!

                  Overriding instantiateItem() in TabsAdapter and adding the ViewPager as ContainerViewID in the FragmentTransaction made it!

                  这是整个 FragmentActivity 的功能!

                  Here's the functioning whole FragmentActivity!

                  import java.util.ArrayList;
                  import android.app.Activity;
                  import android.app.FragmentManager;
                  import android.app.FragmentManager.OnBackStackChangedListener;
                  import android.content.Context;
                  import android.os.Build;
                  import android.os.Bundle;
                  import android.support.v4.app.ActionBar;
                  import android.support.v4.app.Fragment;
                  import android.support.v4.app.FragmentActivity;
                  import android.support.v4.app.FragmentPagerAdapter;
                  import android.support.v4.app.FragmentTransaction;
                  import android.support.v4.app.ActionBar.Tab;
                  import android.support.v4.app.SupportActivity;
                  import android.support.v4.view.ViewPager;
                  import android.util.Log;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.ViewGroup;
                  import android.view.View.OnClickListener;
                  import android.widget.TextView;
                  
                  /**
                   * Demonstrates combining the action bar with a ViewPager to implement a tab UI
                   * that switches between tabs and also allows the user to perform horizontal
                   * flicks to move between the tabs.
                   */
                  public class ActionBarTabsPager extends FragmentActivity implements SecondFragment.OnButtonClickedXListener{
                      ViewPager  mViewPager;
                      TabsAdapter mTabsAdapter;
                      FragmentManager fm;
                  
                  
                  
                      @Override
                      protected void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                  
                          Log.i("ONCREATE START","ONCREATE START");
                  
                  
                          setContentView(R.layout.actionbar_tabs_pager);
                  
                  
                          if (savedInstanceState == null) {
                  
                  
                              Fragment newFragment = new FirstFragment();
                              Fragment newFragment2 = new SecondFragment();
                  
                              FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                              ft.add(R.id.pager, newFragment, "FirstFragment");
                              ft.add(R.id.pager, newFragment2, "SecondFragment");
                              ft.commit();
                  
                  
                         }
                  
                          getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                  
                          ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Fragment 1");
                          ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Fragment 2");
                          //ActionBar.Tab tab3 = getSupportActionBar().newTab().setText("Fragment 1");
                          //ActionBar.Tab tab4 = getSupportActionBar().newTab().setText("Fragment 2");
                  
                          mViewPager = (ViewPager)findViewById(R.id.pager);
                          mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager);
                  
                              mTabsAdapter.addTab(tab1, FirstFragment.class);
                              mTabsAdapter.addTab(tab2, SecondFragment.class);//LoaderCursorSupport.CursorLoaderListFragment.class);
                              //mTabsAdapter.addTab(tab3, FirstFragment.class);//LoaderCustomSupport.AppListFragment.class);
                              //mTabsAdapter.addTab(tab4, SecondFragment.class);//LoaderThrottleSupport.ThrottledLoaderListFragment.class);
                  
                  
                  
                          if (savedInstanceState != null) {
                              getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("index"));
                          }
                  
                          Log.i("ONCREATE END","ONCREATE END");
                  
                      }
                  
                      @Override
                      protected void onSaveInstanceState(Bundle outState) {
                          Log.i("onSaveInstanceState START","onSaveInstanceState START");
                          super.onSaveInstanceState(outState);
                          outState.putInt("index", getSupportActionBar().getSelectedNavigationIndex());
                          Log.i("onSaveInstanceState END","onSaveInstanceState END");
                  
                      }
                  
                      /**
                       * This is a helper class that implements the management of tabs and all
                       * details of connecting a ViewPager with associated TabHost.  It relies on a
                       * trick.  Normally a tab host has a simple API for supplying a View or
                       * Intent that each tab will show.  This is not sufficient for switching
                       * between pages.  So instead we make the content part of the tab host
                       * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
                       * view to show as the tab content.  It listens to changes in tabs, and takes
                       * care of switch to the correct paged in the ViewPager whenever the selected
                       * tab changes.
                       */
                      public class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
                          private final Context mContext;
                          private final ActionBar mActionBar;
                          private final ViewPager mViewPager;
                          private final ArrayList<String> mTabs = new ArrayList<String>();
                          private FragmentTransaction mCurTransaction = null;
                  
                  
                          public TabsAdapter(FragmentActivity activity, ActionBar actionBar, ViewPager pager) {
                              super(activity.getSupportFragmentManager());
                              mContext = activity;
                              mActionBar = actionBar;
                              mViewPager = pager;
                              mViewPager.setAdapter(this);
                              mViewPager.setOnPageChangeListener(this);
                  
                  
                          }
                  
                          public void addTab(ActionBar.Tab tab, Class<?> clss) {
                              Log.i("addTab","addTab");
                  
                              mTabs.add(clss.getName());
                              mActionBar.addTab(tab.setTabListener(this));
                              notifyDataSetChanged();
                          }
                  
                          @Override
                          public int getCount() {
                              return mTabs.size();
                          }
                  
                          @Override
                          public Object instantiateItem(View container, int position) {
                              if (mCurTransaction == null) {
                                  mCurTransaction = getSupportFragmentManager().beginTransaction();
                              }
                              // TODO Auto-generated method stub
                              Fragment fragment = getItem(position);
                  
                              if (fragment!=null){
                                  Log.i("Fragment Found!","Fragment Found! "+fragment.getTag());
                                  mCurTransaction.attach(fragment);
                                  }
                  
                  
                              return fragment;//super.instantiateItem(container, position);
                          }
                  
                          @Override
                          public Fragment getItem(int position) {
                              Log.i("getItem","getItem");
                  
                              if (position==0)
                                  {Log.i("position=0","position=0");
                                  return getSupportFragmentManager().findFragmentByTag("FirstFragment");}
                  
                              else if (position==1)
                              {Log.i("position=1","position=1");
                                  return getSupportFragmentManager().findFragmentByTag("SecondFragment");}
                  
                              else return null;//Fragment.instantiate(mContext, mTabs.get(position), null);
                  
                          }
                  
                  
                          @Override
                          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                          }
                  
                          @Override
                          public void onPageSelected(int position) {
                              mActionBar.setSelectedNavigationItem(position);
                          }
                  
                          @Override
                          public void onPageScrollStateChanged(int state) {
                          }
                  
                          @Override
                          public void onTabSelected(Tab tab, FragmentTransaction ft) {
                              mViewPager.setCurrentItem(tab.getPosition());
                  
                          }
                  
                          @Override
                          public void onTabReselected(Tab tab, FragmentTransaction ft) {
                          }
                  
                          @Override
                          public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                          }
                  
                  
                      }
                  
                      @Override
                      public void OnButtonClickedX(View v) {
                  
                          if (v==findViewById(R.id.button1)){
                              Log.i("TRIGGERED","TRIGGERED");
                  
                              FirstFragment ff = (FirstFragment) getSupportFragmentManager().findFragmentByTag("FirstFragment");
                  
                              View root = ff.getView();
                              TextView tv = (TextView) root.findViewById(R.id.textView1);
                              Log.i("Text before Edit",""+tv.getText());
                                    tv.setText("MODIFIED");
                                    Log.i("Text after Edit",""+tv.getText());
                  
                  
                  
                  
                          }
                          // TODO Auto-generated method stub
                  
                      }
                  
                  
                  
                  
                  
                  }
                  

                  感谢 vbsteven!

                  Thanks to vbsteven!

                  这篇关于从另一个 Fragment 更新 ViewPager 中的 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Viewpager 和列表视图 下一篇:FragmentManager 在使用 setOffscreenPageLimit() 方法后已经在执行事务

                  相关文章

                    • <bdo id='VCE62'></bdo><ul id='VCE62'></ul>
                  1. <legend id='VCE62'><style id='VCE62'><dir id='VCE62'><q id='VCE62'></q></dir></style></legend>

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

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