quoted

RecyclerView is a more powerful Android control, which can not only achieve the same effect as ListView, but also optimize the various deficiencies of ListView. It can scroll vertically or horizontally (the ListView does not scroll horizontally). Then explain the usage of RecyclerView.

In the first experiment, a simple wechat interface was realized through the fragment developed by Android. This time, RecyclerView layout was carried out in the fragment based on it, and RecyclerView was set as longitudinal layout and waterfall flow to display different page layouts.

RecyclerView usage

Because RecyclerView is a new control,Android RecyclerView is defined in the support library. To use RecyclerView, the first step is to add the corresponding dependent libraries to build. Gradle. The dependency libraries used here are:

Implementation 'androidx. Recyclerview: recyclerview: 1.1.0'Copy the code

The code for invoking RecyclerView layout in the layout file is:

 <androidx.recyclerview.widget.RecyclerView      />
Copy the code

Vertical layout

Functions:

  • Drag the up and down
  • Click on the color
  • Click the corresponding menu bar to display a View prompt message

Project code

This experiment has built a general framework in the last experiment, in which the Fragment control can be equivalent to a small activity to write, so the experiment code of this time is to modify the Java class of the Fragment in the last experiment.

Insert data for the vertical layout bar on the Fragment page:

Create a DragAdapter inherited from RecyclerView.Adapter

Transfer value to recyclerView first; Then override the View’s onCreateViewHolder method to call in the drag_item.xml layout file

Then to drag and drop the page, you need to pass in the location information of the menu bar. I just overwrite onBindViewHolder to get the menu bar information. Among them, the last three lines of code separated by carriage return are the codes that receive the contents of the menu bar first and output the contents of View prompt information. I use toast method to output the contents.This line of code is the drag-and-drop code. After the menu bar is moved, the position of the following menu bar is changed, so the position of the next menu bar is changed, and the changed menu bar is inserted into a changed positionFinally, fill the list code into the layout file’s menu bar. The code is:The ItemTouchHelperListener Java function listens for the position of the layout bar clicked and returns its starting position. This is its Adapter code. But to do that, you need to create a New DragItemTouchHelper that defines the specific drag operation. For the specific code, I mainly refer to other people’s code, because it is a fixed template that can be used directly.

Reference template: RecyclerView use method summary

Modify the fragment layout file. You need to pay attention to several details: The fragment is used in the activity to create a view, to use recyclerView, you need to change the view to recyclerView. Then it is time to set its layout, adapter, and so on. The specific code can be viewed in my code repository.

The waterfall flow

Recyclerview layout file formats are roughly the same, all use recyclerview. The first widget. Recyclerview create a layout, and then write a layout file inside a ImageView is used for the introduction of the pictures. After the above two layout files, the basic RECYclerView XML file has been completed.

Add an adapter to recyclerView, inherit from RecyclerView. adapter, and use viewholder. We’ll override onCreateViewHolder to render the ItemView, create ViewHolde onBindViewHolder to pass in the information about the image, and getItemCount to pass in the size of the image. Myviewholder passes information about the image into the layout file for output. Its main code is the following:For convenience, I put the picture information in a class, convenient function call.

Waterfall flow is mainly used to achieve irregular space arrangement of pictures of different sizes. We can write configuration code directly to fragments, which I use the StaggeredGridLayoutManager waterfalls flow form layout. The StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager (2, StaggeredGridLayoutManager. VERTICAL); “2” refers to the number of items displayed in each line. If the picture is too large, the layout will automatically divide the picture into multiple pictures for layout.

For aesthetics, you can also use CardView control 5.0 to achieve rounded corners, eliminating the need to write your own Drawble, which is optional. (This experiment was not written for convenience)

See my private warehouse for the code. Wechat interface contactFragment is the fragment layout code of waterfall stream, Adapter is the waterfall stream Adapter code. FrdFragment is the fragment layout code for vertical layout, and DragAdapter is the adapter code for vertical layout.