The new Widgetrecyclerview
The Recyclerview is an upgraded version of the ListView that provides better performance and is easier to use. Like a ListView, Recyclerview is a container for displaying large amounts of data and improves performance when scrolling by reusing a limited number of view. You can use the Recyclerview control when the elements on your view are constantly changing dynamically and regularly.
Unlike the ListView, Recyclerview is now no longer responsible for the layout, focusing only on the reuse mechanism, which is managed by LayoutManager. Recyclerview still gets the object that needs to be displayed through adapter.
To use the Recyclerview component, create adapter no longer inherits from Baseadapter, Instead of inheriting from the Recyclerview.adapter class, and preferably specifying a paradigm that inherits from Recyclerview.viewholder, adapter no longer asks you to return a view, but a viewholder.
After inheriting from adapter, you need to implement 3 abstract methods:
// 当RecyclerView需要一个ViewHolder时会回调该方法,如果有可复用的View则该方法不会得倒回调public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i);// 当一个View需要出现在屏幕上时,该方法会被回调,你需要在该方法中根据数据来更改视图public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i);// 用于告诉RecyclerView有多个视图需要显示public int getItemCount();
The new adapter and the original adapter do not have much difference, just no longer need us to write the logic of re-use judgment, because the re-use logic is actually similar, it has its own implementation. As with the original adapter, you can still notifyDataSetChanged
refresh the UI by getItemViewType
getting the type of the corresponding location, but it no longer requires you to specify how many types, because the method has been able to determine how many types. The new method allows the user to listen to the onViewRecycled
time the view is removed from the screen, and also provides an AdapterDataObserver
observer to provide a callback when the data is changed externally.
Viewholder is the encapsulation of all individual item that contains not only the view that the item needs to display, but also other data related to the item, such as: current position, previous position, position to be displayed, number of times to be recycled, The type of view, whether the medium information is displayed. Creating a Viewholder needs to pass a View object, which is the view of the holder, which usually contains sub-views, which we'd like to record in holder to make it easier to set up different data when it's time to reuse.
Recyclerview no longer manages layouts, but through LayoutManager management layouts, we can implement special layouts by inheriting from LayoutManager, and the system provides three common layout managers:
- Linearlayoutmanager Linear layout
- Gridlayoutmanager Nine Gongge layout
- Staggeredgridlayoutmanager Waterfall Flow Layout
And each can set the horizontal and vertical layout, unfortunately can not add header, if you want to add a header, we can use different types in adapter to achieve this effect.
Recyclerview Default provides an animated effect of adding and deleting item, if we use custom animations, we need to inherit the inheriting RecyclerView.ItemAnimator
class, when RecyclerView.setItemAnimator()
we set our custom animations by means of the method.
CardView
In the implementation of flat UI processing, usually without the shadow and fillet, we usually let the artist provide a shadow and rounded effect of the background picture, now we have a better way to implement, that is CardView.
CardView is actually a subclass of the Framelayout class that provides a card style for the view and maintains a uniform style on different platforms. The CardView component can set the shadow and rounded corners.
We can use cardElevation
attributes to set the shadow effect in the XML layout, which can be setCardElevation
achieved by the same effect in the code. The settings for the shadow are similar to the Z property in Android L.
It is also fairly easy to set the fillet, which is set in the XML, used in the cardCornerRadius
code setRadius
, and the rounded corners are similar to the tailoring in Android L.
If we want to set the background of the CardView, please note that the use carBackgroundColor
method setBackgroundColor
may affect our fillet effect.
ToolBar
Toolbar is a new control introduced by Android L to replace Actionbar, which provides actionbar similar functionality, but is more flexible. Unlike Actionbar, toolbar is more like a general view element that can be placed anywhere in the view tree system, can be animated, and can be scrolled along with ScrollView, and can interact with other view in the layout. Of course, you can also replace Actionbar with toolbar, just call Activity.setactionbar ().
In order to be compatible with more devices generally we use the toolbar in the AppCompat Android.support.v7.widget.Toolbar.
There are two ways to use toolbar:
- Use toolbar as a actionbar. This typically happens when you want to take advantage of some of the existing features of Actionbar (such as being able to display action items in menus, responding to menu clicks, using Actionbardrawertoggle, etc.), but want to gain more control than Actionbar.
- Use toolbar as a standalone control, which is also known as standalone.
If you want to use toolbar as a actionbar, you first need to remove the actionbar, the simplest way is to use the Theme.AppCompat.NoActionBar
theme. Or, set the properties of the theme android:windowNoTitle
to True. Then it is called in the activity's OnCreate setSupportActionBar(toolbar)
, and the menu that should appear on the Actionbar automatically appears on the Actionbar.
Toolbar height, width, background color, and so on all view properties are entirely up to you, because toolbar is essentially a viewgroup. Using toolbar as a standalone control is not necessary to remove actionbar (both can coexist) and can use any topic. However, in this case, the menu is not automatically displayed on toolbar, toolbar will not respond to the menu's callback function, and if you want the menu item to appear on toolbar, you must manually inflate menu.
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { // 处理menu事件 return true; }});// 创建一个menu添加到toolbar上toolbar.inflateMenu(R.menu.your_toolbar_menu);
Android5.0 new Features--new widgets (widgets)