Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

preface

The last post was a shame because I wasted a lot of time because I didn’t know English. Otherwise, I would have spent a whole day trying to save my face by introducing FlycoTabLayout, a TAB library I’ve been using for a long time

Simple to use

Xml

<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".flyco.FlycoActivity"> <com.flyco.tablayout.SlidingTabLayout android:layout_width="match_parent" android:layout_height="50dp" app:tl_indicator_color="@color/teal_200" app:tl_indicator_height="10dp" app:tl_indicator_corner_radius="8dp" app:tl_tab_space_equal="true" android:id="@+id/flyco" app:tl_textSelectColor="#E91E63" app:layout_constraintTop_toTopOf="parent" app:tl_textUnselectColor="@color/black"/> <androidx.viewpager.widget.ViewPager  android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/vp" app:layout_constraintTop_toBottomOf="@id/flyco" app:layout_constraintBottom_toBottomOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code

Activity

public class FlycoActivity extends AppCompatActivity { private SlidingTabLayout mFlyco; private ViewPager mVp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flyco); initView(); } private void initView() { mFlyco = findViewById(R.id.flyco); mVp = findViewById(R.id.vp); String[] titles = new String[5]; ArrayList<Fragment> fragments = new ArrayList<>(); for (int i = 0; i < 5; I ++) {titles[I] = (" titles "+ I); fragments.add(new FlycoFragment()); } mFlyco.setViewPager(mVp, titles, this, fragments); }}Copy the code

Done, look at the effect

Whoa, whoa, whoa!! It's fucking ugly! This is my XML attribute setting problem, if you don't care about the details of the Activity code is very little, it is a lazy person must, outsourcing must ah. Just need a viewpager, title, context,fragment, directly meet the common page in the project, move as quickly as you use it!

Ahem, digress, generally in the project have this page, and requirements to appropriate TAB, is set to bold, although the author of the library open this set of apis, but set up only slide the bold, and the last updated is 2016, true hip ao, we either in monitoring the operation, or deeper source to operate , today we continue to in-depth source, Gogogo!

operation

First let's analyze this thing. I estimate the enclosed LinearLayout, and then snap a series of operations for setting the Viewpager. The first thing is to look at the SetAdapter method, let's go ahead and find it.

This library opens the Settings for selected and unselected font colors. Let’s take a look at where we got them, and then bold them in the same place.

Let me tell you a little bit about how I can quickly find that property, okay

First go to XML, then hold down your Control and click the left mouse button to go to Value.xml, which contains some of the library's properties. Then copy the property we clicked on tl_textSelectColor and go to SlidingTabLayout to search

Look at the naming. How delightful. If Uncle Hule…

All right, let’s find the target code and move it in and get started

The author of the library defines a notifyDataSetChanged method which updates the data, updates the text state, and adds a Tab to the LinearLayout, which is Linearlyaout because the Tab is the encapsulated LienarLayout

private void updateTabStyles() { for (int i = 0; i < mTabCount; i++) { View v = mTabsContainer.getChildAt(i); // v.setPadding((int) mTabPadding, v.getPaddingTop(), (int) mTabPadding, v.getPaddingBottom()); TextView tv_tab_title = (TextView) v.findViewById(com.flyco.tablayout.R.id.tv_tab_title); if (tv_tab_title ! = null) { tv_tab_title.setTextColor(i == mCurrentTab ? mTextSelectColor : mTextUnselectColor); tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextsize); tv_tab_title.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0); if (mTextAllCaps) { tv_tab_title.setText(tv_tab_title.getText().toString().toUpperCase()); } if (mTextBold == TEXT_BOLD_BOTH) { tv_tab_title.getPaint().setFakeBoldText(true); } else if (mTextBold == TEXT_BOLD_NONE) { tv_tab_title.getPaint().setFakeBoldText(false); }else{ tv_tab_title.getPaint().setFakeBoldText(mCurrentTab==i); }}}}Copy the code

And then there’s a second method, the first one is not bold because the ViewPager listener didn’t go Select, so we’ll just call it manually

mFlyco.onPageSelected(0);
Copy the code

In the Activity add line, manually setting a callback to OnPageSelected will do the same.

B. Although we wasted a lot of energy to modify the source code, but we harvest a lot of ah, before I looked at the source code to know that setFakeBoldText can also be bold ah, and not so obtruse, looks very natural. Well, we didn’t get nothing, did we?

Ok, that’s all for today. Goodbye to old age commercials

I almost forgot the renderings