2016-08-06 103 views
2

當我選擇選項卡3時出現我的列表視圖,但是當我選擇選項卡1並重新選擇選項卡3時,不會出現列表視圖。 這是爲什麼? https://github.com/jdavey1996/News-Android-AppAndroid片段選項卡式佈局,重新選擇選項卡3,列表視圖消失

import android.support.design.widget.TabLayout; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 


public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
     tabLayout.addTab(tabLayout.newTab().setText("Local")); 
     tabLayout.addTab(tabLayout.newTab().setText("Top rated")); 
     tabLayout.addTab(tabLayout.newTab().setText("All")); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

     final FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(),tabLayout.getTabCount()); 
     final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
     viewPager.setAdapter(adapter); 
     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 

     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       viewPager.setCurrentItem(tab.getPosition()); 
      } 

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

      } 

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

      } 
     }); 
    } 
} 





package com.josh_davey.news_app; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 

public class FragmentAdapter extends FragmentPagerAdapter { 
    int count; 
    public FragmentAdapter(FragmentManager fm, int count) { 
     super(fm); 
     this.count = count; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position){ 
      case 0: 
       Fragment1 temp = new Fragment1(); 
       return temp; 
      case 1: 
       Fragment2 temp2 = new Fragment2(); 
       return temp2; 
      case 2: 
       Fragment3 temp3 = new Fragment3(); 
       return temp3; 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     return count; 
    } 
} 



package com.josh_davey.news_app; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class Fragment3 extends Fragment { 

    public Fragment3() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     GetData getData = new GetData(getContext(),getActivity()); 
     getData.execute("lincoln"); 

    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment3, container, false); 
    } 
} 

如果您需要鏈接到完整的代碼它在我的github上。在此先感謝您的幫助 https://github.com/jdavey1996/News-Android-App

回答

2

默認ViewPager的offscreenPageLimit是1.因此,當tab1被重新選擇時tab3被分離。如果片段被分離,片段的視圖被破壞。
看到:ViewPager.setOffscreenPageLimit

因此,我建議你offscreenPageLimit設置爲2

viewPager.setOffscreenPageLimit(2); 

或者你可以按住ArticleConstructor列表中Fragment3類似自爆。

GetData.java:

public class GetData extends AsyncTask<String, String,ArrayList<ArticleConstructor>>{ 
    Callback callback; 

    interface Callback { 
     void onArticleConstructorLoaded(ArrayList<ArticleConstructor> articleConstructors); 
    } 

    public GetData(Callback callback) { 
     this.callback = callback; 
    } 

    ... 

    @Override 
    protected void onPostExecute(ArrayList<ArticleConstructor> result) { 
     callback.onArticleConstructorLoaded(result); 
    } 
    .... 
} 

Fragment3.java:

public class Fragment3 extends Fragment implements GetData.Callback { 

    private ListView listView; 
    private ArrayList<ArticleConstructor> articleConstructors = new ArrayList<>(); 
    private ArticleArrayAdapter adapter; 

    public Fragment3() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment3, container, false); 
     listView = (ListView) view.findViewById(R.id.listView); 
     adapter = new ArticleArrayAdapter(getActivity(), getContext(), articleConstructors); 
     listView.setAdapter(adapter); 
     return view; 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     if (articleConstructors.size() == 0) { 
      GetData getData = new GetData(this); 
      getData.execute("lincoln"); 
     } 
    } 

    @Override 
    public void onArticleConstructorLoaded(ArrayList<ArticleConstructor> articleConstructors) { 
     this.articleConstructors = articleConstructors; 
     adapter.setArticleConstructors(articleConstructors); 
    } 
} 
+0

TNS進行說明** viewPager.setOffscreenPageLimit(2); **救了我的一天。 :) – FlipNovid

相關問題