2016-04-26 45 views
0

我试图刷新我的片段,以便我可以为用户获取新信息。基本上我是做一个应用程序来参加锦标赛。我在我的应用上有3个标签,第一个标签允许用户添加第二个标签,允许用户创建比赛,最后一个标签向用户显示分数。我如何使新的片段onTabSelected刷新我的内容?

enter image description here

但我有一个问题,当我从第一个选项卡中删除一个用户,我去我的第二个选项卡我的内容doesn't更新,我真的希望它更新,我因子评分有关创建一个新的片段,当它被选中或尝试刷新它时,它被选中,但我是新的android和我不知道该怎么做。

任何意识?

这里是我的MainActivity代码:

public class MainActivity extends AppCompatActivity{ 
    // Declaring Your View and Variables 
    Toolbar toolbar; 
    ViewPager viewPager; 
    ViewPagerAdapter adapter; 
    TabLayout tabLayout; 
    CharSequence Titles[]={"Participantes","Torneio","Classificação"}; 
    int Numboftabs =3; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     toolbar = (Toolbar) findViewById(R.id.tool_bar); 
     viewPager = (ViewPager) findViewById(R.id.viewPager); 
     tabLayout = (TabLayout)findViewById(R.id.tabLayout); 


     // Creating The Toolbar and setting it as the Toolbar for the activity 
     setSupportActionBar(toolbar); 
     // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs. 
     adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs); 
     // Assigning ViewPager View and setting the adapter 
     viewPager.setAdapter(adapter); 


     tabLayout.setupWithViewPager(viewPager); 
     tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorTextIcons)); 
     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       //should i refresh my tab here? 
       // how do i do that ? 
      } 


      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 

      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 

      } 
     }); 
    } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     if (id == R.id.action_info) { 
      Toast.makeText(getApplicationContext(), "Contacto: [email protected]", Toast.LENGTH_LONG).show(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


} 

这里是我的ViewPageAdapter代码:

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

    CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created 
    int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created 


    // Build a Constructor and assign the passed Values to appropriate values in the class 
    public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) { 
     super(fm); 

     this.Titles = mTitles; 
     this.NumbOfTabs = mNumbOfTabsumb; 

    } 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     super.destroyItem(container, position, object); 
     FragmentManager manager = ((Fragment) object).getFragmentManager(); 
     FragmentTransaction trans = manager.beginTransaction(); 
     trans.remove((Fragment) object); 
     trans.commit(); 
    } 


    //This method return the fragment for the every position in the View Pager 
    @Override 
    public Fragment getItem(int position) { 

     switch(position) {// if the position is 0 we are returning the First tab 
      case 0: 
      Tab1 tab1 = new Tab1(); 
      return tab1; 
      case 1: 
      Tab2 tab2 = new Tab2(); 
      return tab2; 
      case 2: 
      Tab3 tab3 = new Tab3(); 
      return tab3; 
      default: 
       return null; 
     } 


    } 

    // This method return the titles for the Tabs in the Tab Strip 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return Titles[position]; 
    } 

    // This method return the Number of tabs for the tabs Strip 

    @Override 
    public int getCount() { 
     return NumbOfTabs; 
    } 
} 
+0

我已经尝试使用onResume()并没有解决 –

+0

您是否希望每次切换标签页时都要刷新适配器? –

+0

请看这里:http://stackoverflow.com/questions/18088076/update-fragment-from-viewpager ...问题已经在这里回答。 – Sin

回答

1

我面对同样的问题,基于答案和一些测试。

我有三分之一的替代(quite similar to the second method):

,而是调用notifyDataSetChanged(),(这引起了我的TabLayout奇怪的行为),创建ViewPager.OnPageChangeListener类型的侦听器,并直接调用片段更新方法。看到代码:

public interface Updateable { 
    public void update(); 
} 


public class MyFragment extends Fragment implements Updateable { 

    ... 

    public void update() { 
    // do your stuff on fragment became visible 
    } 
} 

在活动主持人:

mAdapter = new DashboardPagerAdapter(DashboardActivity.this, 
     getSupportFragmentManager(), mTabs); 
mAdapter.configure(mViewPager); 
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ 
    @Override 
    public void onPageSelected(int position) { 
     if (mAdapter != null) { 
      Updateable fragment = (Updateable)mAdapter.getItem(position); 
      if (fragment != null) { 
       fragment. update(); 
      } 
     } 
    } 
}); 

在这样的片段不重新创建,只是运行的更新方法。对我来说是足够的。