Nested GridView in ScrollView. The solution for displaying only one row in ListView is scrollviewgridview.

Source: Internet
Author: User

Nested GridView in ScrollView. The solution for displaying only one row in ListView is scrollviewgridview.

Reprinted: http://blog.csdn.net/luohai859/article/details/39347583

My personal understanding is: if you use the GridView and ListView separately, the content is fixed, and the underlying system can easily calculate the width and height occupied by the control, after a layer of ScrollView is nested on the outside, you can slide up or down or left or right, but you do not know how many systems can slide. In this case, you need to calculate a sliding area for ScrollView. The value is the sum of the total width and height of the content in the GridView and ListView. For more precise understanding, please kindly advise. There are two solutions:

Method 1: Calculate the height and display of the Child column in ListView or GridView:
public void setListViewHeightBasedOnChildren(ListView listView) {        ListAdapter listAdapter = listView.getAdapter();         if (listAdapter == null) {            return;        }          int totalHeight = 0;        for (int i = 0; i < listAdapter.getCount(); i++) {            View listItem = listAdapter.getView(i, null, listView);            listItem.measure(0, 0);            totalHeight += listItem.getMeasuredHeight();        }          ViewGroup.LayoutParams params = listView.getLayoutParams();        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));        ((MarginLayoutParams)params).setMargins(15, 15, 15, 15);      listView.setLayoutParams(params);    }

Method 2: override the onMeasure method of the GridView and ListView, and directly give it a high enough height:

Override ListView:

public class MyListView extends ListView {        public MyListView(Context context) {          // TODO Auto-generated method stub          super(context);      }        public MyListView(Context context, AttributeSet attrs) {          // TODO Auto-generated method stub          super(context, attrs);      }        public MyListView(Context context, AttributeSet attrs, int defStyle) {          // TODO Auto-generated method stub          super(context, attrs, defStyle);      }        @Override      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {          // TODO Auto-generated method stub          int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                  MeasureSpec.AT_MOST);          super.onMeasure(widthMeasureSpec, expandSpec);      }  }

Rewrite the GridView:

public class MyGridView extends GridView{        public MyGridView(Context context, AttributeSet attrs) {               super(context, attrs);           }                  public MyGridView(Context context) {               super(context);           }                  public MyGridView(Context context, AttributeSet attrs, int defStyle) {               super(context, attrs, defStyle);           }                  @Override           public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {                      int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                       MeasureSpec.AT_MOST);               super.onMeasure(widthMeasureSpec, expandSpec);           }   }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.