在ViewPager中创建包含视图的动态片段

时间:2017-03-13 16:50:21

标签: android android-fragments android-viewpager android-json

我想要实现的是根据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": 

我想要的结果是:

So as you can see there are 3 fragemnts following the amount of objects in the JSONArray.

因此,您可以看到JSONArray中的对象数量有3个片段

我应该遵循什么方法?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我建议使用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();
    }
}
}

注意:此代码未运行,因为我已从之前的项目中进行了调整。但是工作示例不应该与提供的不同。 如果您对上述代码有任何疑问,请与我们联系。 祝你好运,编码愉快。