作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个测验应用程序,我在其中使用 Fragment
和 ViewPager
显示我的问题。 Question
取自 SQLite,这里我很困惑:如果我有 10 个问题,那么我必须使用 10 个 fragment 。如何使用 1 个 Fragment
容纳所有问题?
有这方面的想法吗?
主要 Activity :
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);
}
}
}
我的 fragment ,问题 1:
public class Question1 extends Fragment {
private List<Question> questionsList;
private Question currentQuestion;
private TextView txtQuestion, tvNoOfQs;
private RadioButton rbtnA, rbtnB, rbtnC, rbtnD;
private int obtainedScore = 0;
private int questionId = 0;
private int answeredQsNo = 0;
ArrayList<String> myAnsList;
public Question1() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final DBAdapter dbAdapter = new DBAdapter(getContext());
questionsList = dbAdapter.getAllQuestions();
currentQuestion = questionsList.get(questionId);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_question1, container, false);
final RadioGroup grp = (RadioGroup)view.findViewById(R.id.radioGroup1);
tvNoOfQs = (TextView)view.findViewById(R.id.tvNumberOfQuestions);
txtQuestion = (TextView)view.findViewById(R.id.tvQuestion);
rbtnA = (RadioButton)view.findViewById(R.id.radio0);
rbtnB = (RadioButton)view.findViewById(R.id.radio1);
rbtnC = (RadioButton)view.findViewById(R.id.radio2);
rbtnD = (RadioButton)view.findViewById(R.id.radio3);
setPertanyaan();
myAnsList = new ArrayList<String>();
return view;
}
private void setPertanyaan() {
rbtnA.setChecked(false);
rbtnB.setChecked(false);
rbtnC.setChecked(false);
rbtnD.setChecked(false);
answeredQsNo = questionId+1;
tvNoOfQs.setText(+answeredQsNo+"/"+questionsList.size());
txtQuestion.setText(currentQuestion.getQUESTION());
rbtnA.setText(currentQuestion.getOptionA());
rbtnB.setText(currentQuestion.getOptionB());
rbtnC.setText(currentQuestion.getOptionC());
rbtnD.setText(currentQuestion.getOptionD());
questionId++;
}
}
最佳答案
我建议使用 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);
}
关于java - 如何在 fragment 中放置或替换列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51789965/
我是一名优秀的程序员,十分优秀!