我正在尝试制作一种测试应用程序,您可以通过向左或向右刷动来更改问题。因此,我有一个FragmentActivity、一个ViewPager和一个FragmentPagerAdapter。
每个新页面,FragmentPagerAdapter使用相同的布局实例化一个新的Fragment,布局上有一个ListView,总共有5个页面。然后,ArrayAdapter将ListView填充到4个CheckedTextViews,当用户选择一个时,背景会改变。
问题在于,当您从一个问题滑动到另一个问题时,所选项会丢失,并且当用户向后滑动到先前的问题时,该选项不再被选中。
虽然我是新手,但在直接从xml膨胀复选框时,它们的选择状态不会丢失。
以下是一些代码:
FragmentActivity onCreate():
每个新页面,FragmentPagerAdapter使用相同的布局实例化一个新的Fragment,布局上有一个ListView,总共有5个页面。然后,ArrayAdapter将ListView填充到4个CheckedTextViews,当用户选择一个时,背景会改变。
问题在于,当您从一个问题滑动到另一个问题时,所选项会丢失,并且当用户向后滑动到先前的问题时,该选项不再被选中。
虽然我是新手,但在直接从xml膨胀复选框时,它们的选择状态不会丢失。
以下是一些代码:
FragmentActivity onCreate():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
//Getting the array of questions.
res = getResources();
questions = res.getStringArray(R.array.questions_1);
// Create the adapter that will return a fragment for each of the five
// questions of the app.
mAdapter = new MyAdapter(getSupportFragmentManager());
// Set up the ViewPager with the FragmentPagerAdapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(10);
mViewPager.setAdapter(mAdapter);
}
FragmentPagerAdapter:
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
//Number of pages
return 5;
}
@Override
public Fragment getItem(int position) {
ArrayListFragment fragment = ArrayListFragment
.newInstance(position);
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
return getString(R.string.title_section) + " " + (position + 1);
}
}
片段类:
public static class ArrayListFragment extends Fragment {
//Current Page number.
int mNum;
public static final String ARG_SECTION_NUMBER = "section_number";
ListView list;
/**
* Create a new instance of ArrayListFragment, providing "num" as an
* argument.
*/
static ArrayListFragment newInstance(int num) {
ArrayListFragment f = new ArrayListFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment's UI is a textview showing a question and the ListView with
* the four possible answers.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container,
false);
View tv = v.findViewById(R.id.text);
//Set the Question
((TextView) tv).setText(questions[mNum]);
//Get que ListView and fill it.
list = (ListView) v.findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.list_item, myStringArray));
//Select an item.
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
arg1.setSelected(true);
}
});
return v;
}
}