1

我正在创建一个应用程序,我必须在其中显示问题类别以及与每个类别相关的问题。每个问题类别都有不同数量的问题。在选择一个类别时,我必须将问题屏幕显示为滑动屏幕,这意味着用户可以通过滑动移动到下一个问题。所以,我设计了一个有两个Fragment. 第一个片段是一个ListFragment显示类别列表,另一个是ViewPager显示滑动问题。我能够ViewPager为第一个类别(包含 3 个问题)填充问题片段,但是当我从列表中选择另一个类别(包含 5 个问题)时,我无法替换前三个Fragment。它只是添加了最后两个问题ViewPager。我想ViewPager用新的数据集重新填充。我在下面添加了代码。

QuestionFragmentActivity

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.questions_activity);

        if (findViewById(R.id.question_section_fragment_container) != null) {
            /*
             * if (getIntent().getExtras() != null) { return; }
             */
            QuestionSection firstFragment = new QuestionSection();
            firstFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.question_section_fragment_container,
                            firstFragment).commit();
        }

        categoryPosition = 0;//static variable to hold the position of category item selected.
        CategoryVO cvo = QuestionFragmentActivity.categoryList
                .get(categoryPosition);

        if (findViewById(R.id.question_fragment_container) != null) {
            /*
             * if (getIntent().getExtras() != null) { return; }
             */
            QuestionContainer firstFragment = new QuestionContainer();
            firstFragment.setCvo(cvo);
            firstFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.question_fragment_container,
                            firstFragment).commit();
        }
   }

QuestionContainer.java

public class QuestionContainer extends Fragment {
ViewPager mPager;
CategoryVO cvo;

public CategoryVO getCvo() {
    return cvo;
}
public void setCvo(CategoryVO cvo) {
    this.cvo = cvo;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.question_container, container,
            false);
    ArrayList<QuestionFragment> fragmentList=new ArrayList<QuestionFragment>();
    for(QuestionVO qvo: cvo.getQuestionList()){
        fragmentList.add(new QuestionFragment(qvo));
    }
    mPager = (ViewPager) view.findViewById(R.id.question_viewpager);
    MyAdapter mAdapter = new MyAdapter(getActivity().getSupportFragmentManager());
    mAdapter.setFragmentList(fragmentList);
    mPager.setAdapter(mAdapter);
    return view;
}

public static class MyAdapter extends FragmentPagerAdapter {
    ArrayList<QuestionFragment> fragmentList;       

    public ArrayList<QuestionFragment> getFragmentList() {
        return fragmentList;
    }
    public void setFragmentList(ArrayList<QuestionFragment> fragmentList) {
        this.fragmentList = fragmentList;
    }
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public Fragment getItem(int position) {
                    return fragmentList.get(position);
    }
    @Override
    public int getItemPosition(Object object) {
            return POSITION_NONE;
    }
}
}

问题部分

public class QuestionSection extends ListFragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.list_fragment1, container, false);
    return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Button hideListSection = (Button) getActivity().findViewById(
            R.id.btn_hide_section);
    hideListSection.setOnClickListener(this);
    ListView lv = getListView();
    ArrayList<String> listItem = new ArrayList<String>();
    for (CategoryVO cvo : QuestionFragmentActivity.categoryList) {
        listItem.add(cvo.getCategoryName());
    }
    lv.setAdapter(new SectionListAdapter(getActivity(), listItem));
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos,
                long arg3) {
            if (pos == 1) {
                  FragmentManager fragmentManager = getFragmentManager();
                  FragmentTransaction fragmentTransaction = fragmentManager
                  .beginTransaction();
                  QuestionFragmentActivity.categoryPosition=pos; CategoryVO cvo=
                  QuestionFragmentActivity.categoryList.get(pos);

                  QuestionContainer fragment = new QuestionContainer();
                  fragment.setCvo(cvo);
                  fragmentTransaction.replace(R.id.question_fragment_container
                  , fragment); fragmentTransaction.commit();
            }
        }
    });
}   
}
4

1 回答 1

1
ViewPager.setAdapter(pager_adapter)

强制重绘 ViewPager。首先进行适当的更改并执行此操作以进行刷新。

编辑: Adapter.notifyDataSetChanged() 使现有数据无效以准备新数据。如果您直到现在还没有使用它,请调查一下。

于 2012-11-24T17:53:18.850 回答