Data item loading method and related device
The method addresses performance degradation in RecyclerView by dynamically loading more data items based on scrolling events, ensuring smooth and efficient data loading within the Android development context.
Patent Information
- Application Number
- CN202510180627.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-19
- Publication Date
- 2025-03-21
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In Android application development, modifying the RecyclerView component to implement additional features often leads to performance degradation, such as smooth scrolling issues and slow data loading, resulting in suboptimal loading effects.
A method that responds to vertical scrolling in RecyclerView by rewriting specific methods (getItemCount, getItemViewType, onCreateViewHolder, and onBindViewHolder) to include loading more views, while using the LayoutManager to determine if the last visible item has been reached, triggering the loading of additional data items and refreshing the view using notifyDataSetChanged.
This approach maintains the performance stability of the RecyclerView component while enabling seamless loading of additional data items, enhancing the loading experience without invasive modifications to the original class.
Smart Images

Figure CN119668725A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a method for loading data items and related devices. Background Art
[0002] In mobile application development, especially on the Android platform, the RecyclerView component is a highly customizable and flexible list control commonly used in development, which is used to display a scrolling list of large data sets, that is, a list of data items. However, as application requirements become increasingly complex, it is often necessary to implement additional functions in the RecyclerView component, such as loading more data items.
[0003] Currently, the original class of the RecyclerView component is usually modified to implement additional functions in the RecyclerView component.
[0004] However, if the original class of the RecyclerView component is not modified properly, the performance of the RecyclerView component may be degraded, such as unsmooth sliding and slow data loading, which may lead to poor loading effects. Summary of the invention
[0005] In view of the above problems, the present application provides a data item loading method and related devices to improve the loading effect. The specific scheme is as follows: The first aspect of the present application provides a method for loading a data item, comprising: In response to a vertical slide of a current data item list of the RecyclerView component, taking the last visible data item in the current data item list as the current data item; Based on the current data item list and the current data item, rewrite the getItemCount method, getItemViewType method, onCreateViewHolder method and onBindViewHolder method of the inherited class to obtain the current view including loading more views, the inherited class inherits from RecyclerView.Adapter; Determine, based on the LayoutManager component of the RecyclerView component, whether the current data item is the last data item in the current data item list; If the current data item is the last data item in the current data item list, the next data item list of the current data item list is loaded, and the notifyDataSetChanged method of the RecyclerView component is called to refresh the current view.
[0006] In a possible implementation, before responding to the vertical sliding of the list of data items of the RecyclerView component, the method further includes: The vertical sliding of the current data item list is detected based on the RecyclerView.OnScrollListener interface of the RecyclerView component, and the RecyclerView.OnScrollListener interface is set based on the onAttachedToRecyclerView method of the inherited class.
[0007] In a possible implementation, based on the current data item list and the current data item, rewriting the getItemCount method, the getItemViewType method, the onCreateViewHolder method, and the onBindViewHolder method of the inherited class to obtain the current view containing more views to be loaded includes: Based on the current data item list, rewrite the getItemCount method of the inherited class to obtain the total number of data items in the current data item list; Based on the position index of the current data item and the total number of data items in the current data item list, rewrite the getItemViewType method of the inherited class to obtain the viewType parameter for loading more views; Based on the viewType parameter of the loading more views, rewrite the onCreatViewHolder method of the inherited class to obtain the ViewHolder instance corresponding to the loading more views; Based on the loading more views and the ViewHolder instance corresponding to the loading more views, the onBindViewHolder method of the inherited class is rewritten to obtain the current view containing the loading more views.
[0008] In a possible implementation, the LayoutManager component based on the RecyclerView component determines whether the current data item is the last data item in the current data item list, including: Rewrite the onScrolled method of the RecyclerView.OnScrollListener interface to obtain the type of the LayoutManager component of the RecyclerView component; If the type of the LayoutManager component is a LinearLayoutManager component or a GridLayoutManager component, calling the findLastVisibleItemPosition method of the LinearLayoutManager component or the GridLayoutManager component to determine whether the current data item is the last data item in the current data item list; If the type of the LayoutManager component is a StaggeredGridLayoutManager component, the findLastCompletelyVisibleItemPositions method of the StaggeredGridLayoutManager component is called to determine whether the current data item is the last data item in the current data item list.
[0009] In a possible implementation, after obtaining the current view including loading more views by rewriting the getItemCount method, the getItemViewType method, the onCreateViewHolder method, and the onBindViewHolder method of the inherited class based on the current data item list and the current data item, the method further includes: Rewrite the onViewAttachedToWindow method of the inherited class to initialize the current view that contains more views to be loaded.
[0010] In a possible implementation, the inherited class holds an instance of the original class of the RecyclerView.Adapter, and the method further includes: Based on the position index of the current data item and the total number of data items in the current data item list, rewrite the getItemViewType method of the inherited class to obtain the viewType parameter of the current data item; Based on the viewType parameter of the current data item, the onCreatViewHolder method and the onBindViewHolder method of the original class are called to obtain the current view containing the current data item; The onViewAttachedToWindow method of the original class is called to initialize the current view containing the current data item.
[0011] A second aspect of the present application provides a data item loading device, comprising: A response unit, used to respond to the vertical sliding of the current data item list of the RecyclerView component and take the last visible data item in the current data item list as the current data item; A rewriting unit, configured to rewrite a getItemCount method, a getItemViewType method, an onCreateViewHolder method, and an onBindViewHolder method of an inherited class based on the current data item list and the current data item, to obtain a current view including loading more views, wherein the inherited class inherits from RecyclerView.Adapter, and the inherited class holds an instance of an original class of the RecyclerView.Adapter; A judging unit, configured to judge whether the current data item is the last data item in the current data item list based on the LayoutManager component of the RecyclerView component; A loading unit is used to load the next data item list of the current data item list if the current data item is the last data item in the current data item list, and call the notifyDataSetChanged method of the RecyclerView component to refresh the current view.
[0012] A third aspect of the present application provides a computer program product, comprising computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements the method for loading data items of the first aspect or any implementation of the first aspect.
[0013] A fourth aspect of the present application provides an electronic device, comprising at least one processor and a memory connected to the processor, wherein: The memory is used to store computer programs; The processor is used to execute the computer program so that the electronic device can implement the method for loading data items of the above-mentioned first aspect or any implementation manner of the first aspect.
[0014] A fifth aspect of the present application provides a computer storage medium, which carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can load the data items of the first aspect or any implementation method of the first aspect.
[0015] By means of the above technical scheme, the present application provides a method for loading data items and related devices, the method comprising: in response to the vertical sliding of the current data item list of the RecyclerView component, taking the last visible data item in the current data item list as the current data item; based on the current data item list and the current data item, rewriting the getItemCount method, getItemViewType method, onCreateViewHolder method and onBindViewHolder method of the inherited class to obtain the current view including loading more views, the inherited class inherits from RecyclerView.Adapter; based on the LayoutManager component of the RecyclerView component, judging whether the current data item is the last data item in the current data item list; if the current data item is the last data item in the current data item list, loading the next data item list of the current data item list, and calling the notifyDataSetChanged method of the RecyclerView component to refresh the current view. The present application wraps the original class through the inherited class, and realizes non-intrusive loading of more functions without destroying the original class, thereby maintaining the performance stability of the RecyclerView component and improving the loading effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.
[0017] Figure 1 A schematic diagram of a flow chart of a method for loading a data item provided in an embodiment of the present application; Figure 2 A flowchart of a method for loading a common view provided in an embodiment of the present application; Figure 3 A schematic diagram of the structure of a data item loading device provided in an embodiment of the present application; Figure 4 A schematic diagram of the hardware structure of a data item loading device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0018] The following describes the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. The terms used in the implementation method section of the present application are only used to explain the specific embodiments of the present application, and are not intended to limit the present application.
[0019] The embodiments of the present application are described below in conjunction with the accompanying drawings. Those skilled in the art will appreciate that, with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0020] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and need not be used to describe a specific order or sequential order. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, which is only to describe the distinction mode adopted by the objects of the same attributes when describing in the embodiments of the present application. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that the process, method, system, product or equipment comprising a series of units need not be limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or equipment.
[0021] In order to improve the accuracy of predicted stress monitoring data, the present application provides a method for loading data items. The method for loading data items provided by the present application is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.
[0022] Please see attached Figure 1 , Figure 1 A flowchart of a method for loading a data item provided in an embodiment of the present application. The method may include the following steps: Step S101: In response to the vertical sliding of the current data item list of the RecyclerView component, the last visible data item in the current data item list is used as the current data item.
[0023] The RecyclerView component is an important view component in Android development, mainly used to display a large amount of data sets in a limited window. Its advantage is that it provides a flexible layout manager (LayoutManager component), which developers can use to present data in different ways, such as list, grid or custom layout, to meet various interface design requirements. For example, displaying a dynamic list of friends in a social application and a list of products in an e-commerce application can all be efficiently implemented with the help of the RecyclerView component.
[0024] In this application, when the user operates the interface and slides up or down (i.e., vertically slides) the current list of data items displayed by the RecyclerView component on the screen, the corresponding response mechanism will be triggered. For example, in a news and information application, if the user wants to view more news content, he will naturally slide the news list on the screen up and down. This sliding action is the vertical sliding of the data item list mentioned here.
[0025] During this vertical sliding process, the entire list of data items will scroll and display accordingly according to the direction and amplitude of the sliding. During this scrolling process, which data items are currently visible on the screen. For example: the screen can display five data items at a time. As the user continues to slide the screen upward, new data items will continue to scroll into the visible range of the screen, and the data items above will gradually scroll out of the screen. At any time, the data item that is at the bottom of the screen and can still be seen is the last visible data item. For example: in a product list, after the user slides the screen, the data item corresponding to the information of the product displayed in the last row on the current screen is the last visible data item.
[0026] Before responding to the vertical sliding of the data item list of the RecyclerView component, the vertical sliding of the current data item list may also be detected based on the RecyclerView.OnScrollListener interface of the RecyclerView component, where the RecyclerView.OnScrollListener interface is set based on the onAttachedToRecyclerView method of the inherited class.
[0027] It should be noted that the RecyclerView component is an important view component used in Android development to display a large number of data sets. It cannot directly know what data to display and how to display it, so it needs to be associated and managed through an adapter. The setAdapter (RecyclerView.Adapter adapter) method is an important way provided by the RecyclerView component to establish this association. Calling this method tells the RecyclerView component to use the passed-in adapter to provide data and define the display method of data items on the interface and other related operations.
[0028] In this application, the LoadMoreAdapter inherited class is a custom adapter that inherits from RecyclerView.Adapter. It will expand on the basis of the basic functions of the original adapter. When the instance of the LoadMoreAdapter adapter is passed into the setAdapter method of the RecyclerView component, the onAttachedToRecyclerView method overridden by the instance of the passed-in LoadMoreAdapter inherited class will be called inside the RecyclerView component. This calling mechanism is a standard design in Android development based on component life cycle and interaction logic. It is like an agreed process. Once the adapter is set to the RecyclerView component, the RecyclerView component will notify the adapter "I am associated with you, you can perform some initialization or additional settings related operations", and the onAttachedToRecyclerView method is where the adapter responds to this notification and performs related operations.
[0029] In the onAttachedToRecyclerView(RecyclerViewrecyclerView) method of the LoadMoreAdapter adapter, the instance object of the RecyclerView component currently associated with it will be received as a parameter. Based on this parameter, the developer can set the RecyclerView.OnScrollListener interface for the RecyclerView component to listen to the scrolling events of the RecyclerView, especially when the user slides the data list vertically, the sliding action can be captured in time. For example, when the user slides the list to the bottom (by judging the scrolling situation), the callback method defined in this interface can trigger the logic of loading more data implemented in the LoadMoreAdapter adapter, and then request new data from the server, so that the user can continue to browse more content smoothly.
[0030] Step S102: Based on the current data item list and the current data item, rewrite the getItemCount method, getItemViewType method, onCreateViewHolder method and onBindViewHolder method of the inherited class to obtain the current view containing more views to be loaded, and the inherited class inherits from RecyclerView.Adapter.
[0031] RecyclerView.Adapter is an abstract class that works closely with the RecyclerView component. When the LoadMoreAdapter inherits RecyclerView.Adapter, the inheriting class can obtain some basic features and method definitions of RecyclerView.Adapter. For example, RecyclerView.Adapter defines important abstract methods such as onCreateViewHolder, onBindViewHolder, and getItemCount. Through inheritance, the LoadMoreAdapter inheriting class implements these abstract methods according to the grammatical rules of Java (or other programming languages used for Android development) and the requirements of object-oriented programming, and then builds data display logic suitable for specific business scenarios.
[0032] In this application, firstly, based on the current data item list, the getItemCount method of the inherited class is rewritten to obtain the total number of data items in the current data item list.
[0033] It should be noted that the main function of the getItemCount method of the LoadMoreAdapter adapter is to notify the RecyclerView component how many data items there are in the current list of data items to be displayed. This is equivalent to providing the RecyclerView component with an "instruction manual" about the total amount of data, so that the RecyclerView component can reasonably perform layout, scrolling, display and other related operations based on this number.
[0034] The reason for overriding the getItemCount method is that this method often only defines a basic framework or a specification, and does not specify the actual number of data items. In different application scenarios, the source of the number of data items is different, such as data read from the local database, data obtained from network requests, or data sets obtained after various business logic processing. Therefore, in order to accurately tell the RecyclerView component the specific total number of data items currently expected to be displayed, you need to rewrite this method in the inherited class according to the actual situation.
[0035] Specifically, make it clear where the current list of data items is obtained from. For example, it may be a list that stores user contact information, which is queried from the local address book database of the mobile phone; it may also be a list of products, which is obtained and parsed from the e-commerce server through a network request. In short, you must clearly know the specific content represented by the current list of data items and how to obtain it. The next step is to get the actual number of data items in this list. In many programming languages, such as Java, Kotlin, etc. (commonly used in Android development), the list of data items is generally a collection type, such as List, ArrayList, etc. (taking Java as an example). These collection types have corresponding methods to obtain the number of elements in them. For the List type, you can directly use the size() method to obtain the number of elements it contains. This number of elements is also the value to be returned in the getItemCount method.
[0036] Then, based on the position index of the current data item and the total number of data items in the current data item list, the getItemViewType method of the inherited class is rewritten to obtain the viewType parameter for loading more views.
[0037] It should be noted that in actual application scenarios, the data lists displayed by RecyclerView may not all be views of the same style. There may be a mixture of views with different layouts and functions. The getItemViewType method of the LoadMoreAdapter adapter assigns a corresponding "type identifier", that is, the viewType parameter, to each data item in the RecyclerView component.
[0038] In this application, the load more view is usually placed at the end of the data list. When the user slides to the bottom of the list, the user is prompted to load more data content. It is different from the ordinary data item view in layout, function, etc., so it needs to be assigned a specific type identifier (that is, the viewType parameter of the load more view) so that the RecyclerView component can recognize it and make corresponding special processing.
[0039] Specifically, clarify the relationship between the position index of the current data item and the judgment of whether to reach the position of loading more views. The total number of data items in the current data item list is n. Since the index usually starts counting from 0, when the position index of the current data item is equal to n-1, it means that the current position corresponds to loading more views. Once it is determined that the position of the current data item is the position to load more views, a special viewType parameter can be assigned to it. This viewType parameter is usually an integer value, and it must be different from the type identifier of other ordinary data item views, so that it can play a distinguishing role. For example, the viewType parameter value of the ordinary data item view can be set in the numerical range of 0 to 99, and a unique value, such as 100, can be set specifically for loading more views. The specific value can be set according to the actual project.
[0040] When the LoadMoreAdapter adapter overrides the getItemViewType method, the getItemViewType method will pass in the position index of the current data item as a parameter (assuming the parameter name is position), and then first obtain the total number of data items in the current data item list that has been determined before in the getItemViewType method. Then, by comparing the position with the value of the total number minus 1, it is determined whether the current position is the position for loading more views. If the position is equal to the total number minus 1, then the specific viewType parameter value set for loading more views (such as 100 in the previous example) is returned.
[0041] Then, based on the viewType parameter for loading more views, override the onCreatViewHolder method of the inherited class to obtain the ViewHolder instance corresponding to loading more views.
[0042] It should be noted that the onCreateViewHolder method of the LoadMoreAdapter adapter is responsible for creating a ViewHolder instance corresponding to each data item in the RecyclerView component. The ViewHolder instance is an optimization mechanism that caches the various subviews in the view (such as text boxes, buttons, image controls, etc.), so that during the scrolling of the RecyclerView component, it avoids repeatedly searching and creating these subviews, thereby improving the performance of the list scrolling and allowing users to experience a smoother experience when browsing the data list.
[0043] In this application, the onCreateViewHolder method itself has two main parameters, one is the parent of the ViewGroup type, which represents the parent container where the data item view in the RecyclerView component is located, and the other is the viewType of the int type. This parameter is used to receive the identification value of the view type mentioned above, that is, the type of ViewHolder instance to be created can be determined based on the value of this viewType. When rewriting the onCreateViewHolder method, you can make a judgment based on the value of the passed in viewType parameter inside the method. When this viewType parameter value is equal to the specific value previously set for loading more views (for example, the viewType value set for loading more views is 100, here it is determined whether the passed in viewType is equal to 100), it means that the ViewHolder instance corresponding to loading more views needs to be created. Next, the corresponding layout file for loading more views is loaded through the LayoutInflater class (this layout file is designed in advance by the developer, and contains the specific style of loading more views, such as progress bars, text prompts and other elements).
[0044] Finally, based on loading more views and the ViewHolder instance corresponding to loading more views, override the onBindViewHolder method of the inherited class to get the current view that contains loading more views.
[0045] It should be noted that the onBindViewHolder method of the LoadMoreAdapter adapter mainly binds specific data to the special view corresponding to the ViewHolder instance that has been created by the onCreateViewHolder method, that is, fills the data into the special view so that the user can see the actual content presented by each data item, such as in an information list, displaying the news title, text, release time and other data in the corresponding view.
[0046] In this application, when it is determined that a ViewHolder instance corresponding to loading more views needs to be created, the corresponding ViewHolder instance is created and returned using the layout file. This ViewHolder instance corresponding to loading more views contains the layout structure of loading more views, such as components such as a progress bar indicating the loading status and a TextView that prompts the user with text such as "Loading more". It is a "container" for loading more views and provides a basis for subsequent operations such as data binding.
[0047] Specifically, the onBindViewHolder method usually has two parameters, one is a parameter of ViewHolder type, which is the special view holder instance created in the onCreateViewHolder method, and the other is a parameter of int type that represents the position index of the data item (usually represented by position). When rewriting this method, the first thing to do is to determine whether the view to which data is currently bound is the ViewHolder instance corresponding to loading more views based on the passed-in ViewHolder instance. For example, you can use the instanceof keyword (in languages such as Java) to make a judgment. If the passed-in ViewHolder instance is the ViewHolder instance type corresponding to the previously created ViewHolder instance that loads more views, it means that data binding related operations are currently required for loading more views.
[0048] If the Load More view contains a component such as a progress bar, you can perform corresponding operations on it here. For example, during the data loading process, you can use code logic (in the relevant business logic class, not directly in the onBindViewHolder method here, just call the relevant logic here) to obtain the current data loading progress (such as the percentage of the loaded data volume to the total data volume, etc.), and then set this progress value to the progress bar so that the user can intuitively see the progress of the loading. Assuming that the progress bar object is named progressBar, you can update the display status of the progress bar through progressBar.setProgress(progressValue) (progressValue is the obtained progress value) so that the user knows that the data is continuously loading.
[0049] The text prompt part in the loading more view can also be updated or set according to the actual situation. For example, when loading starts, it will display "Loading more, please wait". If the loading time is long, you can also update the text prompt to "Loading a little slowly, please wait patiently" and other more appropriate content after a certain period of time to inform the user of the current loading status in a friendly manner. Also in the onBindViewHolder method, find the corresponding TextView control (assuming it is named textView), and then use textView.setText("new prompt text") to change the text display content.
[0050] If there are other components in the loading more views, for example, there may be a button for users to manually trigger operations such as reloading, you can also set click event listeners and other operations for the button here, so that the button can respond to user clicks and then execute corresponding business logic, such as re-requesting data from the server.
[0051] After the above data binding and operation of each component in the ViewHolder instance corresponding to the load more view, the load more view carried by this ViewHolder instance already has complete display information, and the view set displayed by the entire RecyclerView component also includes this processed load more view, thus obtaining the current view including the load more view. In this way, when the RecyclerView component displays each view, the user can see the normal data item view and the load more view displayed at the end of the list, and the load more view will show the corresponding status according to the data binding situation, such as the progress bar is moving, the text prompt is changing reasonably, etc., providing users with a good interactive experience.
[0052] In addition to loading more views, this embodiment also includes loading failure views and empty data views.
[0053] Loading failure view: When requesting more data from the server or performing other related data acquisition operations, the request may fail, such as unstable network connection, server failure, etc., which leads to failure to successfully obtain new data. At this time, in order to inform the user of the problem, the loading failure view will be displayed. It may show a text prompt such as "Loading failed" and can be accompanied by some icons to enhance the visual effect, so that users can intuitively understand that there is an abnormality in loading data. The corresponding LoadFailedHolder is responsible for managing the relevant layout and display logic of the view.
[0054] Empty data view: All the data in the data list has been displayed, and there is no more data to load, or the data source itself is empty and there is no data to display. In these cases, it is necessary to display an empty data view, such as presenting a text prompt such as "No more data" to inform the user of the current situation and avoid user confusion. NoMoreHolder is responsible for controlling how this view should be laid out and displayed.
[0055] The styles of these views are not fixed. Developers are allowed to customize them according to the overall style of the application and specific usage scenarios. For example, for the loading failure view, in addition to the text prompt, you can also add an icon indicating the error. The icon can be a cartoon image, a simple style, or other styles, as long as it can clearly convey the information of the loading failure. The same is true for the empty data view. The layout of the text, the display position, and whether to add some decorative elements can all be decided by the developer. It also has strong flexibility in terms of content. For example, the text content of the loading failure view can explain in detail the possible reasons for the failure, "The network connection is abnormal and no more products can be loaded. Please check your network settings", so that users can know the problem more clearly. The empty data view can also customize the content according to different data source scenarios. If the data obtained from a certain category is empty, you can write a prompt content that is more in line with the actual situation, such as "There are no products in this category".
[0056] Furthermore, you can also override the onViewAttachedToWindow method of the inherited class to initialize the current view that contains more views to be loaded.
[0057] It should be noted that whenever a data item's view is ready to be displayed on the screen, whether it has just entered the visible range of the screen or re-entered the visible range due to scrolling of the RecyclerView component, the onViewAttachedToWindow method will be triggered. For the current view that contains more views to be loaded, it is important to override the onViewAttachedToWindow method and perform initialization processing.
[0058] Specifically, the Load More view is a special view placed at the end of the data list to prompt users to load more data and other related operations. Each time it is displayed, it may be necessary to ensure that it is in a correct initial state. For example, there is a progress bar in the Load More view to show the data loading progress. Each time it enters the visible range, it is necessary to ensure that the progress bar starts from the initial state (usually the progress is 0) to avoid the progress left over from the last display or abnormal state affecting the user's judgment of the current loading status.
[0059] This method can be used to set some display-related properties of the Load More view. For example, its visibility (show or hide). If you do not want the Load More view to be displayed at first, and only display it when certain conditions are met (such as the user swipes to a certain distance from the bottom of the list), then you can control its visibility according to the corresponding logic in this method; you can also set other display properties of the view, such as transparency and size, so that it can be displayed to the user in the expected appearance.
[0060] If the load more view contains some interactive elements, such as a button for users to manually trigger the operation of loading more data, you can add a click event listener to this button in the onViewAttachedToWindow method. In this way, when the view enters the visible range, the button can respond to the user's click operation normally. After the user clicks, the corresponding business logic can be triggered, such as requesting new data from the server, etc., which enhances the interactivity and usability of the view.
[0061] Step S103: The LayoutManager component of the RecyclerView component determines whether the current data item is the last data item in the current data item list.
[0062] In this application, you can override the onScrolled method of the RecyclerView.OnScrollListener interface to get the type of the LayoutManager component of the RecyclerView component. If the type of the LayoutManager component is a LinearLayoutManager component or a GridLayoutManager component, call the findLastVisibleItemPosition method of the LinearLayoutManager component or the GridLayoutManager component to determine whether the current data item is the last data item in the current data item list. If the type of the LayoutManager component is a StaggeredGridLayoutManager component, call the findLastCompletelyVisibleItemPositions method of the StaggeredGridLayoutManager component to determine whether the current data item is the last data item in the current data item list.
[0063] Whenever the RecyclerView component scrolls, that is, when the user slides the current list of data items in the RecyclerView component on the screen, the onScrolled method will be triggered. The onScrolled method can obtain some scroll-related information during the scrolling of the RecyclerView component, and then execute corresponding business logic based on this information, such as determining whether the scroll has reached the bottom of the list.
[0064] When overriding the onScrolled method of the RecyclerView.OnScrollListener interface, the RecyclerView component itself does not know how to arrange and display data items. You can get the LayoutManager component associated with it through the RecyclerView instance. Because different types of LayoutManager groups use different methods when processing data item positions and other related judgments, you can determine which type this LayoutManager component belongs to.
[0065] If the type of the LayoutManager component is a LinearLayoutManager component (commonly used to implement simple vertical or horizontal linear list displays, such as news lists, chat history lists, etc.) or a GridLayoutManager component (used to create regular grid-like data displays, such as album image displays, product list displays, etc. that use grid layouts), then you can call the findLastVisibleItemPosition method they provide. The function of this method is to return the position index of the last visible data item in the current RecyclerView component. By obtaining the position index of the last visible data item, combined with the total number of data items in the current data item list, it can be determined whether the current data item is the last data item in the current data item list. Specifically, if the position index of the last visible data item is equal to the total number of data items minus 1, it can be determined that the current data item is the last data item in the list.
[0066] If the type of the LayoutManager component is a StaggeredGridLayoutManager component (commonly used in some irregular, staggered grid layout scenarios, such as waterfall layout display pictures, etc.), you need to call its findLastCompletelyVisibleItemPositions method. This method is used to return the position index array of the last set of completely visible data items in the staggered grid layout (because in the staggered grid layout, the same row may have elements of different heights, so there may be more than one completely visible element in the end, and their position indexes will be returned in array form). It is also necessary to combine information such as the total number of data items to determine whether the current data item is the last data item. However, the situation here is relatively complicated, and the returned position index array needs to be analyzed. For example, you can determine whether the maximum value in the array (because in the last set of completely visible data items, the one with the largest index corresponds to the last element in the list order) is equal to the total number of data items minus 1. If they are equal, it can be determined that the current data item is the last data item in the current data item list.
[0067] When the current data item is the last data item in the current data item list, step S104 is executed.
[0068] Step S104: Load the next data item list of the current data item list, and call the notifyDataSetChanged method of the RecyclerView component to refresh the current view.
[0069] In this application, when it comes to the RecyclerView component displaying a large amount of data and the need to implement the function of dynamically loading more data, an OnLoadMoreListener interface is often defined. The main purpose of this interface is to provide a mechanism that allows developers to listen to and respond to the specific operation requirement of "loading more". Among them, the onLoadMore method is the key method in the OnLoadMoreListener interface. Usually, the onLoadMore method is called based on specific trigger conditions. When the user continues to slide down the list of data items until it slides to the bottom of the list, it means that the user may want to see more data content. At this time, the onLoadMore method can be triggered to load the next list of data items. Other trigger conditions can also be set according to specific business needs, such as automatically triggering loading more when the number of data items is less than a certain threshold, or automatically trying to load more data after a certain time interval.
[0070] The source of the next data item list generally depends on the overall architecture of the application and the location of the data storage. If the application data is obtained from a network server, then the onLoadMore method needs to build appropriate network request logic to send a request to the server to obtain the next page of data. The request usually contains some necessary parameters, such as the current page number, the amount of data per page, and other information. The server searches the database or other storage media based on these parameters and returns the corresponding next data item list data.
[0071] Furthermore, when the user has not explicitly requested to obtain the next page of data, the data of the next page can be pre-judged and automatically loaded in advance. In this way, when the user continues to browse the current page to a certain extent (for example, when the current page is almost finished), the data of the next page is ready in the background and can be seamlessly switched to display to the user, reducing the time the user waits for new data to load, making the entire browsing process smoother. Whether to preload the next page of data is determined based on the value of the preload attribute mPreLoadMoreEnabled.
[0072] Among them, the mPreLoadMoreEnabled property is usually of Boolean type (in most programming languages, Boolean values have only two conditions: True and False). It is like a switch to determine whether the subsequent preloading-related logic is executed. The setting of this property may be affected by many factors, such as the network environment where the application is located (enable preloading when the network condition is good, and disable it when the network condition is poor to avoid wasting traffic and slow loading), the performance of the device (high-performance devices are more suitable for enabling preloading, while devices with poor performance may disable this function due to the burden of preloading), or according to the user's personalized settings (some users want to save traffic and reduce the number of background programs, so they can disable preloading by themselves).
[0073] In summary, the present application provides a method for loading data items, the method comprising: in response to the vertical sliding of the current data item list of the RecyclerView component, the last visible data item in the current data item list is used as the current data item. Based on the current data item list and the current data item, the getItemCount method, getItemViewType method, onCreateViewHolder method and onBindViewHolder method of the inherited class are rewritten to obtain the current view containing more views to be loaded, and the inherited class is inherited from RecyclerView.Adapter. Based on the LayoutManager component of the RecyclerView component, it is determined whether the current data item is the last data item in the current data item list. If the current data item is the last data item in the current data item list, the next data item list of the current data item list is loaded, and the notifyDataSetChanged method of the RecyclerView component is called to refresh the current view. The present application wraps the original class through the inherited class, and realizes non-invasive loading of more functions without destroying the original class, thereby maintaining the performance stability of the RecyclerView component and improving the loading effect.
[0074] In order to solve the above problems, an embodiment of the present application provides a method for loading a common view. The method for loading a common view in an embodiment of the present application is described in detail below with reference to the accompanying drawings.
[0075] Reference Figure 2 , Figure 2 A flowchart of a method for loading a normal view provided in an embodiment of the present application is provided. The method for loading a normal view provided in an embodiment of the present application includes steps S201 to S203, and these steps are described in detail below.
[0076] Step S201: based on the position index of the current data item and the total number of data items in the current data item list, rewrite the getItemViewType method of the inherited class to obtain the viewType parameter of the current data item.
[0077] When the LoadMoreAdapter adapter overrides the getItemViewType method, the getItemViewType method will pass in the position index of the current data item as a parameter (assuming the parameter name is position), and then obtain the total number of data items in the current data item list that has been previously determined inside the getItemViewType method. Next, by comparing the position with the value of the total number minus 1, determine whether the current position is the position for loading more views. If position is not equal to this value, it means that the current position corresponds to an ordinary data item view. At this time, the corresponding viewType parameter value is returned according to the logic of the original type identifier assigned to the ordinary data item view (for example, different values between 0 and 99 are returned based on factors such as the different business types of the data items).
[0078] Step S202: Based on the viewType parameter of the current data item, call the onCreatViewHolder method and the onBindViewHolder method of the original class to obtain the current view containing the current data item.
[0079] It should be noted that the inherited class holds an instance of the original class of the RecyclerView.Adapter. The original class of RecyclerView.Adapter itself contains some basic and common functions. Although the inherited class rewrites some methods according to specific needs, sometimes you may still want to directly use the already written and applicable functions in the original class. For example, the original class may have some default processing logic for data items or some basic specifications for view creation. In some cases, there is no need to re-implement them. By holding an instance of the original class, you can easily call these existing functions, avoid reinventing the wheel, and improve code reusability and development efficiency.
[0080] In this application, if the passed in viewType parameter value does not correspond to the type identifier for loading more views, it is processed according to the normal logic of creating a ViewHolder instance corresponding to the normal data item view, such as loading other corresponding layout files to create the corresponding ViewHolder instance according to different normal data item view types. If the passed in ViewHolder instance is not the type corresponding to loading more views, it is processed according to the logic of binding data for the normal data item view, such as filling the values of each field in the data model at the corresponding position into the corresponding positions such as the text box and picture control in the normal data item view, to ensure that the data display of the entire RecyclerView component is complete and accurate.
[0081] Specifically, the onCreateViewHolder method is the original class of RecyclerView.Adapter and is mainly responsible for creating the ViewHolder instance corresponding to each data item. The ViewHolder instance caches the subviews in the normal view (such as text boxes, image controls, buttons, etc.). This caching mechanism can effectively avoid repeatedly searching and creating these subviews during the scrolling of the RecyclerView component, thereby improving the performance of the list scrolling and allowing users to experience a smoother experience when browsing the data list.
[0082] The onBindViewHolder method in the original class of RecyclerView.Adapter is mainly responsible for binding specific data to the normal view corresponding to the ViewHolder instance that has been created by the onCreateViewHolder method, that is, filling the data into the normal view so that the user can see the actual content presented by each data item.
[0083] Step S203: Call the onViewAttachedToWindow method of the original class to initialize the current view containing the current data item.
[0084] In this application, the existence and function of the original Adapter cannot be ignored. The onViewAttachedToWindow() method of the registered original Adapter needs to be called so that the original class can be initialized according to its established logic to ensure that the basic functions of the entire data display system are not affected.
[0085] For regular data item views, the original class may have default settings for the visibility, size and other display properties of the view in the onViewAttachedToWindow method, or have logic for coordinating with the scrolling mechanism and layout management of the RecyclerView component. If the original Adapter is not allowed to execute this method, these originally normal functions may be missing or abnormal, which will affect the accuracy and fluency of the entire data display. Therefore, to ensure the correct function of the original class, it is necessary to let it perform its initialization operation at the right time (that is, when onViewAttachedToWindow is triggered).
[0086] In summary, the method of loading a common view provided in the embodiment of the present application can realize function reuse when the inherited class holds an instance of the original class of RecyclerView.Adapter. The original class defines many basic and common methods and logic related to RecyclerView data display. When the project continues to develop or the requirements change, holding an instance of the original class can make code maintenance and function expansion easier.
[0087] A method for loading a data item provided in an embodiment of the present application is introduced above, and a device for executing the method for loading a data item is introduced below.
[0088] See also Figure 3 , Figure 3 This is a schematic diagram of the structure of a data item loading device provided in an embodiment of the present application. Figure 3 As shown, the data item loading device includes: The responding unit 11 is used to respond to the vertical sliding of the current data item list of the RecyclerView component and take the last visible data item in the current data item list as the current data item.
[0089] The rewriting unit 12 is used to rewrite the getItemCount method, getItemViewType method, onCreateViewHolder method and onBindViewHolder method of the inherited class based on the current data item list and the current data item, so as to obtain the current view including loading more views, and the inherited class inherits from RecyclerView.Adapter.
[0090] The judging unit 13 is configured to judge, based on the LayoutManager component of the RecyclerView component, whether the current data item is the last data item in the current data item list.
[0091] The loading unit 14 is used to load the next data item list of the current data item list if the current data item is the last data item in the current data item list, and call the notifyDataSetChanged method of the RecyclerView component to refresh the current view.
[0092] In a possible implementation, the device further includes: A detection unit is used to detect the vertical sliding of the current data item list based on the RecyclerView.OnScrollListener interface of the RecyclerView component, and the RecyclerView.OnScrollListener interface is set based on the onAttachedToRecyclerView method of the inherited class.
[0093] In a possible implementation, the rewriting unit 12 includes: The first rewriting subunit is used to rewrite the getItemCount method of the inherited class based on the current data item list to obtain the total number of data items in the current data item list.
[0094] The second rewriting subunit is used to rewrite the getItemViewType method of the inherited class based on the position index of the current data item and the total number of data items in the current data item list to obtain the viewType parameter for loading more views.
[0095] The third rewriting sub-unit is used to rewrite the onCreatViewHolder method of the inherited class based on the viewType parameter of the loading more views, and obtain the ViewHolder instance corresponding to the loading more views.
[0096] The fourth rewriting sub-unit is used to rewrite the onBindViewHolder method of the inherited class based on the loading more views and the ViewHolder instance corresponding to the loading more views, so as to obtain the current view including the loading more views.
[0097] In a possible implementation, the determining unit 13 includes: The fifth rewriting subunit is used to rewrite the onScrolled method of the RecyclerView.OnScrollListener interface to obtain the type of the LayoutManager component of the RecyclerView component.
[0098] The first judgment subunit is used to call the findLastVisibleItemPosition method of the LinearLayoutManager component or the GridLayoutManager component if the type of the LayoutManager component is a LinearLayoutManager component or a GridLayoutManager component, to determine whether the current data item is the last data item in the current data item list.
[0099] The second judgment subunit is used to call the findLastCompletelyVisibleItemPositions method of the StaggeredGridLayoutManager component if the type of the LayoutManager component is the StaggeredGridLayoutManager component, to judge whether the current data item is the last data item in the current data item list.
[0100] In a possible implementation, the device further includes: The first processing unit is used to rewrite the onViewAttachedToWindow method of the inherited class to initialize the current view containing more views to be loaded.
[0101] In a possible implementation, the inherited class holds an instance of the original class of the RecyclerView.Adapter, and the apparatus further includes: The method rewriting unit is used to rewrite the getItemViewType method of the inherited class based on the position index of the current data item and the total number of data items in the current data item list to obtain the viewType parameter of the current data item.
[0102] The calling unit is used to call the onCreatViewHolder method and the onBindViewHolder method of the original class based on the viewType parameter of the current data item to obtain the current view containing the current data item.
[0103] The second processing unit is used to call the onViewAttachedToWindow method of the original class to initialize the current view containing the current data item.
[0104] The present application also provides an electronic device in an embodiment. Figure 4 As shown, it shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiment of the present application. The electronic device in the embodiment of the present application may include but is not limited to fixed terminals such as mobile phones, laptops, PDAs (personal digital assistants), PADs (tablet computers), desktop computers, etc. Figure 4 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0105] like Figure 4As shown, the electronic device may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 401, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 402 or a program loaded from a storage device 408 to a random access memory (RAM) 403. When the electronic device is powered on, various programs and data required for the operation of the electronic device are also stored in the RAM 403. The processing device 401, the ROM 402, and the RAM 403 are connected to each other via a bus 404. An input / output (I / O) interface 405 is also connected to the bus 404.
[0106] Typically, the following devices may be connected to the I / O interface 405: an input device 406 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 407 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 408 including, for example, a memory card, a hard disk, etc.; and a communication device 409. The communication device 409 may allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. Although Figure 4 An electronic device having various devices is shown, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0107] An embodiment of the present application also provides a computer program product including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements any method for loading data items provided in the embodiment of the present application.
[0108] A computer-readable storage medium is also provided in an embodiment of the present application. The storage medium carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement any data item loading method provided in the embodiment of the present application.
[0109] It should also be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed over multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the drawings of the device embodiments provided by the present application, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines.
[0110] Through the description of the above implementation mode, the technicians in the field can clearly understand that the present application can be implemented by means of software plus necessary general hardware, and of course, it can also be implemented by special hardware including special integrated circuits, special CPUs, special memories, special components, etc. In general, all functions completed by computer programs can be easily implemented by corresponding hardware, and the specific hardware structure used to implement the same function can also be various, such as analog circuits, digital circuits or special circuits. However, for the present application, software program implementation is a better implementation mode in more cases. Based on such an understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a readable storage medium, such as a computer floppy disk, a U disk, a mobile hard disk, a ROM, a RAM, a disk or an optical disk, etc., including a number of instructions to enable a computer device (which can be a personal computer, a training device, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0111] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or part of the embodiments may be implemented in the form of a computer program product.
[0112] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from a website site, a computer, a training device, or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, training device, or data center. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a training device, a data center, etc. that includes one or more available media integrations. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)), etc.
Claims
1. A method for loading a data item, characterized in that: include: In response to a vertical slide of a current data item list of the RecyclerView component, taking the last visible data item in the current data item list as the current data item; Based on the current data item list and the current data item, rewrite the getItemCount method, getItemViewType method, onCreateViewHolder method and onBindViewHolder method of the inherited class to obtain the current view including loading more views, the inherited class inherits from RecyclerView.Adapter; Determine, based on the LayoutManager component of the RecyclerView component, whether the current data item is the last data item in the current data item list; If the current data item is the last data item in the current data item list, the next data item list of the current data item list is loaded, and the notifyDataSetChanged method of the RecyclerView component is called to refresh the current view.
2. The method for loading data items according to claim 1, characterized in that: Before responding to the vertical sliding of the current data item list of the RecyclerView component, the method further includes: The vertical sliding of the current data item list is detected based on the RecyclerView.OnScrollListener interface of the RecyclerView component, and the RecyclerView.OnScrollListener interface is set based on the onAttachedToRecyclerView method of the inherited class.
3. The method for loading data items according to claim 1, characterized in that: Based on the current data item list and the current data item, the getItemCount method, the getItemViewType method, the onCreateViewHolder method and the onBindViewHolder method of the inherited class are rewritten to obtain the current view including loading more views, including: Based on the current data item list, rewrite the getItemCount method of the inherited class to obtain the total number of data items in the current data item list; Based on the position index of the current data item and the total number of data items in the current data item list, rewrite the getItemViewType method of the inherited class to obtain the viewType parameter for loading more views; Based on the viewType parameter of the loading more views, rewrite the onCreatViewHolder method of the inherited class to obtain the ViewHolder instance corresponding to the loading more views; Based on the loading more views and the ViewHolder instance corresponding to the loading more views, the onBindViewHolder method of the inherited class is rewritten to obtain the current view containing the loading more views.
4. The method for loading data items according to claim 2, characterized in that: The LayoutManager component based on the RecyclerView component determines whether the current data item is the last data item in the current data item list, including: Rewrite the onScrolled method of the RecyclerView.OnScrollListener interface to obtain the type of the LayoutManager component of the RecyclerView component; If the type of the LayoutManager component is a LinearLayoutManager component or a GridLayoutManager component, calling the findLastVisibleItemPosition method of the LinearLayoutManager component or the GridLayoutManager component to determine whether the current data item is the last data item in the current data item list; If the type of the LayoutManager component is a StaggeredGridLayoutManager component, the findLastCompletelyVisibleItemPositions method of the StaggeredGridLayoutManager component is called to determine whether the current data item is the last data item in the current data item list.
5. The method for loading data items according to claim 1, characterized in that: After rewriting the getItemCount method, getItemViewType method, onCreateViewHolder method and onBindViewHolder method of the inherited class based on the current data item list and the current data item to obtain the current view including loading more views, the method further includes: Rewrite the onViewAttachedToWindow method of the inherited class to initialize the current view that contains more views to be loaded.
6. The method for loading data items according to claim 3, characterized in that: The inherited class holds an instance of the original class of the RecyclerView.Adapter, and the method further includes: Based on the position index of the current data item and the total number of data items in the current data item list, rewrite the getItemViewType method of the inherited class to obtain the viewType parameter of the current data item; Based on the viewType parameter of the current data item, the onCreatViewHolder method and the onBindViewHolder method of the original class are called to obtain the current view containing the current data item; The onViewAttachedToWindow method of the original class is called to initialize the current view containing the current data item.
7. A data item loading device, characterized in that: include: A response unit, used to respond to the vertical sliding of the current data item list of the RecyclerView component and take the last visible data item in the current data item list as the current data item; A rewriting unit, configured to rewrite a getItemCount method, a getItemViewType method, an onCreateViewHolder method, and an onBindViewHolder method of an inherited class based on the current data item list and the current data item, to obtain a current view including loading more views, wherein the inherited class inherits from RecyclerView.Adapter, and the inherited class holds an instance of an original class of the RecyclerView.Adapter; A judging unit, configured to judge whether the current data item is the last data item in the current data item list based on the LayoutManager component of the RecyclerView component; A loading unit is used to load the next data item list of the current data item list if the current data item is the last data item in the current data item list, and call the notifyDataSetChanged method of the RecyclerView component to refresh the current view.
8. A computer program product, characterized in that It comprises computer-readable instructions, and when the computer-readable instructions are executed on an electronic device, the electronic device implements the method for loading a data item as claimed in any one of claims 1 to 6.
9. An electronic device, characterized in that: The method comprises at least one processor and a memory connected to the processor, wherein: The memory is used to store computer programs; The processor is used to execute the computer program so that the electronic device can implement the method for loading data items as described in any one of claims 1 to 6.
10. A computer storage medium, characterized in that: The storage medium carries one or more computer programs, and when the one or more computer programs are executed by an electronic device, the electronic device can implement the method for loading data items as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Method for accelerating display speed of Android list
CN112272329A
Method for dynamically combining Android layouts
CN112363723A
Event response apparatus and method, electronic device, and storage medium
WO2022062835A1