Nishant
Nishant

Reputation: 32233

How to repopulate each Fragments in ViewPager?

I am creating an application in which I have to display question category and questions related to each category. Each question category have different number of questions.On selecting a category I have to display question screen as a sliding screen means user can move to next question on sliding. So, I desgined a screen with two Fragment. First fragment is a ListFragment to display List of category and another is ViewPager to display sliding questions. I was able to populate the Question Fragments in ViewPager for the First Category(containing 3 ques) , but when I select another Category(containing 5 ques) from the List I was not able to replace the previous three Fragment. it just added last two questions in ViewPager. I want to repopulate ViewPager with new set of data. I have added the code below.

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;
    }
}
}

QuestionSection

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();
            }
        }
    });
}   
}

Upvotes: 1

Views: 591

Answers (1)

Shakti
Shakti

Reputation: 1581

ViewPager.setAdapter(pager_adapter)

forces a redraw of the ViewPager. Make appropriate changes first and do this for a refresh.

Edit: Adapter.notifyDataSetChanged() invalidates the existing data to prepare for new one. Do look into that if you are not using it till now.

Upvotes: 1

Related Questions