Android custom gridview displays a row and can slide left and right

Source: Internet
Author: User

Recently, I made a title similar to the sliding menu bar. The grildview is used to bind the data source. To achieve horizontal sliding, The grildview displays only one row. Finally, the layout is dynamically added to the activity in the form of code.

Viewgroup. layoutparams Params = dishtype. getlayoutparams (); // dishtype, welist is the arraylist
Int dishtypes = welist. size (); Params. width = 115 * dishtypes; log. D ("Look at this width", Params. width + "" + welist. size (); dishtype. setlayoutparams (Params); // set the number of columns to the expected list length.
Dishtype. setnumcolumns (welist. Size ());

The layout file uses the horizontalscrollview control to control the left and right sliding.

<HorizontalScrollView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:scrollbars="none" >        <FrameLayout            android:layout_width="match_parent"            android:layout_height="80dp"                   >            <LinearLayout                android:id="@+id/liear"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="horizontal"                  >                <GridView                    android:id="@+id/dishtype"                    android:layout_width="match_parent"                    android:layout_height="80dip"                    android:cacheColorHint="#00000000"                    android:columnWidth="108dip"                    android:gravity="center"                    android:horizontalSpacing="1.0dip"                    android:listSelector="#00000000"                    android:numColumns="auto_fit"                    android:stretchMode="spacingWidthUniform"                    android:verticalSpacing="1.0dip" >                </GridView>            </LinearLayout>        </FrameLayout>    </HorizontalScrollView>

 

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.