在 ViewPager 的多个选项卡中使用单个片段
Use Single Fragment in Multiple tabs of ViewPager
我用的是ViewPager左右滑动,我也加了tabs,tabs的个数取决于服务器数据,所以不能让tabs的个数固定。为此,我仅使用 Single Fragment 和 RecyclerView 在 recyclerView 中显示 JSON 数据。当第一个应用程序启动时,应该显示在第二个选项卡中的数据会显示在第一个选项卡本身中。在我滑动到第 3 个选项卡并再次返回到第 1 个选项卡后,数据显示正确。
与GooglePlayStore一样。我认为只有一个片段,因为 UI 在所有选项卡中都是相同的。
这是添加 Fragment 并将数据显示到 recyclerView 的代码。
PagerAdapter.java
private class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
List<List<ProductInfo>> data;
public PagerAdapter(FragmentManager fm, int NumofTabs, List<List<ProductInfo>> data) {
super(fm);
mNumOfTabs = NumofTabs;
this.data = data;
}
@Override
public Fragment getItem(int position) {
/* ProductFragment pf = ProductFragment.newInstance(data.get(position),position);
return pf;*/
return ProductFragment.newInstance(data.get(position),0);
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Fragment.java
public class ProductFragment extends Fragment {
private static final String ARG_PRODUCTS = "PRODS";
private static List<ProductInfo> allProducts;
int position = 0;
RecyclerView prodList;
public static ProductFragment newInstance(List<ProductInfo> products,int position) {
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putParcelableArrayList(ARG_PRODUCTS, (ArrayList<ProductInfo>) products);
args.putInt("KEY_POSITION",position);
args.putInt("KEY_ID",id);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//if(isVisibleToUser){
if (getArguments() != null) {
allProducts = getArguments().getParcelableArrayList(ARG_PRODUCTS);
this.position = getArguments().getInt("KEY_POSITION");
}
// }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_product, container, false);
prodList = (RecyclerView) view.findViewById(R.id.product_list);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
prodList.setLayoutManager(new LinearLayoutManager(getActivity()));
prodList.setAdapter(new ProductAdapter(getActivity(), allProducts));
Log.e("ProductFragment " ,"" + allProducts.get(position).getName());
}
已编辑:
Activity.java
onCreate(){
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
allProducts = new ArrayList<>();
for (Category cat : catList) {
tabLayout.addTab(tabLayout.newTab().setText(cat.getName()));
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
//tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
allProducts.add(cat.getProductsList());
}
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount(), allProducts);
viewPager.setOffscreenPageLimit(0);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.e("ViewPager "," getCurrentItem() "+viewPager.getCurrentItem());
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
在此处输入图片描述
在 PagerAdapter 中编辑如下,这可能会有所帮助:
@Override
public Fragment getItem(int position) {
/* ProductFragment pf = ProductFragment.newInstance(data.get(position),position);
return pf;*/
return ProductFragment.newInstance(data.get(position),position);
}
然后在您的 Fragment 中进行如下更改:
public class ProductFragment extends Fragment {
private static final String ARG_PRODUCTS = "PRODS";
private static List<ProductInfo> allProducts;
int position = 0;
RecyclerView prodList;
ProductAdapter productAdapter=null;
public static ProductFragment newInstance(List<ProductInfo> products,int position) {
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putParcelableArrayList(ARG_PRODUCTS, (ArrayList<ProductInfo>) products);
args.putInt("KEY_POSITION",position);
args.putInt("KEY_ID",id);
fragment.setArguments(args);
return fragment;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
productAdapter.notifyDataSetChanged();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//if(isVisibleToUser){
if (getArguments() != null) {
allProducts = getArguments().getParcelableArrayList(ARG_PRODUCTS);
this.position = getArguments().getInt("KEY_POSITION");
}
// }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_product, container, false);
prodList = (RecyclerView) view.findViewById(R.id.product_list);
prodList.setLayoutManager(new LinearLayoutManager(getActivity()));
productAdapter= new ProductAdapter(getActivity(), allProducts)
prodList.setAdapter(productAdapter);
return view;
}
}
更改此行:PagerAdapter.java
@Override
public Fragment getItem(int position) {
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putParcelableArrayList(ARG_PRODUCTS, (ArrayList<ProductInfo>) products);
args.putInt("KEY_POSITION",position);
args.putInt("KEY_ID",id);
fragment.setArguments(args);
return fragment;
}
并从片段中删除静态方法:
public static ProductFragment newInstance()
bcoz这个静态方法从viewPager设置最新页面的数据。仅供参考,viewPager 会提前在内存中加载下一页。静态方法让它现在显示而不是将来显示。
我遇到了一个小问题,我使用的是带有多个 TabLayout 的单个片段。我一直在我的片段中得到错误的位置,这是 TabLayout 中的最后一个位置。
问题是在静态变量或 Bundle 中保存片段位置将保存最后创建的片段,而 ViewPager 将在当前位置之前创建片段,这就是我们丢失当前位置的原因。
所以我为解决这个问题所做的是创建一个局部变量来保存所创建片段的制表符位置。
Kotlin 中的代码
class TabFragment : Fragment() {
private var tabPosition = 0
companion object {
fun newInstance(tabPosition: Int): TabFragment {
val tabFragment = TabFragment()
tabFragment.tabPosition = tabPosition
return tabFragment
}
}
}
代码在Java
public class TabFragment extends Fragment {
private int tabPosition = 0;
static TabFragment newInstance(int tabPosition){
TabFragment tabFragment = new TabFragment();
tabFragment.tabPosition = tabPosition;
return tabFragment;
}
}
我用的是ViewPager左右滑动,我也加了tabs,tabs的个数取决于服务器数据,所以不能让tabs的个数固定。为此,我仅使用 Single Fragment 和 RecyclerView 在 recyclerView 中显示 JSON 数据。当第一个应用程序启动时,应该显示在第二个选项卡中的数据会显示在第一个选项卡本身中。在我滑动到第 3 个选项卡并再次返回到第 1 个选项卡后,数据显示正确。
与GooglePlayStore一样。我认为只有一个片段,因为 UI 在所有选项卡中都是相同的。
这是添加 Fragment 并将数据显示到 recyclerView 的代码。
PagerAdapter.java
private class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
List<List<ProductInfo>> data;
public PagerAdapter(FragmentManager fm, int NumofTabs, List<List<ProductInfo>> data) {
super(fm);
mNumOfTabs = NumofTabs;
this.data = data;
}
@Override
public Fragment getItem(int position) {
/* ProductFragment pf = ProductFragment.newInstance(data.get(position),position);
return pf;*/
return ProductFragment.newInstance(data.get(position),0);
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Fragment.java
public class ProductFragment extends Fragment {
private static final String ARG_PRODUCTS = "PRODS";
private static List<ProductInfo> allProducts;
int position = 0;
RecyclerView prodList;
public static ProductFragment newInstance(List<ProductInfo> products,int position) {
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putParcelableArrayList(ARG_PRODUCTS, (ArrayList<ProductInfo>) products);
args.putInt("KEY_POSITION",position);
args.putInt("KEY_ID",id);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//if(isVisibleToUser){
if (getArguments() != null) {
allProducts = getArguments().getParcelableArrayList(ARG_PRODUCTS);
this.position = getArguments().getInt("KEY_POSITION");
}
// }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_product, container, false);
prodList = (RecyclerView) view.findViewById(R.id.product_list);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
prodList.setLayoutManager(new LinearLayoutManager(getActivity()));
prodList.setAdapter(new ProductAdapter(getActivity(), allProducts));
Log.e("ProductFragment " ,"" + allProducts.get(position).getName());
}
已编辑: Activity.java
onCreate(){
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
allProducts = new ArrayList<>();
for (Category cat : catList) {
tabLayout.addTab(tabLayout.newTab().setText(cat.getName()));
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
//tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
allProducts.add(cat.getProductsList());
}
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount(), allProducts);
viewPager.setOffscreenPageLimit(0);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.e("ViewPager "," getCurrentItem() "+viewPager.getCurrentItem());
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
在此处输入图片描述
在 PagerAdapter 中编辑如下,这可能会有所帮助:
@Override
public Fragment getItem(int position) {
/* ProductFragment pf = ProductFragment.newInstance(data.get(position),position);
return pf;*/
return ProductFragment.newInstance(data.get(position),position);
}
然后在您的 Fragment 中进行如下更改:
public class ProductFragment extends Fragment {
private static final String ARG_PRODUCTS = "PRODS";
private static List<ProductInfo> allProducts;
int position = 0;
RecyclerView prodList;
ProductAdapter productAdapter=null;
public static ProductFragment newInstance(List<ProductInfo> products,int position) {
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putParcelableArrayList(ARG_PRODUCTS, (ArrayList<ProductInfo>) products);
args.putInt("KEY_POSITION",position);
args.putInt("KEY_ID",id);
fragment.setArguments(args);
return fragment;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
productAdapter.notifyDataSetChanged();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//if(isVisibleToUser){
if (getArguments() != null) {
allProducts = getArguments().getParcelableArrayList(ARG_PRODUCTS);
this.position = getArguments().getInt("KEY_POSITION");
}
// } }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_product, container, false);
prodList = (RecyclerView) view.findViewById(R.id.product_list);
prodList.setLayoutManager(new LinearLayoutManager(getActivity()));
productAdapter= new ProductAdapter(getActivity(), allProducts)
prodList.setAdapter(productAdapter);
return view;
}
}
更改此行:PagerAdapter.java
@Override
public Fragment getItem(int position) {
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putParcelableArrayList(ARG_PRODUCTS, (ArrayList<ProductInfo>) products);
args.putInt("KEY_POSITION",position);
args.putInt("KEY_ID",id);
fragment.setArguments(args);
return fragment;
}
并从片段中删除静态方法:
public static ProductFragment newInstance()
bcoz这个静态方法从viewPager设置最新页面的数据。仅供参考,viewPager 会提前在内存中加载下一页。静态方法让它现在显示而不是将来显示。
我遇到了一个小问题,我使用的是带有多个 TabLayout 的单个片段。我一直在我的片段中得到错误的位置,这是 TabLayout 中的最后一个位置。
问题是在静态变量或 Bundle 中保存片段位置将保存最后创建的片段,而 ViewPager 将在当前位置之前创建片段,这就是我们丢失当前位置的原因。
所以我为解决这个问题所做的是创建一个局部变量来保存所创建片段的制表符位置。
Kotlin 中的代码
class TabFragment : Fragment() {
private var tabPosition = 0
companion object {
fun newInstance(tabPosition: Int): TabFragment {
val tabFragment = TabFragment()
tabFragment.tabPosition = tabPosition
return tabFragment
}
}
}
代码在Java
public class TabFragment extends Fragment {
private int tabPosition = 0;
static TabFragment newInstance(int tabPosition){
TabFragment tabFragment = new TabFragment();
tabFragment.tabPosition = tabPosition;
return tabFragment;
}
}