0

我正在開發一個Android應用程序。在我的應用程序中,我使用TabLayout和ViewPager。我需要在選擇底部導航欄中的項目時以編程方式更新TabLayout的選項卡及其片段。我可以更新TabLayout的選項卡。但是尋呼機的碎片沒有更新。以編程方式更改TabLayout的選項卡,但ViewPager的片段未更新

這是問題:

enter image description here

正如你可以在上面看到,標籤被改變,但其碎片也不會改變。列表片段始終顯示。所有的片段與選擇的第一個選項卡一樣。我的意思是碎片根本沒有改變。請參閱下面的代碼。

這是對標籤和查看傳呼機我的XML文件:

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> 


     <android.support.design.widget.TabLayout 
      android:id="@+id/ma_tab_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabGravity="fill" 
      app:tabMode="fixed" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:id="@+id/ma_viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

這是我的整個活動:

public class MainActivity extends AppCompatActivity { 

    private BottomBar bottomBar; 
    private TabLayout topTabLayout; 
    private ViewPager mainViewPager; 
    private MainPagerAdapter pagerAdapter; 
    private ArrayList<Fragment> pagerFragments; 
    private ArrayList<String> pagerTitleList; 
    protected TfrApplication app; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     app = (TfrApplication)getApplication(); 
     setContentView(R.layout.activity_main); 
     initialize(); 
     initViews(); 
     setUpViews(); 
    } 

    private void initialize() 
    { 
     pagerFragments = new ArrayList<Fragment>(); 
     pagerTitleList = new ArrayList<String>(); 
    } 

    private void initViews() 
    { 
     bottomBar = (BottomBar)findViewById(R.id.ma_bottom_action_bar); 
     mainViewPager = (ViewPager)findViewById(R.id.ma_viewpager); 
     topTabLayout = (TabLayout)findViewById(R.id.ma_tab_layout); 
    } 

    private void setUpViews() 
    { 
     pagerAdapter = new MainPagerAdapter(getSupportFragmentManager(),pagerFragments,pagerTitleList); 
     mainViewPager.setAdapter(pagerAdapter); 
     topTabLayout.setupWithViewPager(mainViewPager); 
     setUpBottomBar(); 
    } 

    private void clearPagerFragments() 
    { 
     if(pagerAdapter!=null && pagerFragments!=null && pagerFragments.size()>0) 
     { 
      pagerFragments.removeAll(pagerFragments); 
      pagerTitleList.removeAll(pagerTitleList); 
      pagerAdapter.notifyDataSetChanged(); 
     } 
    } 

    private void setUpNewsPagerFragments() 
    { 
     NewsFragment latestNewsFragment = new NewsFragment(); 
     Bundle latestNewsBundle = new Bundle(); 
     latestNewsBundle.putInt(NewsFragment.FIELD_CATEGORY_ID,0); 
     latestNewsBundle.putInt(NewsFragment.FIELD_LEAGUE_ID,0); 
     latestNewsBundle.putInt(NewsFragment.FIELD_COUNTRY_ID,0); 
     latestNewsBundle.putInt(NewsFragment.FIELD_TEAM_ID,0); 
     latestNewsFragment.setArguments(latestNewsBundle); 
     pagerTitleList.add("LATEST"); 
     pagerFragments.add(latestNewsFragment); 

     PrioritizedTeamsFragment teamsFragment = new PrioritizedTeamsFragment(); 
     pagerTitleList.add("TEAMS"); 
     pagerFragments.add(teamsFragment); 

     NewsFragment articlesFragment = new NewsFragment(); 
     Bundle articlesBundle = new Bundle(); 
     articlesBundle.putInt(NewsFragment.FIELD_CATEGORY_ID, 0); 
     articlesBundle.putInt(NewsFragment.FIELD_LEAGUE_ID, 0); 
     articlesBundle.putInt(NewsFragment.FIELD_COUNTRY_ID, 0); 
     articlesBundle.putInt(NewsFragment.FIELD_TEAM_ID, 0); 
     articlesFragment.setArguments(articlesBundle); 
     pagerTitleList.add("ARTICLES"); 
     pagerFragments.add(articlesFragment); 

     TopFragment topFragment = new TopFragment(); 
     pagerTitleList.add("TOP 10"); 
     pagerFragments.add(topFragment); 
    } 

    private void setUpMatchesPagerFragments() 
    { 
     MatchesFragment matchesFragment = new MatchesFragment(); 
     pagerTitleList.add("MATCHES"); 
     pagerFragments.add(matchesFragment); 

     StatisticsFragment statisticsFragment = new StatisticsFragment(); 
     pagerTitleList.add("STATISTICS"); 
     pagerFragments.add(statisticsFragment); 
    } 

    private void setUpBottomBar() 
    { 
     bottomBar.setOnTabSelectListener(new OnTabSelectListener() { 
      @Override 
      public void onTabSelected(int tabId) { 
       switch (tabId){ 
        case R.id.bottom_tab_news: 
         clearPagerFragments(); 
         setUpNewsPagerFragments(); 
         pagerAdapter.notifyDataSetChanged(); 
         break; 
        case R.id.bottom_tab_matches: 
         clearPagerFragments(); 
         setUpMatchesPagerFragments(); 
         pagerAdapter.notifyDataSetChanged(); 
         topTabLayout.getTabAt(0).select(); 
         break; 

        case R.id.bottom_tab_meme: 
         Toast.makeText(getBaseContext(),"MEME",Toast.LENGTH_SHORT).show(); 
         break; 

        case R.id.bottom_tab_settings: 
         Toast.makeText(getBaseContext(),"Settings",Toast.LENGTH_SHORT).show(); 
         break; 
       } 
      } 
     }); 
    } 
} 

我展示了整個活動,因爲代碼整個活動處理這一問題的。此外,整個活動只包含用於創建選項卡,查看傳呼機和底欄的代碼。所有連接。

這是我的看法尋呼機適配器:

public class MainPagerAdapter extends FragmentPagerAdapter { 
    private ArrayList<Fragment> fragmentList; 
    private ArrayList<String> titleList; 

    public MainPagerAdapter(FragmentManager fragmentManager,ArrayList<Fragment> fragmentsParam,ArrayList<String> titlesParam) 
    { 
     super(fragmentManager); 
     this.fragmentList = fragmentsParam; 
     this.titleList = titlesParam; 
    } 

    @Override 
    public int getCount() { 
     return fragmentList.size(); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return fragmentList.get(position); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return titleList.get(position); 
    } 
} 

爲什麼內容或查看傳呼機的片段不是當標籤被改變改變了嗎?我的代碼有什麼問題?

正如你可以在底部項目選擇的聽衆看到我試圖將選定的片段是這樣的:

topTabLayout.getTabAt(0).select(); 

我嘗試這樣做,以及:

mainViewPager.setCurrentItem(0); 

兩者都不能正常工作。

回答

0

嘗試添加一個方法來交換片段列表和電話notifyDataSetChanged()在適配器這樣的:

public void notifyDataSetChanged(List<Fragment> list) { 
    fragmentList.removeAll(); 
    fragmentList.addAll(list); 
    notifyDataSetChanged(); 
} 

改變數據的活動,並呼籲adapter.notifyDataSetChanged()可能無法更新view.You可以登錄getItem()檢查適配器是否知道數據集已更改。

0

只需檢查您在MatchesFragment類中正在充氣的佈局。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    v = inflater.inflate(R.layout.matches_fragment, container, false); 
} 
+0

充氣正確的佈局。這不僅僅是更新整套片段。 –

相關問題