ViewPager and ListFragment

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Aug 21, 2015 4:28:27 PM via Website

My code consists of a ListFragment and a new Fragment(DetailFragment and DetailActivity contains the ViewPager) opens when you click the item in the list.

My problem is that swiping the DetailsFragment works but it keeps recreating the same Fragment which I opened. New Fragments are not created.(The Title I have in EditText for DetailFragment keeps displaying the same title after swiping when its supposed to show the Title of the next Item in my ListView).

Please check the DetailsFragment because that's where I think the error exists.

The Activity which opens when an item is clicked

public class NoteDetailActivity extends FragmentActivity {

private ViewPager noteViewPager;
private int Index; // index for Pager related positioning of pages
private ArrayList noteList;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);

noteViewPager = new ViewPager(this);
noteViewPager.setId(R.id.NoteViewPager);
setContentView(noteViewPager);

noteList = NoteLab.getNoteLabInstance().getNoteList();

noteViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager())
{

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        NoteData note = noteList.get(position);
        UUID id = note.getID();
        return NoteDetailFragment.newInstance(id);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return noteList.size();
    }

});


/*Below methods sets the appropriate Page Details for the Item selected from my ListFragment */
UUID noteID = (UUID) getIntent().getSerializableExtra(NoteListFragment.NoteID);
for(Index=0 ; Index<noteList.size();Index++)
{
    if(noteList.get(Index).getID().equals(noteID))
    {
        noteViewPager.setCurrentItem(Index);
        break;
    }
}

noteViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub
        /* Setting Head Title for each Page */
        NoteData note = noteList.get(Index);
        if(note.getTitle() != null)
        {
            setTitle(note.getTitle());
        }

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }
});

}
}

Corresponding Fragment which is supposed to load by Pager Adapter

public class NoteDetailFragment extends Fragment {

private EditText titleText;
private NoteData note;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
UUID NoteID = (UUID) getActivity().getIntent().getSerializableExtra(NoteListFragment.NoteID);
note = NoteLab.getNoteLabInstance().getNoteForID(NoteID); // NoteLab is a Singleton.
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_note_detail,container,false);

    titleText = (EditText) view.findViewById(R.id.Note_Title_Details_Fragment);
    titleText.setText(note.getTitle());         
return view;

}
public static NoteDetailFragment newInstance(UUID id)
{
NoteDetailFragment noteFrag = new NoteDetailFragment();
Bundle b = new Bundle();
b.putSerializable(NoteListFragment.NoteID, id);

//Indication of Error:The below log statement displays my package name and random junk value
Log.d("MyLog", "newInstance i NoteDetFrg Task title ="+NoteLab.getNoteLabInstance().getNoteForID(id)+ "ID = "+id);
noteFrag.setArguments(b);
return noteFrag;
}
}