将 RecyclerView 与 TabLayout 同步
Synchronize RecyclerView with TabLayout
我们可以在 TabLayout 下放置 Recyclerview 而不是 ViewPager 吗?而且,当一个选项卡被选中时,Recyclerview 的内容将被更新。
您可以这样设置,添加所需的数量,在 RecycleView
的初始数据中为 0 位置然后添加 addOnTabSelectedListener
并在 Tab 根据其位置更改时更新列表
private TabLayout tabLayout;
private MyAdapter myAdapter;
private List<MyObject> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list=new ArrayList<>();
myAdapter=new MyAdapter(this,list);
RecyclerView recyclerViewOrderList = view.findViewById(R.id.recyclerViewOrderList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerViewOrderList.setLayoutManager(layoutManager);
recyclerViewOrderList.setAdapter(myAdapter);
TabLayout.Tab tab1 = tabLayout.newTab();
tab1.setText("TAb1");
TabLayout.Tab tab2 = tabLayout.newTab();
tab1.setText("TAb1");
tabLayout.addTab(tab1);
tabLayout.addTab(tab2);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
//list
// setup adapter for position 0
break;
case 1:
// setup adapter for position 1
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
我已经回答了 ,但对于这个问题可能仍然与复制它相关:
足以覆盖 RecyclerView 的 onScrolled 和 select 选项卡并在选项卡 selected 并且该选项卡尚未 selected 时滚动到位置:
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val llm = recyclerView.layoutManager as LinearLayoutManager
// depending on sections'heights one may want to add more logic
// on how to determine which section to scroll to
val firstCompletePos = llm.findFirstCompletelyVisibleItemPosition()
if (firstCompletePos != tabLayout.selectedTabPosition)
tabLayout.getTabAt(firstCompletePos)?.select()
}
然后我有一个 TextView,它被设置为 tabLayout 的 customView:
tabLayout.addTab(newTab().also { tab ->
tab.customView = AppCompatTextView(context).apply {
// set layout params match_parent, so the entire section is clickable
// set style, gravity, text etc.
setOnClickListener {
tabLayout.selectTab(tab)
recyclerView.apply {
val scrollTo = tabLayout.selectedTabPosition
smoothScrollToPosition(scrollTo)
}
}
}
})
通过此设置,您拥有:
- Tab select 当用户滚动和滑动时
- 当用户单击选项卡时,RecyclerView 会滚动。
我们可以在 TabLayout 下放置 Recyclerview 而不是 ViewPager 吗?而且,当一个选项卡被选中时,Recyclerview 的内容将被更新。
您可以这样设置,添加所需的数量,在 RecycleView
的初始数据中为 0 位置然后添加 addOnTabSelectedListener
并在 Tab 根据其位置更改时更新列表
private TabLayout tabLayout;
private MyAdapter myAdapter;
private List<MyObject> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list=new ArrayList<>();
myAdapter=new MyAdapter(this,list);
RecyclerView recyclerViewOrderList = view.findViewById(R.id.recyclerViewOrderList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerViewOrderList.setLayoutManager(layoutManager);
recyclerViewOrderList.setAdapter(myAdapter);
TabLayout.Tab tab1 = tabLayout.newTab();
tab1.setText("TAb1");
TabLayout.Tab tab2 = tabLayout.newTab();
tab1.setText("TAb1");
tabLayout.addTab(tab1);
tabLayout.addTab(tab2);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
//list
// setup adapter for position 0
break;
case 1:
// setup adapter for position 1
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
我已经回答了
足以覆盖 RecyclerView 的 onScrolled 和 select 选项卡并在选项卡 selected 并且该选项卡尚未 selected 时滚动到位置:
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val llm = recyclerView.layoutManager as LinearLayoutManager
// depending on sections'heights one may want to add more logic
// on how to determine which section to scroll to
val firstCompletePos = llm.findFirstCompletelyVisibleItemPosition()
if (firstCompletePos != tabLayout.selectedTabPosition)
tabLayout.getTabAt(firstCompletePos)?.select()
}
然后我有一个 TextView,它被设置为 tabLayout 的 customView:
tabLayout.addTab(newTab().also { tab ->
tab.customView = AppCompatTextView(context).apply {
// set layout params match_parent, so the entire section is clickable
// set style, gravity, text etc.
setOnClickListener {
tabLayout.selectTab(tab)
recyclerView.apply {
val scrollTo = tabLayout.selectedTabPosition
smoothScrollToPosition(scrollTo)
}
}
}
})
通过此设置,您拥有:
- Tab select 当用户滚动和滑动时
- 当用户单击选项卡时,RecyclerView 会滚动。