Thoughts and methods of Android imitation to realize free fish home page
First, let's look at the renderings
The demo is written based on MVVM mode. You are welcome to criticize and correct it.
Among them, pagesnaphelper is used for unlimited rotation of banner, and subsequent recycleview can achieve more effects similar to viewpage
It can be seen that the page can be roughly divided into these parts
1. At the top is a rotating banner
2. There may be other function lists in the middle
3. Finally, there is the tab page (here are the fresh and nearby lists)
OK, there may be two ideas when you see such layout requirements
1. The overall layout is a refreshlayout with a recycleview embedded. The banner page, other function lists and tablayout are added to the recycleview as the head of the recycleview. The real list items are below the tablayout
2. The overall layout is also a refreshlayout. Internally, there is a nestscrollview, banner page and other function list. Tablayout is arranged in nestscrollview in turn, and then a FrameLayout is arranged at the bottom. When tablayout is switched, different fragments are switched
The first method is used in the demo. The second method takes into account the conflict between the sliding of swiperefreshlayout and internal FrameLayout, and tries to write it later
Next, consider the issues that need to be considered
Here are some core codes and ideas
The first is the layout. The layout is very simple. A FrameLayout is packaged in swiperefreshlayout, and then a recycleview is included in the FrameLayout
Next, let's see how stickyhead is implemented
This is the definition of variables. The following class is to extract some variables involved in page logic
This is the definition of variables, and then initialize two tablayouts, mainly because you need to listen to the switching of tablayout
The logic of this section is relatively simple, which is to maintain the unity of tablayout switching state. When tablayout switching, you need to set the tab selected by stickytablayout. Mviewmodel. Changehomedata (position) is to switch data, which will be analyzed below
Next is the important code of stickyhead
The main logic is to obtain the position of tablayout in the window first. If the Y coordinate is less than the Y coordinate of the paste head, the paste head will be added to the layout and displayed. Otherwise, the paste head layout will be removed from the layout. The value of count is determined to prevent repeated addition and removal of paste header layout.
Mbinding.layoutrefresh.setenabled (true / false) is to eliminate the drop-down refresh error of the outer swiperefreshlayout when the paste head is fixed on the top. The commented out code will be described below
Only so much code above is needed to implement a stickyhead. There is a small problem encountered during the test, that is, an error that recycleview returns to its original position due to focus reset. The following is a temporary solution
Here's how to implement a waiting page when loading fresh or nearby data for the first time
The main idea is this
1. The waiting loadingview is added after tablayout as the header of recycleview. When the data is loaded, the loadingview is set to invisible
2. Because tablayout will be switched, the data of recycleview will be redrawn, and then the recycleview will return to the initial position. Therefore, it is necessary to record the position of recycleview, and then manually slide to the recorded position
Let's look at the code
Finally, let's look at the implementation of fresh and nearby pages when loading more
Here, the adapter uses the third-party brvah, so the brvah relative to the loadingmore state is sealed for me. Although it is a list, two lists reuse one list, so we need to record two loadingmore states here. When switching, the loadingmore state of the column table is correct. Let's see the main code below
Br.refreshloading and br.nearloading are both listening for the first load. Here
This is to prevent the loading more layout from being displayed when the loading page is loaded for the first time
Br. Loadingmorestatus is the adapter that listens to the status of loadingmore to update the list
The other main ViewModel code is in homeviewmodel.
Main points
Project link: https://github.com/ly85206559/demo4Fish
Local download: click here
summary
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.