Recently I will make a page with two tab switching. The two pages have a common description information area. Both tabs are listviews. You can pull them up or down to refresh. There is a tab switching area in the middle of the page. Slide it up. When the tab area reaches the top, it stops moving. Pull it down and return to the initial position. Let's look at the same style picture first!
The entire requirement is roughly as shown in the figure above. There are no screenshots for pull-up refresh and pull-down refresh. The open source control PullToRefreshListView is used to achieve this effect.
1. The general idea is that for the sake of simplicity, I don’t want to monitor many gesture issues, so I adopt the following method to implement it opportunistically.
a. The entire page is a listview, and the public area is added as the header of the listview. The two switching tabs are also added as a header,
b. When laying out the page, add a layer above the listview, and put the tab layout inside. The layout of this tab is the same as the layout in the header of the listview,
c. Later, when the listview slides, the display and hiding of the page tab layout are processed in the onScroll function. When the tab layout of the listview reaches the top of the screen, the tab layout in the page is displayed. When the listview slides down, the entire tab appears as a shadow. Tab layout in the interface
d. Tab switching. Since the data of tab1 and tab2 are different, three data sources are used. When the tab is switched, the data switches back and forth. When the tab is clicked, the currently displayed tab is remembered. pos and offset (just remember that there will be deviation when pos is relocated)
The general process of the demo is like this. There is no refresh processing added. Although more logic is processed in the actual project, the demo I don’t want to write too complicated (mainly because no one will read it, so I just read it myself and write a little bit).
2. Having said so much, some people may still not understand it. Let’s take a look at the code.
a. The first is the interface layout, with two layers of protection, a listview on the bottom and a tab layout on the top. The interface Layout up_float_first_activity.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
b. tab layout, up_float_tab_layout.xml, text all use selector, so that it can be highlighted when selected
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
c. Public part layout up_float_common_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
d. Next is the code of the main page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
Summary,
a The above demo only achieves the effect of sliding up. In fact, it has great limitations, two The item layout of the tabs must be consistent so that they can be switched freely. Secondly, the two tabs cannot slide left or right
b The above is only suitable for two or one tab. The more variable states you need to control, the more variable states you need to control. It is easy to make mistakes, and the above It does not include the effect of refresh. When the data is returned, it cannot just be added to the item, but the relationship between the refresh tab and the currently displayed tab must be judged.
c After reading other open source projects, if I have time, I will write a demo to create real multiple tabs, and can switch left and right.
The above is the entire content of this article. I hope it will be helpful to everyone learning Android software programming.
For more articles related to the Android Listview pull up and down refresh tab sliding switching function, please pay attention to the PHP Chinese website!