The size of GridView keeps growing when PageAdapter (Android)

Xhulio

Solved( I just needed to change from FragmentPagerAdapter to FragmentStatePagerAdapter becuase the last one saves the states of a fragment that is not visible in the screen. So when you have to many fragments, which are not visible, the memory, information may leak and the activity maybe destroyed. This is what happend with the GridView ( Problem below) Adding FragmentStatePagerAdapter will save the state of the fragment when it becomes visible again.

I am developing an application for on-line shopping. Due to the big number of products that I have to display I have used a lot of categorization. One of the main feature that I am using is ViewPager with PageAdapter. In every fragment it's supposed to be different layouts. In one of the fragments I have used a GridView. The problem is that when I switch between pages, and I return to the first page the size of the GridView , the number of elements inside keeps growing repeating themselves. For example if I have 12 elements at the beginning, after I switch pages and then get back again it becomes 24, 36 etc.

Below is the code I used for this: EbuyHomeScreen.class

 public class EbuyHomeScreen extends FragmentActivity {

        @SuppressLint("InlinedApi") protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.ebuy_home_screen);

            /** Getting a reference to the ViewPager defined the layout file */        
            ViewPager pager = (ViewPager) findViewById(R.id.pager);

            /** Getting fragment manager */
            FragmentManager fm = getSupportFragmentManager();

            /** Instantiating FragmentPagerAdapter */
            EbuyFragmentPagerAdapter pagerAdapter = new EbuyFragmentPagerAdapter(fm);

            /** Setting the pagerAdapter to the pager object */
            pager.setAdapter(pagerAdapter);
        }

EbuyFragmentPagerAdapter

 public class EbuyFragmentPagerAdapter extends FragmentStatePagerAdapter{

        final int PAGE_COUNT = 3;

        /** Constructor of the class */
        public EbuyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        /** This method will be invoked when a page is requested to create */
        @Override
        public Fragment getItem(int position) {
            switch (position) {

            case 0:
                EbuyFragmentLatest myFragmentLatest = new EbuyFragmentLatest();
                return myFragmentLatest;

            case 1:

                EbuyFragmentSold myFragmentSold = new EbuyFragmentSold();
                return myFragmentSold;

            case 2:
                EbuyFragmentSponsored myFragmentSponsored = new EbuyFragmentSponsored();
                return myFragmentSponsored;

            default:

                EbuyFragmentLatest myFragmentLatestDefault = new EbuyFragmentLatest();
                return myFragmentLatestDefault;
            }

        }

        /** Returns the number of pages */
        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            /** code for returning the title*/      }
        }

    }

EbuyFragmentLatest

    public class EbuyFragmentLatest extends Fragment {

        final ArrayList<EbuyItem> ebuy_data = new ArrayList<EbuyItem>();
        private EbuyItemAdapter customAdapter;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.ebuy_home_fragment_latest,
                    container, false);
            JazzyGridView  mGrid = (JazzyGridView)v.findViewById(R.id.ebuy_list);
            mGrid.setTransitionEffect(JazzyHelper.TILT);

            ebuy_data.add(new EbuyItem("http://www.ebuy.al/Images/dsc/8884_400_300.jpg","Fustan Glamour","2,500L [17.9 €]"));
            /*
                ** filing with data continues */

            customAdapter = new EbuyItemAdapter(
                    container.getContext(), ebuy_data);
                    mGrid.setAdapter(customAdapter);
            return v;
        }
    }

EbuyItemAdapter

public class EbuyItemAdapter extends BaseAdapter {

    private ArrayList<EbuyItem> ebuy_data;

    private LayoutInflater layoutInflater;

    public EbuyItemAdapter(Context context, ArrayList<EbuyItem> ebuy_data) {
        this.ebuy_data = ebuy_data;
        layoutInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return ebuy_data.size();
    }

    public Object getItem(int position) {
        return ebuy_data.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({ "DefaultLocale", "InflateParams" })
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.ebuy_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.price = (TextView) convertView.findViewById(R.id.price);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.ebuy_image);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(((EbuyItem) ebuy_data.get(position)).getName());
        holder.price.setText(((EbuyItem) ebuy_data.get(position)).getPrice());
        UrlImageViewHelper.setUrlDrawable(holder.image,
                ((EbuyItem) ebuy_data.get(position)).getImageUrl(),
                R.drawable.loading);

        return convertView;
    }

    static class ViewHolder {

        TextView name;
        TextView price;
        ImageView image;
    }

}

Xhulio

Solved( I just needed to change from FragmentPagerAdapter to FragmentStatePagerAdapter becuase the last one saves the states of a fragment that is not visible in the screen. So when you have to many fragments, which are not visible, the memory, information may leak and the activity maybe destroyed. This is what happend with the GridView. Adding FragmentStatePagerAdapter will save the state of the fragment when it becomes visible again.

So just change this line:

public class EbuyFragmentPagerAdapter extends FragmentPagerAdapter 

to

public class EbuyFragmentPagerAdapter extends FragmentStatePagerAdapter 

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Memory keeps growing

분류에서Dev

Windows Task Manager shows process memory keeps growing

분류에서Dev

GridView how to show Footer when gridview is empty

분류에서Dev

Listview inside gridview android

분류에서Dev

android fixed layout size when listview visibility is gone

분류에서Dev

Framgment / PageAdapter의 ListView null

분류에서Dev

Nexus 7 Keeps Crashing When Sliding Edges?

분류에서Dev

android PageAdapter 및 ViewPager는 setCurrentItem (int index)을 사용할 때 indexoutofbounds 예외를 확인합니까?

분류에서Dev

Android - Dialog Fragment width keeps on matching parent

분류에서Dev

Android + Phonegap + Parse Push keeps crashing

분류에서Dev

Android Application MainActivity keeps Resetting Information

분류에서Dev

Android background animation ListView/GridView

분류에서Dev

Android GridView item touched listener

분류에서Dev

Implementing Multi Select in GridView Android

분류에서Dev

PDF file getting corrupted and size increases when uploading it to FTP server | Android Studio | java

분류에서Dev

Trying to write a shell script that keeps testing a server remotely, but it keeps falling in else statement when I logout

분류에서Dev

Android layouts size and resize

분류에서Dev

Prevent GridView from resizing when clicking on The LinkButton

분류에서Dev

Making GridView sortable when manually asigning DataSource

분류에서Dev

GridView Android 내부의 ListView

분류에서Dev

How make android Gridview items clickable

분류에서Dev

Android: How to remove black line below the GridView

분류에서Dev

InputMethod Android 항목의 GridView

분류에서Dev

Android-GridView와 LinearLayout 결합

분류에서Dev

Android Listview keeps old items after adapter changes

분류에서Dev

Mapbox keeps GPS Lock after app is the background on Android

분류에서Dev

Android Studio keeps backing up changed classes (Git)

분류에서Dev

Page size changing when printing

분류에서Dev

LongListSelector ItemTemplate not growing

Related 관련 기사

  1. 1

    Memory keeps growing

  2. 2

    Windows Task Manager shows process memory keeps growing

  3. 3

    GridView how to show Footer when gridview is empty

  4. 4

    Listview inside gridview android

  5. 5

    android fixed layout size when listview visibility is gone

  6. 6

    Framgment / PageAdapter의 ListView null

  7. 7

    Nexus 7 Keeps Crashing When Sliding Edges?

  8. 8

    android PageAdapter 및 ViewPager는 setCurrentItem (int index)을 사용할 때 indexoutofbounds 예외를 확인합니까?

  9. 9

    Android - Dialog Fragment width keeps on matching parent

  10. 10

    Android + Phonegap + Parse Push keeps crashing

  11. 11

    Android Application MainActivity keeps Resetting Information

  12. 12

    Android background animation ListView/GridView

  13. 13

    Android GridView item touched listener

  14. 14

    Implementing Multi Select in GridView Android

  15. 15

    PDF file getting corrupted and size increases when uploading it to FTP server | Android Studio | java

  16. 16

    Trying to write a shell script that keeps testing a server remotely, but it keeps falling in else statement when I logout

  17. 17

    Android layouts size and resize

  18. 18

    Prevent GridView from resizing when clicking on The LinkButton

  19. 19

    Making GridView sortable when manually asigning DataSource

  20. 20

    GridView Android 내부의 ListView

  21. 21

    How make android Gridview items clickable

  22. 22

    Android: How to remove black line below the GridView

  23. 23

    InputMethod Android 항목의 GridView

  24. 24

    Android-GridView와 LinearLayout 결합

  25. 25

    Android Listview keeps old items after adapter changes

  26. 26

    Mapbox keeps GPS Lock after app is the background on Android

  27. 27

    Android Studio keeps backing up changed classes (Git)

  28. 28

    Page size changing when printing

  29. 29

    LongListSelector ItemTemplate not growing

뜨겁다태그

보관