Recently, I received a task to modify the indicator line width in the official support package TabLayout to a fixed value. Of course, there is some reflection and padding on the Internet, but it can not solve the problem. You tell me github and XXX similar controls, but why I want to give up the source of Google god, change can increase the new function, in order to achieve the purpose, I began the following series of SAO operation.
The old rule: show the renderings first
1. One of the operations: copy support package TabLayout as their own custom view
This operation is based on support ‘27.1.0’ version, from support ‘27.1.0’ test out the file to my project directory as shown below, the blue part, four files, of course, is not smooth sailing, need to change the package name, cancel the annotation warning, in short, the source will be released later
2 SAO operation two: FUCK source code, after reading began to transform
First of all, the line of the indicator is drawn. The key code is as follows (the following modified code is the tabLayout class).
canvas.drawRect(mIndicatorLeft, getHeight() - mSelectedIndicatorHeight,
mIndicatorRight, getHeight(), mSelectedIndicatorPaint);
Copy the code
OK, the mIndicatorLeft is calculated by the offset when you scroll or when you hit cut TAB, but it doesn’t matter, so that’s the first goal. MIndicatorLeft and mIndicatorRight are both mIndicatorRight and mIndicatorLeft are mIndicatorLeft and mIndicatorRight are mIndicatorLeft and mIndicatorRight are mIndicatorRight
private int mSelectedIndicatorWidth = dpToPx(27);;
private int mMinTabWidth = Integer.MAX_VALUE;
Copy the code
I’m going to be a little lazy here and not expose the method, but I’m going to make it 27dp wide
All right, we’re done modifying the line width target. (Extension: here you can also modify the draw method to draw a graph, or draw a small circle, etc.)
Next, add the transition animation that indicates the line sliding to cut the TAB
It’s very simple and I’ll just put in the code, but the key is to do something inside the onPageScrolled method
Maybe 50 changes in total, and we’re done. Isn’t that easy? There are a lot of benefits to this, why? XML basically does not need to change, tablayout name is changed, the code is also changed guide package, when replacing the official tablayout code almost does not need to change, is not very cool?
3. Github download, give a star if you like, if it helps you
Github.com/zjw-swun/Ap…
4. To summarize
Official support packages can be so capricious copy out, sometimes a test of a class does not involve other classes, good fake.
License
Copyright 2018 zjw-swun
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except inThe compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed toin writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Copy the code