使用片段实现滑动视图

时间:2016-06-29 10:37:42

标签: android swipe android-tablayout uiswipegesturerecognizer swipe-gesture

我正在设计一个宪法应用程序,我想使用带有滑动视图的选项卡式布局。选项卡使用自定义适配器从数据库中获取数据。由于数据大小(片段中没有)是未知的,我希望每次滑动都生成一个新的视图,这是章程中不同的章节内容。

我想要的东西看起来像下面的字典应用程序,两边都有那些滑动标签。我熟悉标签,但我很想获得一个资源来帮助我实现这一点,因为我看到的大多数文档都没有解释这一点。感谢

enter image description here

1 个答案:

答案 0 :(得分:0)

使用您想要的OutPut修改此内容

的onCreate

 ArrayList<McqQuestionBean> mcqQuestionBeans= new ArrayList<McqQuestionBean>();
        adapter = new NewsFragmentPagerAdapter(getSupportFragmentManager(),
                mcqQuestionBeans, MCQTestActivity.this);
        pager.setAdapter(adapter);

基础适配器

public class NewsFragmentPagerAdapter extends FragmentStatePagerAdapter {

private ArrayList<McqQuestionBean> mcqQuestionBeans;

private McqQuestionFragment fragment;
private Activity context;

public NewsFragmentPagerAdapter(FragmentManager fm, ArrayList<McqQuestionBean> mcqQuestionBeans, Activity context) {
    super(fm);
    this.mcqQuestionBeans = mcqQuestionBeans;

    this.context = context;

}

public void update(ArrayList<McqQuestionBean> mcqQuestionBeans) {
    this.mcqQuestionBeans = mcqQuestionBeans;
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return mcqQuestionBeans.size();
}

@Override
public int getItemPosition(Object object) {
    // TODO Auto-generated method stub
    return super.getItemPosition(object);
}

@Override
public Fragment getItem(int position) {

    fragment = McqQuestionFragment.newInstance(mcqQuestionBeans.get(position), position, context);

    return fragment;

}

}

你的片段McqQuestionFragment

public class McqQuestionFragment extends Fragment {

private int position, porrefid;
    private String question;
    private ArrayList<McqQuestionChoiceBean> choices;   



@SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        position = getArguments().getInt("position");
        porrefid = getArguments().getInt("porrefid");
        userMarkedOn = getArguments().getInt("userMarkedOn");
        question = getArguments().getString("question");
        choices = (ArrayList<McqQuestionChoiceBean>) getArguments()
                .getSerializable("choices");
    }

    public static McqQuestionFragment newInstance(
            McqQuestionBean mcqQuestionBean, int position, Activity activity) {
        final McqQuestionFragment f = new McqQuestionFragment();
        final Bundle args = new Bundle();
        args.putString("question", mcqQuestionBean.getQuestion());
        args.putInt("position", position);
        args.putInt("userMarkedOn", mcqQuestionBean.getUserCorrectedOn());
        args.putSerializable("choices", mcqQuestionBean.getChoices());
        args.putInt("porrefid", mcqQuestionBean.getPorrefid());
        f.setArguments(args);

        return f;
    }

}