具有不同页码的Viewpager
Viewpager with diffrent page number
我的应用在主屏幕上有 10 个项目。他们每个人都有不同的子类别(比如 item1 他的 5,item8 他的 3)
我的问题是关于实施此类问题的最佳做法。
我已经尝试过使用片段页面适配器加载页面(类别),但它似乎并没有解决我的问题。
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return x.newInstance();
case 1:
return y.newInstance();
case 2:
return z.newInstance();
default:
return null;
}
}
你能提供更多细节吗?您将选项卡称为项目吗?
每个类别都由相同的 Class 预定义吗?因为如果是这种情况,请向 newInstance 方法提供参数,您可以在其中指定类别和所需的项目数,如下所示:
public class Frag_UserItems extends Fragment {
...
public static MyFragment newInstance (int itemsNumber, Category category){
// assuming category is an enum
Bundle bundle = new Bundle();
MyFragment fragment = new MyFragment();
bundle.setInt("num", itemsNumber);
bundle.setString("category", category.toString());
fragment.setArguments(args);
return fragment;
}
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_user_items, container, false);
//fetch the arguments and get the data from the reposotry
// and filter them how you want
Bundle args = getArguments();
int num = args.getInt("num");
String num = args.getString("category");
List<MyItem> items = ...
// bind view..
initview(rootView, items);
return rootView;
}
}
我认为您也可以将片段实例存储在一个数组中,或者您可以将索引号传递给 newInstance 方法,在那里您将处理基于索引创建片段,这样它将减少 getItem 到:
@Override
public Fragment getItem(int position) {
return MyFragment.newInstance(position);
}
public class Frag_UserItems extends Fragment {
...
public static MyFragment newInstance (int position){
// assuming category is an enum
Bundle bundle = new Bundle();
MyFragment fragment = new MyFragment();
bundle.setInt("num", position);
fragment.setArguments(args);
return fragment;
}
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_user_items, container, false);
//fetch the arguments and get the data from the reposotry
// and filter them how you want
Bundle args = getArguments();
int num = args.getInt("num");
List<MyItem> items = ...
// bind view..
initview(rootView, items);
return rootView;
}
}
这基本上遵循了工厂方法模式。
希望对您有所帮助
你应该给我们更多关于你将要做什么的信息!但据我了解,你有一些项目,每个项目都有一些子项目。如果是我,我会使用嵌套的 RecyclerView 来做到这一点!
在这里,我找到了一篇可以帮助您的好文章:
https://android.jlelse.eu/easily-adding-nested-recycler-view-in-android-a7e9f7f04047
我希望这就是你想要做的。
我的应用在主屏幕上有 10 个项目。他们每个人都有不同的子类别(比如 item1 他的 5,item8 他的 3) 我的问题是关于实施此类问题的最佳做法。 我已经尝试过使用片段页面适配器加载页面(类别),但它似乎并没有解决我的问题。
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return x.newInstance();
case 1:
return y.newInstance();
case 2:
return z.newInstance();
default:
return null;
}
}
你能提供更多细节吗?您将选项卡称为项目吗? 每个类别都由相同的 Class 预定义吗?因为如果是这种情况,请向 newInstance 方法提供参数,您可以在其中指定类别和所需的项目数,如下所示:
public class Frag_UserItems extends Fragment {
...
public static MyFragment newInstance (int itemsNumber, Category category){
// assuming category is an enum
Bundle bundle = new Bundle();
MyFragment fragment = new MyFragment();
bundle.setInt("num", itemsNumber);
bundle.setString("category", category.toString());
fragment.setArguments(args);
return fragment;
}
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_user_items, container, false);
//fetch the arguments and get the data from the reposotry
// and filter them how you want
Bundle args = getArguments();
int num = args.getInt("num");
String num = args.getString("category");
List<MyItem> items = ...
// bind view..
initview(rootView, items);
return rootView;
}
}
我认为您也可以将片段实例存储在一个数组中,或者您可以将索引号传递给 newInstance 方法,在那里您将处理基于索引创建片段,这样它将减少 getItem 到:
@Override
public Fragment getItem(int position) {
return MyFragment.newInstance(position);
}
public class Frag_UserItems extends Fragment {
...
public static MyFragment newInstance (int position){
// assuming category is an enum
Bundle bundle = new Bundle();
MyFragment fragment = new MyFragment();
bundle.setInt("num", position);
fragment.setArguments(args);
return fragment;
}
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_user_items, container, false);
//fetch the arguments and get the data from the reposotry
// and filter them how you want
Bundle args = getArguments();
int num = args.getInt("num");
List<MyItem> items = ...
// bind view..
initview(rootView, items);
return rootView;
}
}
这基本上遵循了工厂方法模式。
希望对您有所帮助
你应该给我们更多关于你将要做什么的信息!但据我了解,你有一些项目,每个项目都有一些子项目。如果是我,我会使用嵌套的 RecyclerView 来做到这一点! 在这里,我找到了一篇可以帮助您的好文章: https://android.jlelse.eu/easily-adding-nested-recycler-view-in-android-a7e9f7f04047
我希望这就是你想要做的。