Android Basics (11)

Source: Internet
Author: User

List-View

    • List-View
      • Brief introduction
      • Body
      • Extended Reading

Target group: Android Beginner without foundation
Knowledge Points: The use of Recyclerview
Goal: Use Recyclerview to display list data in a page

Brief introduction
    • Recyclerview Layout-related instructions
Body

1. Open the Activity_main.xml file and modify its internal code to resemble the following

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.v7.widget.RecyclerView        android:id="@+id/recyclerView"        android:layout_width="fill_parent"        android:layout_height="fill_parent" /></RelativeLayout>
    • There is a Recyclerview control inside the page, and the next code expands around it

2. Create a new layout file named Item_card.xml, and the internal code is as follows

<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Xmlns:card_view="Http://schemas.android.com/apk/res-auto"android:layout_width="Fill _parent "android:layout_height=" Fill_parent ">                <!--a CardView control with TextView--    <android.support.v7.widget.cardview  android:id  =" @+id/card_view " android:layout_gravity  = "center"  android:layout_width  = "200DP"  android:layout_height  = "200DP"  card_view:cardcornerradius  =" 4DP ";         <TextViewandroid:id="@+id/info_text"android:layout_width="Match _parent "android:layout_height=" match_parent " />                                        </android.support.v7.widget.CardView></linearlayout>
    • The layout style that corresponds to each child item

    • A new control CardView is used here, go to Build.gradle to add a new code reference, as shown below

dependencies {    compile fileTree(dir‘libs‘, include: [‘*.jar‘])    ‘com.android.support:appcompat-v7:21.0.3‘    ‘com.android.support:cardview-v7:21.0.+‘}

3.Recyclerview is only responsible for UI display and data set processing , in order to specify the specific display of each unit view, we need to specify a layout manager for Recyclerview, the code is as follows

  @Override  protected  void  oncreate  (        Bundle savedinstancestate) {super . OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Recyclerview Mrecyclerview = (recyclerview) Findviewbyid (R.id.recyclerview); //when the size of each child view does not change in the layout, this sentence can be called to improve performance  Mrecyclerview.sethasfixedsize (true );        //specifies a layout manager  of type Recyclerview for Linearlayoutmanager Linearlayoutmanager Mlayoutmanager = new  linearlayoutmanager (this );     Mrecyclerview.setlayoutmanager (Mlayoutmanager); }
    • Linearlayoutmanager is the simplest layout manager, which is the management of linear layouts

    • Linearlayoutmanager have both horizontal and vertical orientations

Extended Reading
    1. Staggered-Position web format layout Manager
    2. CardView's API description

Android Basics (11)

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.