Ad Code

Expendable Gridview android studio java | gridview not expended in the scrollview

Hello developers, in this blog I will solve the problem of gridview, where if we put gridview in the scrollview then it is not expending when the data is increased. So, read this Aricale carefully for solving the problem easily without getting any error. Don't forget to bookmark our site because in this site we are uploading multiple types o0f content like Java coding, Englsih grammar, Group benifits and others etc. If you are facing any problem in Java coding then please contect us and inform about the error you are facing. We will try to solve the problem and publish articles based on the error as soon as possible.

Here I have given Expendable GridView Class. Just add this class into your project and replace gridview to ExpandableGridView as the class name. Your problem will solve easily.

ExpandableGridView
package com.myproject.application;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;

public class ExpandableGridView extends GridView {

    boolean expanded = false;

    public ExpandableGridView(Context context)
    {
        super(context);
    }

    public ExpandableGridView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableGridView(Context context, AttributeSet attrs,
                                    int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {

        if (isExpanded())
        {
             int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    } }

Here, I have have given an example of this ExpandableGridView uses. So If you still cant use then you can see this example and Implement it setp by step.

activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">


  
    <ScrollView
        android:layout_width="match_parent"
        android:scrollbars="none"
        android:scrollbarSize="0dp"
        android:layout_height="match_parent">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            
            <com.myproject.application.ExpandableGridView
                android:id="@+id/gridview1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:scrollbars="none"
                android:gravity="center"
                android:layout_marginRight="35dp"
                android:layout_marginLeft="35dp"
                android:verticalSpacing="8dp"
                android:layout_marginTop="10dp"
                android:padding="4dp"/>
            
            
            
        </LinearLayout>
        
        
        
        
        
    </ScrollView>
    
    
    
</LinearLayout>
MainActivity.java
ExpandableGridView expandableGridView;
        ArrayList<HashMap<String, Object>> listMap = new ArrayList<>();
        
        expandableGridView = findViewById(R.id.expandableGridView);
        expandableGridView.setAdapter(new ExpandableGridViewAdapter(listMap));

Here I have given an Adapter class you can use your old GridView Adapter for better understanding.

ExpandableGridViewAdapter class
public class  ExpandableGridViewAdapter extends BaseAdapter {
        ArrayList<HashMap<String, Object>> _data;
        public ExpandableGridViewAdapter(ArrayList<HashMap<String, Object>> _arr) {
            _data = _arr;
        }

        @Override
        public int getCount() {
            return _data.size();

        }

        @Override
        public HashMap<String, Object> getItem(int _index) {
            return _data.get(_index);
        }

        @Override
        public long getItemId(int _index) {
            return _index;
        }
        @Override
        public View getView(final int _position, View _v, ViewGroup _container) {
            LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View _view = _v;
            if (_view == null) {
                _view = _inflater.inflate(R.layout.itemview, null);
                
                //itemview refers to the custom layout
                //make a simple layout with the name itemview.xml in the layout resources
            }
            
            

            return _view;
        }
    }

What is expadable gridview ?

Expandable gridview is a advanced gridview class which is mostly used in the scrollview. We all know that if we put a gridview in the Scrollview then it is not expanding is gridview data is increasing. So for solving this problem we have published an ExpandableGridView class which is very easy to use and solve your problem without any error. We have also made Expandable listview and publish it as soon as possible. In many time we have to put our Gridview and listview inside VerticleScrollview so there it will much helpful for you. Hope you will understand about ExpandableGridview and its uses.

Thank you for visiting our blog. And Hope you got some help from this Article. You can also downwnload our Android App and also Subscribe our youtube chennal.

Post a Comment

0 Comments

Ad Code