Android - 如何使用Recyclerview在TabLayout中滑动标签

问题描述:

我有一个片段中的四个标签。我正在将数据加载到RecyclerView ..我没有使用每个选项卡的每个片段,并且对所有选项卡使用单个片段。我只是在适配器中更改了数据集。选择选项卡时,加载数据以使其工作良好。现在我想刷卡页面就应该到下一个标签...Android - 如何使用Recyclerview在TabLayout中滑动标签

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> 

一切工作正常...现在我想添加刷卡的页面也进行到下一选项卡,并更改该数据集...帮助我...在此先感谢...

+0

你需要附加查看寻呼机与选项卡布局刷卡选项卡 –

+0

雅我试过了。在viewPager adaper中有这么多的片段,所以你可以为每个标签添加片段。但在我的情况下,我使用单个片段,只是改变设置,所以如何处理滑动页面.. ?? –

+0

你可以添加相同的片段查看pager与recyclerview中的不同数据 –

在您的片段中创建一个方法,每次更新视图。添加viewpager addOnPageChangeListener,并找到您想更新视图的片段。

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) { 

     } 
    });