在 ViewPager 中使用视图创建动态片段
Posted
技术标签:
【中文标题】在 ViewPager 中使用视图创建动态片段【英文标题】:Create dynamic Fragments with Views in ViewPager 【发布时间】:2017-08-03 19:02:41 【问题描述】:我想要实现的是根据我在 JSONArray 中拥有的对象数量生成动态片段。
例如,在下面的 JSON 中,我在一个数组中有 3 个对象:
“JSONArray”: [
"CurrentStep": false,
"Id": 1,
"MessageOne": "Confirm your location",
"MessageTwo": "Prrrrrrrrrrrr”,
"PercentageComplete": 100,
"StepName": "Prescription Created"
,
"CurrentStep": true,
"Id": 2,
"MessageOne": "Location confirmed",
"MessageTwo": “rrrrrrrrrrrrrrrr”,
"PercentageComplete": 0,
"StepName": "Location Confirmed"
,
"CurrentStep": false,
"Id": 3,
"MessageOne": "Pharmacies contacted",
"MessageTwo": “rrrrrrrrrrrr”,
"PercentageComplete": 0,
"StepName": "Pharmacies Contacted"
,
"CurrentStep": false,
"Id": 10,
"MessageOne": "Ready for collection",
"MessageTwo": rrrrrrrrrr,
"PercentageComplete": 0,
"StepName":
我想要的结果是这样的:
如您所见,在 JSONArray 中的对象数量之后有 3 个片段
我应该遵循什么方法?有什么想法吗?
【问题讨论】:
在这里查看我的答案 - ***.com/a/42758986/1649353 【参考方案1】:我建议使用loader pattern 来加载您的数据并触发 onDataSetChanged 或相应地设置适配器。下面是我在许多项目中使用的基本 viewpager 类。
public abstract class BaseDataPagerActivity extends BaseViewPagerActivity implements LoaderManager.LoaderCallbacks<List<BaseData>>
public static final String DATA_ID_KEY = "data_id";
private static final int DATA_LOADER_TASK_ID = 42;
protected YourData mData;
protected long mDataId;
protected BasePagerAdapter mPagerAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
mPager = (ViewPager) findViewById(R.id.view_pager);
setupLoader(getIntent());
@Override
protected void onResume()
super.onResume();
if (mData != null && mData.getCreatedAtDate() != null)
getSupportLoaderManager().restartLoader(DATA_LOADER_TASK_ID, null, this);
@Override
protected void onPause()
super.onPause();
saveData();
protected abstract BaseDataPagerAdapter getPagerAdapter(FragmentManager fm, YourData data);
protected void setupLoader(Intent intent)
mDataId = intent.getLongExtra(DATA_ID_KEY, -1);
if (mData != null)
setupAdapter();
else if (mDataId >= 0L)
getSupportLoaderManager().initLoader(DATA_LOADER_TASK_ID, null, this);
public void nextPage()
int pagerCount = mPagerAdapter.getCount();
if (mPager.getCurrentItem() + 1 < pagerCount)
mPager.setCurrentItem(mPager.getCurrentItem() + 1, true);
else
finish();
private void saveData()
if (mData != null)
//TODO: DO something here to save data
private void setupAdapter()
if (mData != null && mPager != null)
if (mPagerAdapter != null)
mPagerAdapter.notifyDataSetChanged();
else
mPagerAdapter = getPagerAdapter(getSupportFragmentManager(), mData);
mPager.setAdapter(mPagerAdapter);
@Override
public Loader<List<BaseData> onCreateLoader(int id, Bundle args)
return new DataLoader(this, mDataId);
@Override
public void onLoadFinished(Loader<List<BaseData>> loader, List<BaseData> data)
mDataList = data;
setupAdapter();
@Override
public void onLoaderReset(Loader<List<BaseData>> loader)
mData = null;
这是允许您根据类型更改片段的适配器。请注意,您需要一个具有 getType 方法的基类。
public class BasicPagerAdapter extends FragmentStatePagerAdapter
private static final int TYPE_A_FRAGMENT = 0;
private static final int TYPE_B_FRAGMENT = 1;
List<BaseData> mDataList;
private static class BaseData
private int mType = -1;
public BaseData(int type)
mType = type;
public int getType() return mType;
public BasicPagerAdapter(FragmentManager fm, List<BaseData> dataList)
super(fm);
mDataList = dataList;
@Override
public Fragment getItem(int position)
Fragment result = null;
BaseData currentData = mDataList.get(position);
if(currentData.getType() != -1)
switch (currentData.getType())
case TYPE_A_FRAGMENT:
result = TypeAFragment.newInstance(mDataList.get(position);
break;
case TYPE_B_FRAGMENT:
result = TypeBFragment.newInstance(mDataList.get(position));
break;
return result;
@Override
public int getCount()
if(mDataList != null)
mDataList.size();
注意:此代码未运行,因为我已从以前的项目中对其进行了改编。但工作示例不应与提供的示例相差太大。
【讨论】:
以上是关于在 ViewPager 中使用视图创建动态片段的主要内容,如果未能解决你的问题,请参考以下文章