9

TabLayout和ViewPager - 在滑動抽屜項之間滑動之後,單擊選項卡不起作用。TabLayout和ViewPager-Tab在滑動抽屜項之間滑動後不起作用

我工作的應用程序有滑動抽屜和Tablayout。 我參考本教程For Sliding DrawerFor TabLayout

內滑動抽屜

  1. 任務列表 - >TabLayout(Tabs(Today,Monthly,Yearly))
  2. 設置
  3. 訂單

我有滑動抽屜和Tablayout之間的問題。 第一次viewpager正確加載。但是當我移動到下一個抽屜項目(設置片段)之後,返回到TaskList選項卡片段viewpager無法加載任何片段時,在滑動該頁面時,然後加載少量片段。

這裏的Java代碼

public class TaskListFragment extends Fragment { 
private Toolbar toolbar; 
private TabLayout tabLayout; 
private ViewPager viewPager; 

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

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

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_my_page, container, false); 

    viewPager = (ViewPager)rootView.findViewById(R.id.viewpager); 
    tabLayout = (TabLayout)rootView.findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(viewPager); 
    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) { 

     } 
    }); 

    // Inflate the layout for this fragment 
    return rootView; 
} 

private void setupViewPager(ViewPager viewPager) { 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager()); 
    adapter.addFragment(new Mypage_TodayFragment(), "TODAY"); 
    adapter.addFragment(new Mypage_TodayFragment(), "WEEKLY"); 
    adapter.addFragment(new Mypage_TodayFragment(), "MONTHLY"); 
    viewPager.setAdapter(adapter); 
} 

class ViewPagerAdapter extends FragmentPagerAdapter { 
    private final List<Fragment> mFragmentList = new ArrayList<>(); 
    private final List<String> mFragmentTitleList = new ArrayList<>(); 

    public ViewPagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

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

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

    public void addFragment(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } 
} 
@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
} 

這裏.XML代碼

<android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="262dp" 
     android:id="@+id/view" 
     android:layout_weight="0.16"> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabMode="fixed" 
      app:tabGravity="fill" 
      app:tabBackground="@drawable/mytabselector" 
      app:tabIndicatorColor="@color/Orange" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

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

先謝謝了!

+3

使用'FragmentStatePagerAdapter'而不是'F ragmentPagerAdapter' –

回答

13

試試這個,而不是

ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());

ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

這是因爲你有一個片段viewpager,片段經理,這viewpager應該使用,使片段交易應該是兒童片段管理者

+2

非常感謝!它的工作對我來說...告訴我另一件事getFragmentManager()在AppCompatActivity中不起作用? –

+0

由於您正在將片段加載到片段中(即您的視圖頁面加載片段,並且您的視圖頁面本身是一個單獨的片段),您需要使用childfragmentmanager執行片段事務。你看到活動是父母,它的片段是它的孩子,孩子不能使用fragmentManager,fragmentManager是活動,孩子應該使用childFragmentManager – Bhargav