如何在片段中放置或替换列表

时间:2018-08-10 15:43:04

标签: java android android-studio

我制作了一个测验应用程序,其中使用globalEvent.addEventListenerFragment显示我的问题。 ViewPager来自SQLite,在这里我感到困惑:如果我有10个问题,那么我必须使用10个片段。如何使用1 Question来回答所有问题?

有这个主意吗?

MainActivity

Fragment

我的片段,问题1

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private int mFragmentIndex = 0;
    private ViewPager viewPager;
    private Button back, next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        viewPager = findViewById(R.id.view_pager);
        setupViewPager(viewPager);

        back = findViewById(R.id.buttonBack);
        next = findViewById(R.id.buttonNext);
        back.setOnClickListener(this);
        next.setOnClickListener(this);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new Question1(), "Question1");
        adapter.addFrag(new Question2(), "Question2");
        adapter.addFrag(new Question3(), "Question3");
        viewPager.setOffscreenPageLimit(adapter.getCount());
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        if (view == back) {
            mfragmentIndex--;
            viewPager.setCurrentItem(mFragmentIndex);
        } else if(view == next) {
            mfragmentIndex++;
            viewPager.setCurrentItem(mFragmentIndex);
        }
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitle = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
        public void addFrag(Fragment fragment,String title){
            mFragmentList.add(fragment);
            mFragmentTitle.add(title);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return super.getPageTitle(position);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议使用Fragment的参数来传递问题ID。您已经在Question周围有了一个抽象-似乎唯一真正改变的是问题ID。

例如,您只有一个QuestionFragment,它负责显示任何给定的Question

public class QuestionFragment extends Fragment {
    private static final String ARG_QUESTION_ID = "question_id";

    public static QuestionFragment newInstance(String questionId) {
        final QuestionFragment fragment = new QuestionFragment();
        final Bundle args = new Bundle();
        args.putString(ARG_QUESTION_ID, questionId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final String questionId = getArguments().getString(questionId);
        final DBAdapter dbAdapter = new DBAdapter(getContext());
        questionsList = dbAdapter.getAllQuestions();
        currentQuestion = questionsList.get(questionId);

        // ...

        return view;
    }
}

然后,在设置ViewPager时,只需在创建每个实例时为其提供问题ID:

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(QuestionFragment.newInstance(0), "Question1"));
    adapter.addFrag(QuestionFragment.newInstance(1), "Question2"));
    adapter.addFrag(QuestionFragment.newInstance(2), "Question3"));
    viewPager.setOffscreenPageLimit(adapter.getCount());
    viewPager.setAdapter(adapter);
}