Update gridview in viewpager - android

I have been searching for a while for a way to refresh a gridview within a viewpager and I am yet to find an answer. More specificaly my problem is that I have a view pager with three gridviews. Each gridview is populated by an arraylist and if the arraylist is changed the gridview stays the same. How can I make it update? I have tried calling notifydatasetchange on the adapter and that doesnt help.
here is my code for the view pager adapter
public class FavoritesViewPager extends FragmentStatePagerAdapter {
public FavoritesViewPager(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
UserDatabaseHandler UDH = new UserDatabaseHandler(getActivity());
switch (i) {
case 0:
ArrayList<Exercise> strengthList = UDH.getFavoriteExercises("strengthexercises");
UDH.close();
return FavoriteExercises.newInstance(strengthList);
case 1:
ArrayList<Exercise> stretchList = UDH.getFavoriteExercises("stretchingexercises");
UDH.close();
return FavoriteExercises.newInstance(stretchList);
case 2:
ArrayList<Exercise> warmupList = UDH.getFavoriteExercises("warmupexercises");
UDH.close();
return FavoriteExercises.newInstance(warmupList);
default:
ArrayList<Exercise> temp = UDH
.getFavoriteExercises("warmupexercises");
UDH.close();
return FavoriteExercises.newInstance(temp);
}
}
#Override
public int getCount() {
return 3;
}
}

If your pager has only two pages, you should use FragmentPagerAdapter. You can get the fragment by findFragmentByTag then call update method of this fragment.
String fragmentName = makeFragmentName(_viewPager.getId(), i);
Fragment frag = _fragmentManager.findFragmentByTag(fragmentName);
private static String makeFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}

Related

Re-use fragments of ViewPager & FragmentPagerAdapter

How to reuse the fragments inside view pager ???
I have view pager and inside that Having 3 fragments.
I'm creating new instance of fragment to all time. So It's take more space in memory.
In this 3 fragments having perform same functionality. While Im creating new instance every time. It's having more problems like.
1) It take times to keyboard open and close.
2) Scrollview take times to scroll.
3) Edit text take time to focus on and off.
https://prnt.sc/or86ec
Here screen that I have implemented viewpager with 3 fragment. In swipe it's look like same.
I have created Pager adapter:
public class ProposalDetailPagerAdapter : FragmentStatePagerAdapter, CardAdapter
{
private static int NUM_ITEMS = 3;
private float mBaseElevation;
public ProposalDetailPagerAdapter(Android.Support.V4.App.FragmentManager fm, float baseElevation) : base(fm)
{
mBaseElevation = baseElevation;
}
public override int Count => NUM_ITEMS;
public override int GetItemPosition(Java.Lang.Object #object)
{
if (#object is ProposalDetailFragment)
{
ProposalDetailFragment fragment = (ProposalDetailFragment)#object;
if (fragment != null)
{
fragment.UpdateFragmentAfterCreate();
}
}
return base.GetItemPosition(#object);
}
public int getCount()
{
return NUM_ITEMS;
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
switch (position)
{
case 0:
return OpenFragment(0);
case 1:
return OpenFragment(1);
case 2:
return OpenFragment(2);
default:
return null;
}
}
private Android.Support.V4.App.Fragment OpenFragment(int position)
{
ProposalDetailFragment createdFragment = new ProposalDetailFragment();
Bundle bundle = new Bundle();
bundle.PutInt("position", position);
createdFragment.Arguments = bundle;
return createdFragment;
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
switch (position)
{
case 0:
return OpenFragment(0);
case 1:
return OpenFragment(1);
case 2:
return OpenFragment(2);
default:
return null;
}
}
}
In this adapter, created 3 fragments. But It's having lot's of memory problem. Because lot's of condition having in fragments. and created 3 instance of memory. So How to resolve this issue ???
Here is a solution :
In ProposalDetailPagerAdapter , can override DestroyItem method.And comment out the method of calling the parent class to avoid repeated creation of the fragment.
As follow:
public class ProposalDetailPagerAdapter : FragmentStatePagerAdapter, CardAdapter
{
...
public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object #object)
{
//base.DestroyItem(container, position, #object);
// Comment out the calling parent class
}
...
}

Fragment update view in view pager

I am using view pager which is having 3 fragments
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private static final String TAG="ViewPageAdapter";
private final int PAGES = 3;
private String[] title = new String[]{"Frag1",
"Frag2", "Frag2"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Log.d(TAG, "position is " + position);
switch (position) {
case 0:
return new Frag1();
case 1:
return new Frag2();
case 2:
return new Frag3();
default:
throw new IllegalArgumentException(
"The item position should be less or equal to:" + PAGES);
}
}
#Override
public int getCount() {
return PAGES;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
I am setting view pager from main activity:
viewPageAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.addOnPageChangeListener(onPageChangeListener);
viewPager.setAdapter(viewPageAdapter);
When i execute viewPageAdapter.notifyDataSetChanged();
or viewPager.setAdapter(viewPageAdapter); method to recreate fragments the activity is getting destroyed.don't know what causing the issue.
I checked many solution nothing worked.
ViewPager PagerAdapter not updating the View
Update ViewPager dynamically?
I am asking another problem because which is related to above question.
The problem which is described in link
ViewPager onPageSelected for first page
where i am not able to get first page tab title on load.i am using android.support.v4.view.PagerTabStrip
Please help me in getting solved. Wasted my whole day to get rid of this :(

Android fragmentpageradapter update data

Here , I explain my problem. I install a ViewPager and FragmentPagerAdapter so you can slide between two page.
Each fragment contains data ( TextView ) .
When creating my ViewPager with the data passed as a parameter everything goes well.
But if I want to change the data (ie the content of the fragment) it does not refresh
public class MaPagerAdapter extends FragmentPagerAdapter {
private Meteo[]lameteo;
public MaPagerAdapter(FragmentManager fm, Meteo[]lameteo) {
super(fm);
this.lameteo = lameteo;
Log.i("aa", "ville maPagerAdapter"+lameteo[0].getLoc());
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0: return page_droite.newInstance(lameteo[0]);
case 1: return page_gauche.newInstance(lameteo);
}
return null;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
public void setData(Meteo[] meteo){
this.lameteo=meteo;
}
#Override
public int getCount() {
return 2;
}
}
And my main
if(mPagerAdapter!=null){ //if pagerAdapter already initialized
mPagerAdapter.setData(meteo);
mPagerAdapter.notifyDataSetChanged();
}else
mPagerAdapter = new MaPagerAdapter(getSupportFragmentManager(), meteo);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
Can you help me please ?
No, the pager adapter doesn't work that way. Android doesn't realize that you have that particular data in your fragments, so it wouldn't know how to update it.
You'll have to get references to your fragments inside your setData call, and then you'll have to write new methods inside your fragments that you can call to reset their data. Inside the fragments, you'll have to make sure that you update the fragment's views with the new data. But as for you adapter, you can do something like this:
public class MaPagerAdapter extends FragmentPagerAdapter {
private Meteo[]lameteo;
private FragmentManager fm;
public MaPagerAdapter(FragmentManager fm, Meteo[]lameteo) {
super(fm);
this.fm = fm;
this.lameteo = lameteo;
Log.i("aa", "ville maPagerAdapter"+lameteo[0].getLoc());
}
public void setData(Meteo[] meteo){
this.lameteo=meteo;
page_droite droite = (page_droite) getFragment(0);
droite.setData(meteo[0]);
page_gauche gauche = (page_gauche) getFragment(1);
gauche.setData(meteo);
}
private Fragment getFragment(int position) {
String tag = "android:switcher:" + R.id.viewpager + ":" + getItemId(position);
return fm.findFragmentByTag(tag);
}
Note that there is no need to call notifyDataSetChanged in this case, since you still have the same fragment in your adapter. When you implement the setData functions in your fragments, you need to make all the changes then.

How To Start Second tab's Content By View pager Adaptor android

i am Suffering In Little problem .My Second Tab Of Fragment Is Not Performing The Task
my first tab is working
i m using View Page Adapter for Tab
Here Is Tutorial same way of this tutorial i used
but my second tab showing nothing
please help me ..
i will appreciate your suggestion
thanks in adavance
here is my view page adapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 2;
private String titles[] = new String[] { "Friends Request", "FriendsList" };
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
MyFriendsList fragmenttab1 = new MyFriendsList();
return fragmenttab1;
// Open Friendlist.java
case 1:
FriendsList fragmenttab2 = new FriendsList();
return fragmenttab2;
}
return null;
}
public CharSequence getPageTitle(int position) {
return titles[position];
}
#Override
public int getCount() {
return PAGE_COUNT;
}
}
in this my first tab of myfriendlist is working but Why Second tab Is not Calling It show blank page in frienlist
please help me

How to set Activity-Class es for each page of ViewPager in android?

i want to set different pages for ViewPager from class. for example:
i have 4 activity A + B + C and D.
A include my ViewPager.
B +C + D are different pages that must be shown in ViewPager.(each of them have different contex)
my question is that, how can i recognize these pages into ViewPager?
thanks
You can add but better way is that you will create a activity and your four activity should be child fragment of the same .
In short your all activity should be fragments jst load the fregment when you swipe viewpage indicater
sample code for adapter
//User adapter according condition
final FragmentPagerAdapter adapter = new FragmentChildPageAdapter(getChildFragmentManager());
//final FragmentPagerAdapter adapter = new FragmentChildPageAdapter(getActivity().getSupportFragmentManager()));
final ViewPager pager = (ViewPager) v.findViewById(R.id.pager);
pager.setAdapter(adapter);
final TabPageIndicator indicator = (TabPageIndicator) v.findViewById(R.id.indicator);
indicator.setViewPager(pager);
//Adapter
class FragmentChildPageAdapter extends FragmentPagerAdapter implements
IconPagerAdapter {
public FragmentChildPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
//first fregment
break;
case 1:
//Second fregment
break;
case 2:
//third fragment
break;
case 3:
//Forth fragment
break;
}
return fragment;
}
#SuppressLint("DefaultLocale")
/*#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}*/
#Override
public int getCount() {
//return CONTENT.length;
return CONTENT.size();
}
#Override
public int getIconResId(int position) {
return (Integer) Icon.get(position % CONTENT.size());
}
}

Categories

Resources