Android GridView and ScrollView nesting

Source: Internet
Author: User

From answering the channel answer, so the content is based on the original layout of the answer channel

1. layout file 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" > <!--Top Blue toolbar--<relativelayout android:id= "@+id/fujin_top_lla        Yout "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= "30d P "android:layout_alignparentleft=" true "android:layout_marginleft=" 10dip "Android:paddin             gleft= "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/fujin_top_llayout_lessthan " android:paddingleft= "10DP" android:paddingtop= "3DP" android:text= "222" Androi D:textcolor= "#fff" android:textsize= "16sp"/> <textview android:id= "@+id/fujin_top_llay Out_dingwei "android:layout_width=" 70DP "android:layout_height=" 30DP "Android:layout_tole ftof= "@+id/fujin_top_llayout_search" android:text= "33333"/> <textview android:id= "@+id            /fujin_top_llayout_search "android:layout_width=" wrap_content "android:layout_height=" 30DP " Android:layout_alignparentright= "true" android:paddingright= "12DP" android:text= "44444"/> & lt;/relativelayout> <!--top Blue toolbar ends--<scrollview android:layout_width= "Match_parent" and roid: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 and Roid:id= "@+id/fenlei_title_rl" android:layout_width= "Match_parent" android:layout_height= "28 DP "android:layout_marginleft=" 15DP "android:layout_marginright=" 15DP "Androi                    d:layout_margintop= "20DP" > <textview android:id= "@+id/fenlei_title_tv" Android:layout_width= "Match_parent" android:layout_height= "Match_parent" an droid:gravity= "center" android:text= "title" android:textsize= "15sp"/> &L   T;/relativelayout>         <!--Title_over-<!--btnlist-<linearlayout Android:id = "@+id/fujin_btnlist_tl" android:layout_width= "match_parent" android:layout_height= "Wrap_con Tent "android:layout_margintop=" 15DP "android:orientation=" Vertical "Android:        Scrollbaralwaysdrawverticaltrack= "true" > </LinearLayout> <!--btnlist_over-- </LinearLayout> </ScrollView></LinearLayout>

2, the GridView item layout file

<?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 Porterduffactivity 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
4. MyGridView Code

public class MyGridView extends gridview{private Boolean needscrollbar = false;   Sets whether there is a scrollbar, which should be set to false when it is to be displayed in Scollview. 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);}}}   


Summarize:

Because the ScrollView and GridView have layout conflicts, if the outer layer is nested with ScrollView, the GridView cannot display full if it does not specify a fixed height. The ListView also has this problem.

But in fact, for this to the problem, I think the use of LinearLayout equal layout is also possible, vertical layout can be added down, so as to avoid the conflict with the ScrollView. But primarily because the GridView is a packaged component, using the GridView can simplify some of the work, but it also increases the burden of layout refreshes (although it is not generally possible to consider)

Android GridView and ScrollView nesting

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.