Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Gridview Inside the GridView as its item

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 633
    Comment on it

    There are certain conditions when you need to use nested gridview or gridview as an item of parent gridview.

     

    Step 1:- Create an xml file.

    A) xml that contain the parent_gridview_item.xml

    <GridView
        android:id="@+id/parent_gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="5dp"
        android:listSelector="@android:color/transparent"
        android:numColumns="3"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:verticalSpacing="4dp">
    </GridView>
    
    

    B) xml that contain the child_gridview_item.xml for parent gridview.

    <GridView
        android:id="@+id/child_gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:horizontalSpacing="5dp"
        android:listSelector="@android:color/transparent"
        android:numColumns="3"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:verticalSpacing="4dp">
    </GridView>

     

    C) xml that contain the item for item.xml

    <TextView
        android:id="@+id/tv_date"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="@dimen/dp_3"/>

     

    Step 2: create the adapter to enfill gridview_parent.

    public class ParentGridviewAdapter extends BaseAdapter
     {
        Activity activity;
        ChildGridViewAdapter childGridViewAdapter;
        ArrayList<Integer> dateList;
    
        public ParentGridviewAdapter(Activity activity) 
        {
          for(int i=0;i<9;i++)
            {
              dateList.add(i);
            }
            this.activity = activity;
        }
        @Override
        public int getCount() 
        {
            return dateList.size();
        }
        @Override
        public Object getItem(int i)
        {
            return dateList.get(i);
        }
        @Override
        public long getItemId(int i) 
        {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) 
         {
          final ViewHolder holder;
          LayoutInflater inflater;
         inflater=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           
           if (convertView == null) 
            {
                convertView = inflater.inflate(R.layout.child_gridview_item, null);
            }
            holder = new ParentGridviewAdapter.ViewHolder(convertView);
    
            childGridViewAdapter = new ChildGridViewAdapter(activity);
            holder.gridView.setAdapter(childGridViewAdapter);
            return convertView;
        }
        private class ViewHolder 
         {
            private InnerGridView gridView;
            public ViewHolder(View v) {
            gridView = (InnerGridView) v.findViewById(R.id.child_gridview);
         }
       }

     

    Step 3: create the adapter to enfill gridview _child.

     

    public class ChildGridviewAdapter extends BaseAdapter
     {
        Activity activity;
        ArrayList<Integer> countList;
    
        public ChildGridviewAdapter(Activity activity) 
        {
          for(int i=0;i<4;i++)
            {
              countList.add(i);
            }
            this.activity = activity;
        }
        @Override
        public int getCount() 
        {
            return countList.size();
        }
        @Override
        public Object getItem(int i)
        {
            return countList.get(i);
        }
        @Override
        public long getItemId(int i) 
        {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) 
         {
          final ViewHolder holder;
          LayoutInflater inflater;
         inflater=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           
           if (convertView == null) 
            {
                convertView = inflater.inflate(R.layout.item, null);
            }
            holder = new ChildGridviewAdapter.ViewHolder(convertView);
            holder.textv.setText(countList.get(position)+"")
            return convertView;
        }
        private class ViewHolder 
         {
            private TextView textv;
            public ViewHolder(View v) {
            textv = (TextView) v.findViewById(R.id.tv_date);
         }
       }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: