Nesting of the GridView and ScrollView of Android, gridviewscrollview

Source: Internet
Author: User

Nesting of the GridView and ScrollView of Android, gridviewscrollview

The content is based on the original question layout of the answer channel.

1. The layout file is as follows:

File Name: testxm. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "# f1f1f1" android: orientation = "vertical"> <! -- Blue toolbar on the top --> <RelativeLayout android: id = "@ + id/fujin_top_llayout" android: layout_width = "fill_parent" android: layout_height = "36dp" android: background = "#47a9d0" android: gravity = "center_vertical" android: orientation = "horizontal"> <TextView android: id = "@ + id/fujin_top_llayout_lessthan" android: layout_width = "wrap_content" android: layout_height = "30dp" android: layout_alignParentLeft = "true" android: layout _ MarginLeft = "10dip" android: paddingLeft = "10dp" android: text = "1111"/> <TextView android: id = "@ + id/fujin_top_llayout_fujin" android: layout_width = "wrap_content" android: layout_height = "30dp" android: layout_toRightOf = "@ + id/fill" android: paddingLeft = "10dp" android: paddingTop = "3dp" android: text = "222" android: textColor = "# fff" android: textSize = "16sp"/> <TextView android: id = "@ + id/fuji N_top_llayout_dingwei "android: layout_width =" 70dp "android: layout_height =" 30dp "android: layout_toLeftOf =" @ + id/blank "android: text =" 33333 "/> <TextView android: id = "@ + id/Fujitsu _top_llayout_search" android: layout_width = "wrap_content" android: layout_height = "30dp" android: layout_alignParentRight = "true" android: paddingRight = "12dp" android: text = "44444"/> </RelativeLayout> <! -- End of the blue toolbar on the top --> <ScrollView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: fillViewport = "true" android: layout_weight = "1" android: scrollbars = "vertical"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> <! -- Title --> <RelativeLayout android: id = "@ + id/fenlei_title_rl" android: layout_width = "match_parent" android: layout_height = "28dp" android: layout_marginLeft = "15dp" android: layout_marginRight = "15dp" android: layout_marginTop = "20dp"> <TextView android: id = "@ + id/fenlei_title_ TV" android: layout_width = "match_parent" android: layout_height = "match_parent" android: gravity = "center" android: text = "title" android: text Size = "15sp"/> </RelativeLayout> <! -- Title_over --> <! -- Btnlist --> <LinearLayout android: id = "@ + id/tags" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginTop = "15dp" android: orientation = "vertical" android: scrollbarAlwaysDrawVerticalTrack = "true"> </LinearLayout> <! -- Btnlist_over --> </LinearLayout> </ScrollView> </LinearLayout>

2. The item layout file of the gridview

<?xml version="1.0" encoding="utf-8"?><RelativeLayout          xmlns:android="http://schemas.android.com/apk/res/android"          android:layout_height="wrap_content"          android:paddingBottom="4dip" android:layout_width="fill_parent">         <TextView                android:layout_width="wrap_content"                android:layout_below="@+id/ItemImage"                android:layout_height="wrap_content"                android:text="TextView01"                android:layout_centerHorizontal="true"                android:id="@+id/ItemText">         </TextView></RelativeLayout>

3. activity Code

Public class porterduactivity extends Activity {private PorterCanvas mPorterCanvas = null; private GridView gridView; private LinearLayout ll; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. testxm); this. addview ();} private void addview () {for (int I = 1; I <= 35; I ++) {ll = (LinearLayout) findViewById (R. id. fujin_btnlist_tl); this. addTwoClassify (I );} Private void makeGridView (int num) {ArrayList <HashMap <String, Object> lstImageItem = new ArrayList <HashMap <String, Object> (); for (int I = 0; I <10; I ++) {HashMap <String, Object> map = new HashMap <String, Object> (); map. put ("ItemText", num + "NO. "+ String. valueOf (I); // do ItemText lstImageItem by serial number. add (map) ;}// generate the ImageItem of the adapter <===> dynamic array elements. The two correspond to SimpleAdapter saImageItems = new SimpleAdapter (this, // nothing LstImageItem, // data source R. layout. gridview_item, // XML Implementation of night_item // The Child item of the dynamic array and ImageItem new String [] {"ItemText"}, // an ImageView in the XML file of ImageItem, two TextView IDs new int [] {R. id. itemText}); // Add and display the gridView. setAdapter (saImageItems);} private void addTwoClassify (int I) {gridView = new MyGridView (porterduactivity. this); // note that MyGridView is used here. If GridView is used, only one row is displayed, and the second row is not completely displayed. If MyGridView is used, it can be displayed completely. Commend by danielinbiti gridView. setVerticalSpacing (10); gridView. setHorizontalSpacing (15); gridView. setNumColumns (3); this. makeGridView (I); LinearLayout ll_two = new LinearLayout (porterduactivity. this); ll_two.setPadding (10, 0, 0, 0); ll_two.addView (gridView, new LinearLayout. layoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); ll. addView (ll_two, new LinearLayout. layoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));}}

4. MyGridView code

Public class MyGridView extends GridView {private boolean needScrollBar = false; // you can specify whether a ScrollBar exists. to display it in ScollView, set it to false. Otherwise, true public MyGridView (Context context) {super (context);} public MyGridView (Context context, AttributeSet attrs) {super (context, attrs);} public MyGridView (Context context, attributeSet attrs, int defStyle) {super (context, attrs, defStyle);} @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {if (! NeedScrollBar) {int expandSpec = MeasureSpec. makeMeasureSpec (Integer. MAX_VALUE> 2, MeasureSpec. AT_MOST); super. onMeasure (widthMeasureSpec, expandSpec);} else {super. onMeasure (widthMeasureSpec, heightMeasureSpec );}}}


Summary:

Because ScrollView conflicts with the GridView layout, if the outer layer is nested with ScrollView, The GridView cannot be completely displayed if it is not specified with a fixed height. This problem also exists in ListView.

But in fact, for this question, I think it is also possible to use the LinearLayout equal-score layout. You can add vertical layout down to avoid conflicts with ScrollView. However, because the GridView is a encapsulated component, the use of the GridView can simplify some of the work, but it also increases the burden of layout refresh (although generally do not consider)

Related Article

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.