Hire the author: Dennis M
Sticky navigation layout Android Image Source unsplash.com.
Here is the GitHub link for this project sticky-nav-android.
Introduction
In this article, we will be creating a sticky navigation view in android. We will view it by scrolling up to hide the header and display the tab layout below it fully. I will be using the Java programming language for the development of this android project. There are two ways of implementing it.
Firstly, we will use ScrollView embedded software introduction +ViewPager in ScrollView. This method is purely native and does not involve custom controls, but this nesting involves measurement and event conflict handling. It is quite easy to imagine, but it is actually quite laborious to do it. Second, we will focus on transforming ScrollView as an external layer into a custom, inherited from LinearLayout, called StickyNavLayout. Tabbed activity will be used since provides a way to display tabs horizontally. When used together with a ViewPager, a TabLayout can provide a familiar interface for navigating between pages in a swipe view.
This project will offer insights into the workings of a sticky navigation view in Android and will help the reader to get a better understanding of the inner-machinery that goes behind implementing such features.
Photo Preview


Glossary
StickyScrollView uses an android scroll view. All you have to do is changing the XML layout from the default constraint layout to scroll view layout. This will specify the architecture indicators that need to be attached.
Steps:
Step 1: Creating the activity_main.xml file
We have said above that the reason for the second method is to look at the ease of use. This is done using the custom id resource file, shown below:
Many id tools have been described, especially for ease of use. They are in use when make-up is announced. You should be able to use it by looking at the word. If you can’t use it, that’s fine. Then I will attach a layout file. This is not really a method to use, but you will see it below, so post it in advance.
Now, go to activity_main.xml file. Change your default to Relative Layout and add other attributes such as:
<com.quick.lib.StickyNavLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" > | |
<RelativeLayout | |
android:id="@id/id_stickynavlayout_topview" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="#FFFF00" > | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="240dp" | |
android:gravity="center" | |
android:text="Sticky Header" | |
android:textSize="30sp" | |
android:textStyle="bold" /> | |
</RelativeLayout> | |
<com.quick.lib.SimpleViewPagerIndicator | |
android:id="@id/id_stickynavlayout_indicator" | |
android:layout_width="match_parent" | |
android:layout_height="50dp" | |
android:background="#ffffffff" > | |
</com.quick.lib.SimpleViewPagerIndicator> | |
<android.support.v4.view.ViewPager | |
android:id="@id/id_stickynavlayout_viewpager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="#44ff0000" > | |
</android.support.v4.view.ViewPager> | |
</com.quick.lib.StickyNavLayout> |
Step 3: Creating the StickyNavLayout file
The external layer is our custom control StickyNavLayout, then a top-level content, Vp index, and ViewPager. According to the translation, just write it right. Note that some IDs use our pre-set ID resources. Because our StickyNavLayout needs access to id control to perform certain calculations.
3.1. Structure
First look at the flexibility of the members and the implementation of our flexibility. MTop, mNav, and mViewPager represent the three major components of our structure. Activation completed in onFinishInflate. You see, it’s okay to read directly about our id source. Next, there are othermTouchSlop, mScroller, mVelocity, mMaximumVelocity, Tracker, mLastY, mMinimumVelocity, mDragging. Obviously, everyone would think this is related to a cell phone. OverScroller is an auxiliary class that helps us deal with the mathematical component when we customize the movement. There are many variations related to mVelocityTracker. Yes, to calculate the need for automatic navigation. IMTouchSlop helps me distinguish whether a user clicks or drags. There are many features to install on Android for integration. In ViewConfiguration, everyone can learn about them if they find them interesting. The reason for using these issues is not only to save ourselves to explain but to maintain compliance with systemic behaviour.
3.2. OnMeasure
After reading the layout, because we use LinearLayout, setOrientation directly (LinearLayout.VERTICAL); no need to go to the building, all controls are set up. Later we need to do some processing on onMeasure.
The key is to set the ViewPager height and give it a fixed value. When ViewPager scales itself, if you do not give it a fixed value, the rating result may differ significantly from what you expect. For example, you set its WRAP_CONTENT, and you want him to calculate the child’s height to set his own, and then think more. You can view the ViewPager rating source code:
It can be seen that in both AT_MOST and VERY methods. Henceforth, the incoming value of the parent category is directly calculated, i.e., it will not measure the height of its child. Where the mode is: UNCERTIFIED, then the length is straight to 0. If you have made this example, you should be able to meet this situation. When ViewPager is installed in ScrollView, the rating mode is not checked, Vp is not displayed directly, and the reason is here.
We go, come back, we move on.
After setting the Vp value, in theory, our display is already normal, and the controls are displayed as expected, but, what? We are now in Custom LinearLayout, so should we write the phone alone?
Navigation code is very simple, as everyone knows, just get a straight dx and scrollBy. sticky navigation layout
3.3. OnTouchEvent
It’s really easy because we only need to judge the index y. Therefore, we record the value of y when we’re down, and then we get the dy when we go, just go straight to scrollBy, of course, we’re in the whole process. addMovement (event); So, when we go up, we get velocityY on the v side and we call to move.
Fortunately, the key fling code, OverScroller, available to us, is very good.
Everyone should be clear that when we use auxiliary classes like Scroller, they help us complete the calculation of information and statistics. As for the default, we still need to do it ourselves.
How do you do it, where do you do it? This is nothing but a rewrite of the computeScroll method, in which it is judged whether the scroll is complete, if not, scrollTo is clicked, and finally remember to create the appropriate code:
ok, at this point, our onTouchEvent is done but, don’t be proud, why do you say that? Because of what you’ve done, View information manages to drag up and down. Don’t forget, our current StickyNavLayout has ScrollView inside. After that according to the event transfer method, this internal ScrollView will deal with the drag-and-drop mode, that is, our events will be captured by it.
3.4. onInterceptTouchEvent
Okay, next time we have to deal with prevention. For restrictions, we must clearly know when to enter and when not to. Our current example:
1. If our top view is not completely hidden, then directly reduce the pull-up and down;
2. There is another area that needs to be caught, that is when the top view is completely hidden, our inner sc should be able to slide up and down, but if the sticky navigation slides up and down, it should be caught again. You need to slide the top view.
Finally the analysis is complete, look at the code, this is called sour:
okay, go decide the two cases above.
Note: If you want to know how ListView can replace the ScrollView mentioned above, please note that ListView support has been added to the source code, so you can download it and view it yourself.
Step 4: Creating the MainActivity.java file
After that in our MainActivity, start ViewPager. There is no complicated code, the main thing to start our Vp as shown below;
public class MainActivity extends FragmentActivity { | |
private String[] mTitles = new String[] { "Tab 1", "Tab 2", "Tab 3" }; | |
private SimpleViewPagerIndicator mIndicator; | |
private ViewPager mViewPager; | |
private FragmentPagerAdapter mAdapter; | |
private TabFragment[] mFragments = new TabFragment[mTitles.length]; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initViews(); | |
initDatas(); | |
initEvents(); | |
} |
By the way, I wrote this guide for a while, and it can be considered a cultural control. Especially going with Vp. Please see the detailed typing method: Android teaches you to build a cool ViewPagerIndicator. Not only is MIUI a top impersonation, but the triangle change becomes underscore. Therefore, it’s not the focus of this article, you can ignore it for a while.
Each page in our Vp is a Fragment, we will not send a Fragment code. The layout is ScrollView as root layout, and the interior is filled with customization. For details, please refer to the source code.
After the introduction of the application, I feel a little happier. Basically write the composition according to the procedure, and the result is seen automatically.
Eventually, MainActivity.java will be like this.
package com.quick.lib; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.support.v4.view.ViewPager.OnPageChangeListener; | |
public class MainActivity extends FragmentActivity { | |
private String[] mTitles = new String[] { "Tab 1", "Tab 2", "Tab 3" }; | |
private SimpleViewPagerIndicator mIndicator; | |
private ViewPager mViewPager; | |
private FragmentPagerAdapter mAdapter; | |
private TabFragment[] mFragments = new TabFragment[mTitles.length]; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initViews(); | |
initDatas(); | |
initEvents(); | |
} | |
private void initEvents() { | |
mViewPager.setOnPageChangeListener(new OnPageChangeListener() { | |
@Override | |
public void onPageSelected(int position) { | |
} | |
@Override | |
public void onPageScrolled(int position, float positionOffset, | |
int positionOffsetPixels) { | |
mIndicator.scroll(position, positionOffset); | |
} | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
} | |
}); | |
} | |
private void initDatas() { | |
mIndicator.setTitles(mTitles); | |
for (int i = 0; i < mTitles.length; i++) { | |
mFragments[i] = (TabFragment) TabFragment.newInstance(mTitles[i]); | |
} | |
mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) { | |
@Override | |
public int getCount() { | |
return mTitles.length; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return mFragments[position]; | |
} | |
}; | |
mViewPager.setAdapter(mAdapter); | |
mViewPager.setCurrentItem(0); | |
} | |
private void initViews() { | |
mIndicator = (SimpleViewPagerIndicator) findViewById(R.id.id_stickynavlayout_indicator); | |
mViewPager = (ViewPager) findViewById(R.id.id_stickynavlayout_viewpager); | |
} | |
} |
Future Directions
Finally, future indicators will include showing sticky navigation structures from different functions. Different capabilities can be enabled in the app by enabling the use of different adhesive properties.
Learning Strategies and Tools
Sticky navigation layout has been made possible with the use of Android studio and Java experience. Finally, we learned how your app can use the sticky navigation layout on Android. Tasks now have to use different functions in their proper roles to implement sticky navigation layout on android. I used the android documentation to learn more about layout fundamentals. The page provides an overview of the Layout Editor.
Reflective Analysis
Finding out how to use ScrollView administrators on Android was a great learning experience. An amazing tool that makes ScrollView comprehension areas easier. Convert ScrollView as an external layer to a custom controller, acquired as a LinearLayout, called StickyNavLayout. Android apps.
In conclusion, I spent 48 hours completing the project and the blog. Finally, check out the source code here in the GitHub repository.
Link to previous posts: https://blog.ldtalentwork.com/2020/09/16/how-to-check-internet-connection-programatically-on-android-from-a-button-click-in-kotlin
That’s all about this lesson!
This a very educative blog Dennis. Thanks for your efforts.
Have seen sticky navigation views in web applications but this has also given me a chance to learn the same in android. I would also suggest including the sticky navigation on the header instead, to act like a sticky menu when a user scrolls on the application.
Also to all new learners, have noticed a problem with android studio projects loading gradle files if a project is has a different gradle version as compared to your version. To solve this. After cloning a project be sure to change this two things to match your version before loading;
In build.gradle;
classpath “com.android.tools.build:gradle:4.0.1”
In gradle wrapper properties;
services.gradle.org/distributions/gradle-6.1.1-all.zip