0

J'ai les quatre onglets dans un fragment. Je charge des données dans RecyclerView. Je n'utilise pas chaque fragment pour chaque onglet et j'utilise un seul fragment pour tous les onglets. Je viens de changer le jeu de données uniquement dans l'adaptateur. Tout en sélectionnant l'onglet son chargement de ces données de sorte qu'il fonctionne bien et bien. Maintenant, je veux glisser la page, il devrait aller à l'onglet suivant ...Android - Comment faire glisser l'onglet dans TabLayout en utilisant Recyclerview

public class CreateFlipFragment extends Fragment { 

private TabLayout tabLayout; 
private RecyclerView recyclerView; 
private StickerListAdapter adapter; 
private ArrayList<Sticker> stickerList; 
private ViewPager viewPagerStickers; 

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

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

    tabLayout = (TabLayout) view.findViewById(R.id.tablayout_flips); 
    recyclerView = (RecyclerView) view.findViewById(R.id.rv_flips_sticker); 
    viewPagerStickers = (ViewPager) view.findViewById(R.id.vp_stickers); 

    stickerList = new ArrayList<>(); 
    adapter = new StickerListAdapter(stickerList); 
    recyclerView.setHasFixedSize(true); 
    GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3); 
    recyclerView.setLayoutManager(layoutManager); 
    recyclerView.setAdapter(adapter); 

    setupTab(); 

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 

     viewPagerStickers.setCurrentItem(tab.getPosition()); 

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

      switch (tab.getPosition()) { 
       case 0: 
        loadStickers1(); 
        break; 
       case 1: 
        loadStickers2(); 

        } 
        break; 
       case 2: 
        loadStickers4(); 

        break; 
       case 3: 
        loadStickers5(); 

        break; 
       default: 
        stickerList.clear(); 
        adapter.notifyDataSetChanged(); 
        break; 
      } 
     } 

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

     } 

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

     } 
    }); 
    } 

    private void setupTab() { 
    tabLayout.addTab(tabLayout.newTab()); 
    tabLayout.addTab(tabLayout.newTab()); 
    tabLayout.addTab(tabLayout.newTab()); 
    tabLayout.addTab(tabLayout.newTab()); 
    tabLayout.getTabAt(0).setIcon(R.drawable.meme0); 
    tabLayout.getTabAt(1).setIcon(R.drawable.bubble0); 
    tabLayout.getTabAt(2).setIcon(R.drawable.ic_sticker_text_off); 
    tabLayout.getTabAt(3).setIcon(R.drawable.ic_sticker_image_off); 
    } 

    private void loadStickers1() { 
    stickerList.clear(); 
    stickerList.add(new Sticker(null, R.drawable.word4, Sticker.STICKER_TYPE.STICKER)); 
    stickerList.add(new Sticker(null, R.drawable.word5, Sticker.STICKER_TYPE.STICKER)); 
    stickerList.add(new Sticker(null, R.drawable.word6, Sticker.STICKER_TYPE.STICKER)); 
    adapter.notifyDataSetChanged(); 
    } 
    private void loadStickers2() { 
    stickerList.clear(); 
    stickerList.add(new Sticker(null, R.drawable.word4, Sticker.STICKER_TYPE.STICKER)); 
    stickerList.add(new Sticker(null, R.drawable.word5, Sticker.STICKER_TYPE.STICKER)); 
    stickerList.add(new Sticker(null, R.drawable.word6, Sticker.STICKER_TYPE.STICKER)); 
    adapter.notifyDataSetChanged(); 
    } 
    private void loadStickers3() { 
    stickerList.clear(); 
    stickerList.add(new Sticker(null, R.drawable.word4, Sticker.STICKER_TYPE.STICKER)); 
    stickerList.add(new Sticker(null, R.drawable.word5, Sticker.STICKER_TYPE.STICKER)); 
    stickerList.add(new Sticker(null, R.drawable.word6, Sticker.STICKER_TYPE.STICKER)); 
    adapter.notifyDataSetChanged(); 
    } 
    private void loadStickers4() { 
    stickerList.clear(); 
    stickerList.add(new Sticker(null, R.drawable.word4, Sticker.STICKER_TYPE.STICKER)); 
    stickerList.add(new Sticker(null, R.drawable.word5, Sticker.STICKER_TYPE.STICKER)); 
    stickerList.add(new Sticker(null, R.drawable.word6, Sticker.STICKER_TYPE.STICKER)); 
    adapter.notifyDataSetChanged(); 
    } 
    } 

fragment_create_flip_new.xml

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tablayout_flips" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/gray_very_light" 
     app:tabGravity="fill" 
     app:tabIndicatorHeight="1dp" 
     app:tabMode="fixed" 
     app:tabSelectedTextColor="@color/colorPrimary" 
     app:tabTextAppearance="@style/TabDefalt" 
     app:tabTextColor="@color/text_color" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rv_flips_sticker" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/transparent" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/vp_stickers" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

</LinearLayout> 

Tout fonctionne bien ... Maintenant, je veux ajouter glisser la page suivante il va onglet et modifier cet ensemble de données ... Aidez-moi ... Merci d'avance ...

+0

vous devez attacher le pager de vue avec la disposition de tabulation pour l'étiquette de balayage –

+0

ya que j'ai essayé. Dans ViewPager adaper ayant autant de fragments que vous pouvez ajouter un fragment pour chaque onglet. mais dans mon cas, j'utilise un seul fragment et juste changé ensemble seulement ainsi comment manipuler la page de balayage .. ?? –

+0

vous pouvez ajouter même fragment dans le pager de vue avec différentes données dans recyclerview –

Répondre

0

Créez une méthode dans votre fragment pour mettre à jour la vue à chaque fois. Ajoutez viewpager addOnPageChangeListener et trouvez le fragment que vous voulez mettre à jour.

viewPagerStickers.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 


     } 

     @Override 
     public void onPageSelected(int position) { 
      Fragment fragment = YOURADAPTER.getItem(tab.getPosition()); 
      if (fragment != null && fragment.getClass().getSimpleName().equals(YOURFRAGMENT.class.getSimpleName())) { 
       YOURFRAGMENT playListFragment = (YOURFRAGMENT) YOURADAPTER.getCurrentItem();; 
       playListFragment.updateData(data); 
      } 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 

     } 
    });