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
- Staggered-Position web format layout Manager
- CardView's API description
Android Basics (11)