Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate ViewPager dynamically?
    primarykey
    data
    text
    <p>I can't update the content in ViewPager.</p> <p>A question: What is the relationship and correct usage of methods instantiateItem() and getItem() in FragmentPagerAdapter class?</p> <p>I was using only getItem() to instantiate and return my fragments:</p> <pre><code>@Override public Fragment getItem(int position) { return new MyFragment(context, paramters); } </code></pre> <p>This worked well (but as said I can't change the content).</p> <p>So I found this: <a href="https://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view">ViewPager PagerAdapter not updating the View</a></p> <p>Particulary one in which it's talked about method instantiateItem():</p> <blockquote> <p>"My approach is to use the setTag() method for any instantiated view in the instantiateItem() method"</p> </blockquote> <p>So now I want to implement instantiateItem() in order to do that. But I don't know what I have to return there (return is Object) and what is the relation with getItem(int position)?</p> <p>Currently I'm using getItem to instantiate the Fragment, is that wrong? But then do I have to put the fragments in instance variable, or not implement getItem() at all...? I just don't understand it.</p> <p>I tried reading the <a href="http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html" rel="noreferrer">reference</a>:</p> <blockquote> <ul> <li><p>public abstract Fragment getItem (int position)</p> <p>Return the Fragment associated with a specified position.</p></li> <li><p>public Object instantiateItem (ViewGroup container, int position)</p> <p>Create the page for the given position. The adapter is responsible for adding the view to the container given here, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup). Parameters</p> <p>container The containing View in which the page will be shown. position The page position to be instantiated.</p> <p>Returns</p> <p>Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.</p></li> </ul> </blockquote> <p>... but I still don't understand how are they related and what I have to do.</p> <p>Here's my code. I'm using support package v4.</p> <p>ViewPagerTest </p> <pre><code>public class ViewPagerTest extends FragmentActivity { private ViewPager pager; private MyFragmentAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pager1); pager = (ViewPager)findViewById(R.id.slider); String[] data = {"page1", "page2", "page3", "page4", "page5", "page6"}; adapter = new MyFragmentAdapter(getSupportFragmentManager(), 6, this, data); pager.setAdapter(adapter); ((Button)findViewById(R.id.button)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { reload(); } }); } private void reload() { String[] data = {"changed1", "changed2", "changed3", "changed4", "changed5", "changed6"}; //adapter = new MyFragmentAdapter(getSupportFragmentManager(), 6, this, data); adapter.setData(data); adapter.notifyDataSetChanged(); pager.invalidate(); //pager.setCurrentItem(0); } } </code></pre> <p>MyFragmentAdapter</p> <pre><code>class MyFragmentAdapter extends FragmentPagerAdapter { private int slideCount; private Context context; private String[] data; public MyFragmentAdapter(FragmentManager fm, int slideCount, Context context, String[] data) { super(fm); this.slideCount = slideCount; this.context = context; this.data = data; } @Override public Fragment getItem(int position) { return new MyFragment(data[position], context); } @Override public int getCount() { return slideCount; } public void setData(String[] data) { this.data = data; } @Override public int getItemPosition(Object object) { return POSITION_NONE; } } </code></pre> <p>MyFragment</p> <pre><code>public final class MyFragment extends Fragment { private String text; public MyFragment(String text, Context context) { this.text = text; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.slide, null); ((TextView)view.findViewById(R.id.text)).setText(text); return view; } } </code></pre> <p>Here is also somebody with a similar problem, no answers...</p> <p><a href="http://www.mail-archive.com/android-developers@googlegroups.com/msg200477.html" rel="noreferrer">http://www.mail-archive.com/android-developers@googlegroups.com/msg200477.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload