In my activity I have a BottomNavigationView and a RecyclerView, and what I want to do is to hide the BottomNavigationView when I scroll the view, I'm using this code:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
BottomNavigationView navigationView = requireActivity().findViewById(R.id.bottom_navigation_view);
if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING && dy > 0){
navigationView.animate()
.translationY(navigationView.getHeight());
}
else if(recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING && dy < 0)
navigationView.animate()
.translationY(0);
}
});
This works fine except for the fact that it still shows the bounds of the view, so after hiding the BottomNavigationView I get an empty rectangle. What is the best way to make the height of the view follow the translation?
I don't see how your "hiding" BottomNavigationView
but if your using
BottomNavigationView.setVisibility(View.INVISIBLE);
which hides the view but keeps its width and height.
it needs to be
BottomNavigationView.setVisibility(View.GONE);
which hides the view and makes the width = 0 and height = 0
Related
If I load more than 10 items it works fine. e.g. I select 25 or 50 limits from spinner pagination works fine, but when I select limit 10, then scrolling is not detected inside onScrolled of addOnScrollListener method.
Or if I load the amount of elements that cover the screen, then we need to scroll further items of recyclerView.
RecyclerView creation method
VulnerabilityScanAdapter vulnerabilityScanAdapter = new VulnerabilityScanAdapter(
getActivity(), callback, vulnerabilityScanPojoList
);
vulnerabilityScanAdapter.notifyDataSetChanged();
final LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext(),
LinearLayoutManager.VERTICAL, false);
vulnerabilityScanRv.setLayoutManager(mLayoutManager);
vulnerabilityScanRv.setItemAnimator(new DefaultItemAnimator());
vulnerabilityScanRv.setAdapter(vulnerabilityScanAdapter);
// This is the code for pagination of this recyclerView
vulnerabilityScanRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy > 0){
// Scrolling Down
if (totalCount > currentPageCount) {
LinearLayoutManager linearLayoutManager =
(LinearLayoutManager) recyclerView.getLayoutManager();
if (linearLayoutManager != null &&
linearLayoutManager.findLastCompletelyVisibleItemPosition() ==
vulnerabilityScanPojoList.size() - 1) {
//bottom of list!
loadMore();
}
} else {
//showSnackForAttributes(true,"No more data avaiable.");
}
}else if(dy < 0){
// Scrolling Up
}
}
});
I have done debugging to know whether the control is going inside onScrolled method while scrolling or not. It goes inside onScrollStateChanged when it reaches to the bottom of the list.
Currently, if the 2nd item in the recyclerview is not visible to the user, a specific layout will be hidden/gone.
So this is my current code:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, final int dy) { final float test = dy;
if (layoutManager.findFirstCompletelyVisibleItemPosition() > 2) {
if(mAccountLayout.isShown() && mDateLayout.isShown()) {
mAccountLayout.setVisibility(View.GONE);
mDateLayout.setVisibility(View.GONE);
Log.d("SCROLLINGDOWN", "SCROLL");
Log.d("SCROLLdown",""+dy);
}
} else {
if(!mAccountLayout.isShown() && !mDateLayout.isShown()) {
mAccountLayout.setVisibility(View.VISIBLE);
mDateLayout.setVisibility(View.VISIBLE);
Log.d("SCROLLINGUP", "SCROLL");
Log.d("SCROLLUP",""+dy);
}
}
}
});
What i want is to have atleast an animation when the layout is being hidden/gone. Would likely similar to this: Sample animation without using coordinator layout.
If the user scroll slowly down, then the layout should be also slowly be hidden until the 2nd item in the recyclerview is no longer visible.
I create parallax effect moving the image background depends on location of first element of recycler view only.
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if ((holder = recyclerView.findViewHolderForAdapterPosition(0)) != null) {
int offset = recyclerView.findViewHolderForAdapterPosition(0).itemView.getTop() / 10;
backgroundPhoto.setTop(offset);
}
The problem is : when the first item of recycler(header) scroll off the screen, background picture somehow jump ot initial position.
Once the view is scrolled off the screen the RecyclerView might still be able to access it but it's getTop() value will have some random value or 0 which will cause your parallax effect to jump.
You could keep a field in your class which saves the currently "scrolled distance" and add dx to it in the onScrolled(...) callback and use this value to calculate the parallax offset.
Iphone App video link
How I can design and develop view which is posted in above video? This is basically the item expansion of recyclerview with animation. I have tried with onItemtouchlistener of recyclerview and also with some custom view with animation, but didn't get the accurate result.
Finally i came accross addonscrolllistener, this give me results but not accurate.
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if(newState == RecyclerView.FOCUS_UP) {
System.out.println("hello, ia m going up");
}
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0){
TextView tv = (TextView)recyclerView.findViewById(R.id.title);
//tv.setVisibility(View.VISIBLE);
if (tv.getVisibility()==View.VISIBLE){
System.out.println("yes");
}else {
slideToTop(tv);
}
}
}
});
private void slideToTop(View view){
TranslateAnimation animate = new TranslateAnimation(0,0,0,-view.getHeight());
animate.setDuration(1000);
animate.setFillAfter(false);
view.startAnimation(animate);
view.setVisibility(View.VISIBLE);
}
I think your question is too broad - however, here is some psuedocode:
onScrolled() {
View child = getSecondVisibleChild();
int distanceFromTop = child.getTop();
int distanceAtWhichExpandingShouldOccur = 100;
if(distanceFromTop < distanceAtWhichExpandingShouldOccur ) {
child.setHeight(child.getOriginalHeight() + (distanceFromTop - distanceAtWhichExpandingShouldOccur))
}
}
So you'll notice, the second visible child is the one who's height changes. It changed when it's less than distanceAtWhichExpandingShouldOccur from the top of the window. Its height changes to ensure its bottom remains stationary - therefore its height is increasing at the same pace its top is moving.
Once it's no longer the second visible child (aka, its top is 0), it should be scrolled off as normal and the next child should have its height changed when its top is less than distanceAtWhichExpandingShouldOccur.
This library can be a study case for you: https://github.com/florent37/MaterialLeanBack
Hi , I have a layout with Toolbar, PageSlidingTab and ViewPager. Inside ViewPager there is a fragment with RecyclerView. I want to hide the Toolbar as i scroll the RecyclerView. I have achieved it by adding the following code :
toolbar = ((MyActivity)getActivity()).getToolbar();
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
int toolbarMarginOffset = 0;
private int dp(int inPixels){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, inPixels, getActivity().getResources().getDisplayMetrics());
}
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
toolbarMarginOffset += dy;
if(toolbarMarginOffset>dp(56)){
toolbarMarginOffset = dp(56);
}
if(toolbarMarginOffset<0){
toolbarMarginOffset = 0;
}
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)toolbar.getLayoutParams();
params.topMargin = -1*toolbarMarginOffset;
toolbar.setLayoutParams(params);
}
});
It works fine as expected but while scrolling there is a flicker when the toolbar is hiding (As shown in image). I know its happening because of Layout resize. How can i fix this issue? Please suggest a good solution to this.
I had the same problem. I solved the problem using this library Android-ObservableScrollView