📜 ⬆️ ⬇️

RecyclerView and CardView. New Widgets in Android L

As you know, in June, the next Google I / O conference was held, during which it was briefly told about two new widgets. And since nothing has been written on these widgets yet, I decided to write this article with a simple demonstration.

The demo application is available on the githab by reference .

Actually, under the cut, you can read a few words about RecyclerView, CardView and the connection of the corresponding libraries in Gradle. Also, you can watch a short video demonstrating the standard animation of list operations in RecyclerView.

RecyclerView


RecyclerView, in fact, is an evolution of one of the most necessary in Android-development widgets - ListView. Actually, its purpose is exactly the same - to display a list of elements, but there are nuances:
')
1. Mandatory use of the ViewHolder pattern. If using ListView, because of lack of experience, it was possible to use an adapter that creates a separate view for each list item from scratch, which, with a large list size, could result in less responsiveness of the UI and the use of extra memory, then when working with the developer’s RecyclerView, pattern. Let's see how it looks in the adapter code for RecyclerView:

/** *     RecyclerView.Adapter   ,        , .. ,  ViewHolder.        . */ public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { private List<Record> records; public RecyclerViewAdapter(List<Record> records) { this.records = records; } /** *   View  ViewHolder  ,    . */ @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false); return new ViewHolder(v); } /** *   View       i */ @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { Record record = records.get(i); viewHolder.name.setText(record.getName()); } @Override public int getItemCount() { return records.size(); } /** *   ViewHolder,    . */ class ViewHolder extends RecyclerView.ViewHolder { private TextView name; private ImageView icon; public ViewHolder(View itemView) { super(itemView); name = (TextView) itemView.findViewById(R.id.recyclerViewItemName); icon = (ImageView) itemView.findViewById(R.id.recyclerViewItemIcon); } } } 


2. LayoutManager. To use RecyclerView besides the adapter, you need to pass to it using the setLayoutManager () method an object of the class that implements the LayoutManager. This class is responsible for working with the adapter, it is he who decides whether to reuse the View or create a new one, and accordingly, he is the one who pulls the onCreateViewHolder (), onBindViewHolder () and getItemCount () methods of the adapter. While only one implementation of this class is available - LinearLayoutManager, to create a custom LayoutManager, it is necessary to inherit from RecyclerView.LayoutManager.

3. Animate list operations. If you watched the presentation of the Material design, you might have noticed that one of its main features is the smoothness of the UI, which is achieved through the widespread use of animation. Surely, with a special desire, you can add animation to the ListView, I have not had to do this yet, but in RecyclerView, this is done with a couple of lines of code:
- for the RecyclerView object, the class that implements the animation is specified:

  RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator(); recyclerView.setItemAnimator(itemAnimator); 

- when adding or removing a list item, the adapter method is called notifyItemInserted (int position) or notifyItemRemoved (int position), respectively.
If you wish, you can write your own implementation of animation, inheriting from RecyclerView.ItemAnimator.

4. Dampness . It must be remembered that the widget is still damp, and perhaps significant changes are waiting for it. For example, now in RecyclerView there is no possibility to set the header and footer of the list, and generally there is quite a bit of information about it on the Internet.

Actually, everything about RecyclerView, on the githaba, I posted a simple application that demonstrates how to use this widget. Running it, you can admire the animation of adding / deleting an element (or you can watch the video).





CardView


CardView is a widget that implements such a Material design element as a card. In essence, this is a container where you can set the radius of the roundness of the corners, the color of the card and the height along the z axis.
For example, the following layout code:
  <android.support.v7.widget.CardView android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/cardView" card_view:cardCornerRadius="6dp" card_view:cardBackgroundColor="#84ffff"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/info_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Test CardView" android:layout_centerInParent="true" android:textSize="18sp" android:textColor="#fff"/> </RelativeLayout> </android.support.v7.widget.CardView> 

will give us the following result:



Build a project using new widgets

To build with Gradle, add dependencies to dependencies:
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:recyclerview-v7:+'

But these libraries automatically change our build's minSdk to version L, so for normal operation in AndroidManifest, you should set the parameter:

 <uses-sdk tools:node="replace" /> 

Source: https://habr.com/ru/post/237101/


All Articles