我使用这个库。的 com.github.florent37:materialviewpager:1.1.3@aar 如何在MaterialViewPager中使用来自服务器的数据?在这里,Kitchen“,”Pork“,”Fish“,”Soup“是静态的,我想更改来自服务器的数据; api。
mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
String[] categories = new String[]{"Kitchen", "Pork", "Fish", "Soup"}; // static, wanna come from server
@Override
public Fragment getItem(int position) {
switch (position % 4) {
default:
return RecyclerViewFragment.newInstance();
}
}
@Override
public int getCount() {
return categories.length;
}
@Override
public CharSequence getPageTitle(int position) {
return categories[position];
}
});
在这里,改造;数据来自服务器;
NetworkEngine.getInstance().getFoodCategory(new Callback<FoodCategory>() {
@Override
public void success(FoodCategory foodCategory, Response response) {
}
@Override
public void failure(RetrofitError error) {
}
});
这里,api
[
{
"id": 1,
"name": "Sea Foods",
"description": "-",
"icon": "photo_20160503084933_277342661make_money.png",
"background": "photo_20160503084933_754915524make_money.png",
"color": "#000FFF",
"created_at": "2016-05-03 08:48:01",
"updated_at": "2016-05-03 08:49:33",
"deleted_at": null
}
]
FoodCategory.java
public class FoodCategory implements Serializable{
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("description")
@Expose
private String description;
@SerializedName("icon")
@Expose
private String icon;
@SerializedName("background")
@Expose
private String background;
@SerializedName("color")
@Expose
private String color;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private String updatedAt;
@SerializedName("deleted_at")
@Expose
private Object deletedAt;
/**
* @return The id
*/
public Integer getId() {
return id;
}
/**
* @param id The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return The name
*/
public String getName() {
return name;
}
/**
* @param name The name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return The description
*/
public String getDescription() {
return description;
}
/**
* @param description The description
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return The icon
*/
public String getIcon() {
return icon;
}
/**
* @param icon The icon
*/
public void setIcon(String icon) {
this.icon = icon;
}
/**
* @return The background
*/
public String getBackground() {
return background;
}
/**
* @param background The background
*/
public void setBackground(String background) {
this.background = background;
}
/**
* @return The color
*/
public String getColor() {
return color;
}
/**
* @param color The color
*/
public void setColor(String color) {
this.color = color;
}
/**
* @return The createdAt
*/
public String getCreatedAt() {
return createdAt;
}
/**
* @param createdAt The created_at
*/
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
/**
* @return The updatedAt
*/
public String getUpdatedAt() {
return updatedAt;
}
/**
* @param updatedAt The updated_at
*/
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
/**
* @return The deletedAt
*/
public Object getDeletedAt() {
return deletedAt;
}
/**
* @param deletedAt The deleted_at
*/
public void setDeletedAt(Object deletedAt) {
this.deletedAt = deletedAt;
}
}
答案 0 :(得分:2)
您有一些数据解析问题。您必须使用ArrayList<FoodCategory>
来获取所有数据列表。
NetworkEngine.getInstance().getFoodCategory(new Callback<ArrayList<FoodCategory>>() {
@Override
public void success(ArrayList<FoodCategory> foodCategory, Response response) {
FragmentStatePager adapter = new FragmentStatePager(YourActivity.this,foodCategory);
mViewPager.getViewPager().setAdapter(adapter);
}
@Override
public void failure(RetrofitError error) {
}
});
因此,将ArrayList<FoodCategory>
传递给PagerAdapter
并使用它的数据制作片段。
制作不同的PagerAdapter
。就像这样。
public class FragmentStatePager extends FragmentStatePagerAdapter {
ArrayList<FoodCategory> foodCategoryArrayList;
ArrayList<String> categorieName = new ArrayList<>();
ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
public FragmentStatePager(FragmentManager fm,ArrayList<FoodCategory> foodCategoryArrayList) {
super(fm);
this.foodCategoryArrayList = foodCategoryArrayList;
for (FoodCategory foodCategory : foodCategoryArrayList) {
categorieName.add(foodCategory.getName());
YourFragment fragment = new YourFragment();
Bundle data = new Bundle();
data.putSerializable("FoodCategoryInfo", foodCategory);
fragment.setArguments(data);
fragmentArrayList.add(fragment);
}
}
@Override
public Fragment getItem(int position) {
return fragmentArrayList.get(position);
}
@Override
public int getCount() {
return fragmentArrayList.size();
}
// If you want to show the title in the tab layout.
@Override
public CharSequence getPageTitle(int position) {
return categorieName.get(position);
}
}
答案 1 :(得分:0)
我按照以下方式执行此操作
首先创建一个抽象的 BaseFragment 类 如下:
public abstract class BaseListFragment extends Fragment implements SearchableFragment {
private BaseListAdapter mAdapter;
public abstract String getType();
public abstract String getCategorySlug();
public abstract String getCategoryId();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_base_list, container, false);
initViews(rootView);
//call performSearch()
return rootView;
}
@Override
public void performSearch() {
// your request
ServiceHelper.getInstance().searchQuery(getCategoryId(), getCategorySlug(), null, null, getType(), null, mPageRequest).enqueue(new Callback<List<ShortVideoModel>>() {
@Override
public void onResponse(Call<List<ShortVideoModel>> call, Response<List<ShortVideoModel>> response) {
processResponse(response);
}
@Override
public void onFailure(Call<List<ShortVideoModel>> call, Throwable t) {
processResponse(null);
}
});
}
}
然后创建你的片段类并继承自 BaseFragment 并实现抽象方法(你可以为不同类型的请求调用传递不同的类型)
public class LatestVideoFragment extends BaseListFragment {
private static final String TYPE_VIDEO_LATEST = "latest";
public static LatestVideoFragment newInstance() {
return new LatestVideoFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void performSearch() {
super.performSearch();
}
@Override
public String getCategorySlug() {
return null;
}
@Override
public String getCategoryId() {
return null;
}
@Override
public String getType() {
return TYPE_VIDEO_LATEST;
}
}
如您所见,您可以根据需要自定义 LatestVideoFragment 。