I am using Room data base to fetch the data from the data base. Recyclerview I want to scroll to a particular position in recyclerview.
I have already tried list.setNestedScrollingEnabled(true)
StationListRepository stationListRepositoryOnlyActive = new StationListRepository(getApplicationContext());
stationListRepositoryOnlyActive.getActiveStationList().observe(this, new Observer>() {
#Override
public void onChanged(List OnlyActivestations) {
Log.d("OnlyActivestations", "" + OnlyActivestations.size());
assert OnlyActivestations != null;
if (OnlyActivestations.size() > 0 || FinalList.size() > 0) {
if (FinalList != null && FinalList.size() > 0) {
OnlyActivestations = sortList(OnlyActivestations);
FinalList.addAll(OnlyActivestations);
} else {
OnlyActivestations = sortList(OnlyActivestations);
FinalList = OnlyActivestations;
}
Log.d("FinalListSizeActive",""+FinalList.size());
detailsAdapter = new db_stationListAdapter(context, FinalList);
//recyclerView.setNestedScrollingEnabled(true);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
staggeredGridLayoutManager.scrollToPosition(10);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.setAdapter(detailsAdapter);
detailsAdapter.notifyDataSetChanged();
runOnUiThread(new Runnable() {
#Override
public void run() {
// stopLoading();
stopAnimation();
}
});
}
}
});
I except the recycler view to auto scroll to the position 10
Try using the code below after you set the data to the adapter
recyclerView.smoothScrollToPosition(10);
or
You can add OnLayoutChangeListener to your RecyclerView.
recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){
#Override
public void onLayoutChange(View v, int left, int top, int
right, int bottom, int oldLeft, int oldTop, int oldRight,
int oldBottom) {
if (bottom < oldBottom) {
recyclerView.postDelayed(new Runnable() {
#Override
public void run() {
recyclerView.smoothScrollToPosition(10);
}
}, 100);
}
}
});
My requirement is scrolling a ImageView left and right on top of a list of data in the Recyclerview. When Recyclerview is scrolled up the ImageView will move towards right until list reaches to the end and vice versa.
I have tried but facing few complexity any suggestions and reference will be very helpful.Or any logic will also be appreciated.
The problem what I facing now is that not able to handle the values in the addOnScrollListener
Sharing my code in the github: Github Link
MainActivity Code:
Sharing few lines of code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ClickEventLisener {
HorizontalScrollView v_scroll;
Button btn_left, btn_right;
int pos = 0;
int temppos = 0;
int initialpos = 0;
RecyclerView rv_recycler;
UserApapter adapter;
List<Users> user = new ArrayList<>();
Context mContext;
boolean isFirsttime = true;
ImageView iv_image;
String imageurl = "https://www.isometrix.com/wp-content/uploads/2017/03/featured-images-about.jpg";
int screenwidth = 0, imagewidth = 0;
int scroolby_val = 0;
#SuppressLint("ClickableViewAccessibility")
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
iv_image = findViewById(R.id.iv_image);
v_scroll = findViewById(R.id.v_scroll);
btn_left = findViewById(R.id.btn_left);
btn_right = findViewById(R.id.btn_right);
rv_recycler = findViewById(R.id.rv_recycler);
btn_left.setOnClickListener(this);
btn_right.setOnClickListener(this);
v_scroll.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
filllist();
getScreenSize();
setupimage();
getleftscroll();
// when scrolled to the leftmost position the variables are initialized
v_scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
initialpos = i;
if (i == 0) {
temppos = 0;
} else {
}
}
});
///not able to understand how to handle the values
//handling the scroll
rv_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
getleftscroll();
if (dy > 0) {
temppos = temppos + scroolby_val;
v_scroll.scrollTo(temppos, 0);
Log.d("Scroll", "ScrollUp");
Log.d("temppos", "" + temppos);
} else {
if (initialpos != 0) {
temppos = temppos - scroolby_val;
v_scroll.scrollTo(temppos, 0);
Log.d("temppos1", "" + temppos);
Log.d("Scroll", "ScrollDown");
}
}
}
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
// Do something
} else if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
// Do something
} else {
// Do something
}
}
});
}
private void getScreenSize() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Log.d("ScreenWidth", "" + width);
screenwidth = width;
}
private void setupimage() {
Glide.with(mContext)
.asBitmap()
.load(imageurl)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Log.d("ImageWitdh", "" + w);
iv_image.setImageBitmap(bitmap);
imagewidth = w;
}
});
}
/*
Logic I have implemented:calculated Imagewidth and screenwidth. Then minus it (imagewidth > screenwidth) and then divide it by the number of values in the array.
if the value is 0 then scrolling it by 1 per scroll or if the returned value is 3 then scrolling it by 3.
IOS team have done this way and working good for them but I am facing a minor issue here.
*/
private void getleftscroll() {
imagewidth = 2000;
if (imagewidth > screenwidth) {
int leftoutscreen = imagewidth - screenwidth;
scroolby_val = leftoutscreen / user.size();
if (scroolby_val == 0) {
scroolby_val = 1;
}
// temppos = 0;
Log.d("scroolby_val", "" + scroolby_val);
}
}
///button is just for demo purpose///
#Override
public void onClick(View v) {
if (v == btn_left) {
if (initialpos != 0) {
temppos = temppos - pos--;
v_scroll.scrollTo(temppos, 0);
Log.d("temppos", "" + temppos);
}
} else if (v == btn_right) {
temppos = temppos + pos++;
v_scroll.scrollTo(temppos, -temppos);
Log.d("temppos", "" + temppos);
}
}
private void filllist() {
user.clear();
for (int i = 0; i <= 300; i++) {
user.add(new Users(String.valueOf(i + 1), "name " + i));
}
inflateadapter();
}
private void inflateadapter() {
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
adapter = new UserApapter(this, R.layout.row_users, user, this);
rv_recycler.setLayoutManager(mLayoutManager);
rv_recycler.setAdapter(adapter);
}
#Override
public void Currentposition(int position) {
Log.d("Currentposition", "" + position);
if (position == user.size() - 1) {
if (scroolby_val == 1) {
Log.d("scroolby_val", "" + scroolby_val);
Log.d("Inside", "Inside");
// v_scroll.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
// temppos = 1;
}
} else if (position == 0) {
temppos = 0;
}
pos = position;
}
#Override
public void ClickEventLisener(View view, int position) {
Toast.makeText(mContext, "" + position, Toast.LENGTH_SHORT).show();
}
}
Few things you may want to consider-
Don't use dy values from onScrolled callback directly to set scroll of v_scroll. There should be a logic to map scroll state of recycler view to the required scroll in v_scroll.
Don't use dy at all. Logic to set scroll of v_scroll should be based on the number of items in the list and the first/last visible child in it. (RecyclerView provides methods to get that.) You can run that logic from onScrolled callback.
I am using a RecyclerView with a custom RecyclerViewOnScrollListener.
I'd like to change the RecyclerView item height when scrolling down and go back to the original height when scrolling up.
I am already animating a bottomBar in the same listener, but I can't understand how to animate every item in RecyclerView. I've tried with a for loop but I don't think is the right idea.
public abstract class RecyclerViewOnScrollListener extends RecyclerView.OnScrollListener {
private LinearLayout bottomBarContainer;
private boolean animateItems = false;
CardStackLayoutManager cardStackLayoutManager;
public RecyclerViewOnScrollListener(LinearLayout bottomBarContainer) {
this.bottomBarContainer = bottomBarContainer;
}
public RecyclerViewOnScrollListener(LinearLayout bottomBarContainer, boolean animateItems, CardStackLayoutManager cardStackLayoutManager) {
this.bottomBarContainer = bottomBarContainer;
this.animateItems = animateItems;
this.cardStackLayoutManager = cardStackLayoutManager;
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (bottomBarContainer==null || dy==0 ) {
return;
}
long ANIMATION_DURATION = 200L;
if (dy>0) { // Scrolling to bottom
if (mIsScrollDirectionLocked && mScrollingDirection!=0) return;
if (bottomBarContainer.getVisibility()== View.GONE || mIsAnimatingOff) {
return;
} else {
for(int i = 0;i < cardStackLayoutManager.getChildCount();i++)
{
View view = cardStackLayoutManager.getChildAt(i);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,"y",0f,200f);
objectAnimator.setDuration(150);
objectAnimator.start();
}
mScrollingDirection = SCROLLING_DOWN;
mIsAnimatingOff = !mIsAnimatingOff;
ViewCompat.setTranslationY(bottomBarContainer, 0F);
ViewCompat.animate(bottomBarContainer)
.translationY(bottomBarContainer.getHeight())
.setDuration(ANIMATION_DURATION)
.setListener(new ViewPropertyAnimatorListenerAdapter() {
#Override
public void onAnimationEnd(View view) {
mIsAnimatingOff = !mIsAnimatingOff;
bottomBarContainer.setVisibility(View.GONE);
}
}).start();
}
} else { // Scrolling to top
if (mIsScrollDirectionLocked && mScrollingDirection!=0) return;
if (bottomBarContainer.getVisibility()!=View.VISIBLE && !mIsAnimatingOn) {
mScrollingDirection = SCROLLING_UP;
mIsAnimatingOn = !mIsAnimatingOn;
bottomBarContainer.setVisibility(View.VISIBLE);
for(int i = 0;i < cardStackLayoutManager.getChildCount();i++)
{
View view = cardStackLayoutManager.getChildAt(i);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,"y",200f,0f);
objectAnimator.setDuration(150);
objectAnimator.start();
}
ViewCompat.setTranslationY(bottomBarContainer, bottomBarContainer.getHeight());
ViewCompat.animate(bottomBarContainer)
.translationY(0F)
.setDuration(ANIMATION_DURATION)
.setListener(new ViewPropertyAnimatorListenerAdapter() {
#Override
public void onAnimationEnd(View view) {
mIsAnimatingOn = !mIsAnimatingOn;
}
}).start();
}
}
}
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!mIsScrollDirectionLocked) return;
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
mScrollingDirection = 0;
break;
default:
break;
}
}
private static final int SCROLLING_UP = 1;
private static final int SCROLLING_DOWN = 2;
private int mScrollingDirection = 0;
private boolean mIsScrollDirectionLocked = false;
private boolean mIsAnimatingOff = false;
private boolean mIsAnimatingOn = false;
}
Define a method to animate your ConvertView like below:
private void setAnimation(View viewToAnimate,int position) {
if (position > lastPosition) {
lastPosition = position;
Animation animation = AnimationUtils.loadAnimation(activity, R.anim.anim_content);
viewToAnimate.startAnimation(animation);
}
}
And in your Adapter's onBindViewHolder method call your animation method:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Do your initialization
setAnimation(holder.convertView, position);
}
Here's the animation: (anime_content.xml)
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="25%p"
android:toYDelta="0%p"
android:duration="300"
android:interpolator="#android:anim/decelerate_interpolator" />
This example will animate your last inflated recycler view row from down to up. You can change your animation how ever you want.
Good Luck!
I try to add a relativelayout when the user begin to scroll down in a listview.
I would like to show the relativelayout again when the user scroll to top and when first item of the listview is displayed.
My code works but there is a little "climb" when the user try to scroll.
I think that the problem is located on listAtTop function but i don't know where.
list.setOnDetectScrollListener(new OnDetectScrollListener() {
#Override
public void onUpScrolling() {
/* do something */
Log.d("INFO", "UPPPPPPPPPPP");
if (listIsAtTop()) {
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
relative1.setVisibility(View.VISIBLE);
}
}
#Override
public void onDownScrolling() {
/* do something */
Log.d("INFO", "DOWNNNNNNNN");
if (!listIsAtTop()) {
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
relative1.setVisibility(View.GONE);
}
}
});
private boolean listIsAtTop() {
if (list.getChildCount() == 0) {
return true;
}
return list.getFirstVisiblePosition() == 0 && (list.getChildCount() == 0 || list.getChildAt(0).getTop() == 0);
}
Try this way
list.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
int mPosition=0;
int mOffset=0;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
int position = list.getFirstVisiblePosition();
View v = list.getChildAt(0);
int offset = (v == null) ? 0 : v.getTop();
if (mPosition < position || (mPosition == position && mOffset < offset)){
// Scrolled up
//search_layout.setVisibility(View.GONE);
} else {
// Scrolled down
//search_layout.setVisibility(View.VISIBLE);
}
}
});
In this code when you scroll down hide layout and when you scroll up and reached to top then Visible layout
I have a ScrollView which holds a series of Views. I would like to be able to determine if a view is currently visible (if any part of it is currently displayed by the ScrollView). I would expect the below code to do this, surprisingly it does not:
Rect bounds = new Rect();
view.getDrawingRect(bounds);
Rect scrollBounds = new Rect(scroll.getScrollX(), scroll.getScrollY(),
scroll.getScrollX() + scroll.getWidth(), scroll.getScrollY() + scroll.getHeight());
if(Rect.intersects(scrollBounds, bounds))
{
//is visible
}
This works:
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
// Any portion of the imageView, even a single pixel, is within the visible window
} else {
// NONE of the imageView is within the visible window
}
Use View#getHitRect instead of View#getDrawingRect on the view you're testing. You can use View#getDrawingRect on the ScrollView instead of calculating explicitly.
Code from View#getDrawingRect:
public void getDrawingRect(Rect outRect) {
outRect.left = mScrollX;
outRect.top = mScrollY;
outRect.right = mScrollX + (mRight - mLeft);
outRect.bottom = mScrollY + (mBottom - mTop);
}
Code from View#getHitRect:
public void getHitRect(Rect outRect) {
outRect.set(mLeft, mTop, mRight, mBottom);
}
If you want to detect that the view is FULLY visible:
private boolean isViewVisible(View view) {
Rect scrollBounds = new Rect();
mScrollView.getDrawingRect(scrollBounds);
float top = view.getY();
float bottom = top + view.getHeight();
if (scrollBounds.top < top && scrollBounds.bottom > bottom) {
return true;
} else {
return false;
}
}
This extension help detect view fully visible.
It also work if your View is a child of child of ... of ScrollView (eg: ScrollView -> LinearLayout -> ContraintLayout -> ... -> YourView).
fun ScrollView.isViewVisible(view: View): Boolean {
val scrollBounds = Rect()
this.getDrawingRect(scrollBounds)
var top = 0f
var temp = view
while (temp !is ScrollView){
top += (temp).y
temp = temp.parent as View
}
val bottom = top + view.height
return scrollBounds.top < top && scrollBounds.bottom > bottom
}
Note
1) view.getY() and view.getX() return the x,y value to FIRST PARENT.
2) Here is example about how getDrawingRect will return
Link
My Solution is use NestedScrollView Scroll element:
final Rect scrollBounds = new Rect();
scroller.getHitRect(scrollBounds);
scroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (myBtn1 != null) {
if (myBtn1.getLocalVisibleRect(scrollBounds)) {
if (!myBtn1.getLocalVisibleRect(scrollBounds)
|| scrollBounds.height() < myBtn1.getHeight()) {
Log.i(TAG, "BTN APPEAR PARCIALY");
} else {
Log.i(TAG, "BTN APPEAR FULLY!!!");
}
} else {
Log.i(TAG, "No");
}
}
}
});
}
To expand a bit on Bill Mote's answer using getLocalVisibleRect, you may want to check if the view is only partially visible:
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (!imageView.getLocalVisibleRect(scrollBounds)
|| scrollBounds.height() < imageView.getHeight()) {
// imageView is not within or only partially within the visible window
} else {
// imageView is completely visible
}
public static int getVisiblePercent(View v) {
if (v.isShown()) {
Rect r = new Rect();
v.getGlobalVisibleRect(r);
double sVisible = r.width() * r.height();
double sTotal = v.getWidth() * v.getHeight();
return (int) (100 * sVisible / sTotal);
} else {
return -1;
}
}
I faced the same problem today. While Googling and reading Android reference I found this post and a method I ended up using instead;
public final boolean getLocalVisibleRect (Rect r)
Nice of them not to only providing Rect but also boolean indicating if View visible at all. On negative side this method is undocumented :(
I you want to detect if your View is fully visible, try with this method:
private boolean isViewVisible(View view) {
Rect scrollBounds = new Rect();
mScrollView.getDrawingRect(scrollBounds);
float top = view.getY();
float bottom = top + view.getHeight();
if (scrollBounds.top < top && scrollBounds.bottom > bottom) {
return true; //View is visible.
} else {
return false; //View is NOT visible.
}
}
Strictly speaking you can get the visibility of a view with:
if (myView.getVisibility() == View.VISIBLE) {
//VISIBLE
} else {
//INVISIBLE
}
The posible constant values of the visibility in a View are:
VISIBLE
This view is visible. Use with setVisibility(int) and android:visibility.
INVISIBLE
This view is invisible, but it still takes up space for layout purposes. Use with setVisibility(int) and android:visibility.
GONE
This view is invisible, and it doesn't take any space for layout purposes. Use with setVisibility(int) and android:visibility.
Kotlin way;
An extension for listing scroll view's scroll and get an action if child view visible on screen.
#SuppressLint("ClickableViewAccessibility")
fun View.setChildViewOnScreenListener(view: View, action: () -> Unit) {
val visibleScreen = Rect()
this.setOnTouchListener { _, motionEvent ->
if (motionEvent.action == MotionEvent.ACTION_MOVE) {
this.getDrawingRect(visibleScreen)
if (view.getLocalVisibleRect(visibleScreen)) {
action()
}
}
false
}
}
Use this extension function for any scrollable view
nestedScrollView.setChildViewOnScreenListener(childView) {
action()
}
You can use the FocusAwareScrollView which notifies when view becomes visible :
FocusAwareScrollView focusAwareScrollView = (FocusAwareScrollView) findViewById(R.id.focusAwareScrollView);
if (focusAwareScrollView != null) {
ArrayList<View> viewList = new ArrayList<>();
viewList.add(yourView1);
viewList.add(yourView2);
focusAwareScrollView.registerViewSeenCallBack(viewList, new FocusAwareScrollView.OnViewSeenListener() {
#Override
public void onViewSeen(View v, int percentageScrolled) {
if (v == yourView1) {
// user have seen view1
} else if (v == yourView2) {
// user have seen view2
}
}
});
}
Here is class :
import android.content.Context;
import android.graphics.Rect;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class FocusAwareScrollView extends NestedScrollView {
private List<OnScrollViewListener> onScrollViewListeners = new ArrayList<>();
public FocusAwareScrollView(Context context) {
super(context);
}
public FocusAwareScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FocusAwareScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public interface OnScrollViewListener {
void onScrollChanged(FocusAwareScrollView v, int l, int t, int oldl, int oldt);
}
public interface OnViewSeenListener {
void onViewSeen(View v, int percentageScrolled);
}
public void addOnScrollListener(OnScrollViewListener l) {
onScrollViewListeners.add(l);
}
public void removeOnScrollListener(OnScrollViewListener l) {
onScrollViewListeners.remove(l);
}
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
for (int i = onScrollViewListeners.size() - 1; i >= 0; i--) {
onScrollViewListeners.get(i).onScrollChanged(this, l, t, oldl, oldt);
}
super.onScrollChanged(l, t, oldl, oldt);
}
#Override
public void requestChildFocus(View child, View focused) {
super.requestChildFocus(child, focused);
}
private boolean handleViewSeenEvent(View view, int scrollBoundsBottom, int scrollYOffset,
float minSeenPercentage, OnViewSeenListener onViewSeenListener) {
int loc[] = new int[2];
view.getLocationOnScreen(loc);
int viewBottomPos = loc[1] - scrollYOffset + (int) (minSeenPercentage / 100 * view.getMeasuredHeight());
if (viewBottomPos <= scrollBoundsBottom) {
int scrollViewHeight = this.getChildAt(0).getHeight();
int viewPosition = this.getScrollY() + view.getScrollY() + view.getHeight();
int percentageSeen = (int) ((double) viewPosition / scrollViewHeight * 100);
onViewSeenListener.onViewSeen(view, percentageSeen);
return true;
}
return false;
}
public void registerViewSeenCallBack(final ArrayList<View> views, final OnViewSeenListener onViewSeenListener) {
final boolean[] viewSeen = new boolean[views.size()];
FocusAwareScrollView.this.postDelayed(new Runnable() {
#Override
public void run() {
final Rect scrollBounds = new Rect();
FocusAwareScrollView.this.getHitRect(scrollBounds);
final int loc[] = new int[2];
FocusAwareScrollView.this.getLocationOnScreen(loc);
FocusAwareScrollView.this.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
boolean allViewsSeen = true;
#Override
public void onScrollChange(NestedScrollView v, int x, int y, int oldx, int oldy) {
for (int index = 0; index < views.size(); index++) {
//Change this to adjust criteria
float viewSeenPercent = 1;
if (!viewSeen[index])
viewSeen[index] = handleViewSeenEvent(views.get(index), scrollBounds.bottom, loc[1], viewSeenPercent, onViewSeenListener);
if (!viewSeen[index])
allViewsSeen = false;
}
//Remove this if you want continuous callbacks
if (allViewsSeen)
FocusAwareScrollView.this.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) null);
}
});
}
}, 500);
}
}
I ended up implementing a combination of two of the Java answers ( #bill-mote https://stackoverflow.com/a/12428154/3686125 and #denys-vasylenko https://stackoverflow.com/a/25528434/3686125 ) in my project as a set of Kotlin extensions, which support either standard vertial ScrollView or HorizontalScrollView controls.
I just tossed these in a Kotlin file named Extensions.kt, no class, just methods.
I used these to determine which item to snap to when a user stops scrolling in various scrollviews in my project:
fun View.isPartiallyOrFullyVisible(horizontalScrollView: HorizontalScrollView) : Boolean {
val scrollBounds = Rect()
horizontalScrollView.getHitRect(scrollBounds)
return getLocalVisibleRect(scrollBounds)
}
fun View.isPartiallyOrFullyVisible(scrollView: ScrollView) : Boolean {
val scrollBounds = Rect()
scrollView.getHitRect(scrollBounds)
return getLocalVisibleRect(scrollBounds)
}
fun View.isFullyVisible(horizontalScrollView: HorizontalScrollView) : Boolean {
val scrollBounds = Rect()
horizontalScrollView.getDrawingRect(scrollBounds)
val left = x
val right = left + width
return scrollBounds.left < left && scrollBounds.right > right
}
fun View.isFullyVisible(scrollView: ScrollView) : Boolean {
val scrollBounds = Rect()
scrollView.getDrawingRect(scrollBounds)
val top = y
val bottom = top + height
return scrollBounds.top < top && scrollBounds.bottom > bottom
}
fun View.isPartiallyVisible(horizontalScrollView: HorizontalScrollView) : Boolean = isPartiallyOrFullyVisible(horizontalScrollView) && !isFullyVisible(horizontalScrollView)
fun View.isPartiallyVisible(scrollView: ScrollView) : Boolean = isPartiallyOrFullyVisible(scrollView) && !isFullyVisible(scrollView)
Example usage, iterating through scrollview's LinearLayout children and logging outputs:
val linearLayoutChild: LinearLayout = getChildAt(0) as LinearLayout
val scrollView = findViewById(R.id.scroll_view) //Replace with your scrollview control or synthetic accessor
for (i in 0 until linearLayoutChild.childCount) {
with (linearLayoutChild.getChildAt(i)) {
Log.d("ScrollView", "child$i left=$left width=$width isPartiallyOrFullyVisible=${isPartiallyOrFullyVisible(scrollView)} isFullyVisible=${isFullyVisible(scrollView)} isPartiallyVisible=${isPartiallyVisible(scrollView)}")
}
}
My way:
scrollView.viewTreeObserver?.addOnScrollChangedListener {
scrollView.getDrawingRect(Rect())
myViewInsideScrollView.getLocalVisibleRect(Rect())
}
I know its very late. But i have a good solution. Below is the code snippet for getting view visibility percentage in scroll view.
First of all set touch listener on scroll view for getting callback for scroll stop.
#Override
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction( ) ) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(mScrollView == null){
mScrollView = (ScrollView) findViewById(R.id.mScrollView);
}
int childCount = scrollViewRootChild.getChildCount();
//Scroll view location on screen
int[] scrollViewLocation = {0,0};
mScrollView.getLocationOnScreen(scrollViewLocation);
//Scroll view height
int scrollViewHeight = mScrollView.getHeight();
for (int i = 0; i < childCount; i++){
View child = scrollViewRootChild.getChildAt(i);
if(child != null && child.getVisibility() == View.VISIBLE){
int[] viewLocation = new int[2];
child.getLocationOnScreen(viewLocation);
int viewHeight = child.getHeight();
getViewVisibilityOnScrollStopped(scrollViewLocation, scrollViewHeight,
viewLocation, viewHeight, (String) child.getTag(), (childCount - (i+1)));
}
}
}
}, 150);
break;
}
return false;
}
In above code snippet, we are getting call backs for scroll view touch events and post a runnable after 150 millis(Not mandatory) after getting the callback for scroll stopped. In that runnable we will get location of scroll view on the screen and scroll view height. Then get the direct child viewgroup instance of scroll view and get the child counts. In my case direct child of scroll view is LinearLayout named scrollViewRootChild. Then iterate all the child views of scrollViewRootChild. In above code snippet you can see I am getting the location of the child on the screen in a integer array named viewLocation, get height of view in variable name viewHeight. Then i called a private method getViewVisibilityOnScrollStopped. You can get the understanding of the internal working of this method by reading documentation.
/**
* getViewVisibilityOnScrollStopped
* #param scrollViewLocation location of scroll view on screen
* #param scrollViewHeight height of scroll view
* #param viewLocation location of view on screen, you can use the method of view claas's getLocationOnScreen method.
* #param viewHeight height of view
* #param tag tag on view
* #param childPending number of views pending for iteration.
*/
void getViewVisibilityOnScrollStopped(int[] scrollViewLocation, int scrollViewHeight, int[] viewLocation, int viewHeight, String tag, int childPending) {
float visiblePercent = 0f;
int viewBottom = viewHeight + viewLocation[1]; //Get the bottom of view.
if(viewLocation[1] >= scrollViewLocation[1]) { //if view's top is inside the scroll view.
visiblePercent = 100;
int scrollBottom = scrollViewHeight + scrollViewLocation[1]; //Get the bottom of scroll view
if (viewBottom >= scrollBottom) { //If view's bottom is outside from scroll view
int visiblePart = scrollBottom - viewLocation[1]; //Find the visible part of view by subtracting view's top from scrollview's bottom
visiblePercent = (float) visiblePart / viewHeight * 100;
}
}else{ //if view's top is outside the scroll view.
if(viewBottom > scrollViewLocation[1]){ //if view's bottom is outside the scroll view
int visiblePart = viewBottom - scrollViewLocation[1]; //Find the visible part of view by subtracting scroll view's top from view's bottom
visiblePercent = (float) visiblePart / viewHeight * 100;
}
}
if(visiblePercent > 0f){
visibleWidgets.add(tag); //List of visible view.
}
if(childPending == 0){
//Do after iterating all children.
}
}
If you feel any improvement in this code please contribute.
Using #Qberticus answer which was to the point but great btw, I compined a bunch of codes to check if whenever a scrollview is called and got scrolled it trigger the #Qberticus answer and you can do whatever you want, in my case I have a social network containing videos so when the view is drawed on the screen I play the video same idea like facebook and Instagram.
Here's the code:
mainscrollview.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
#Override
public void onScrollChanged() {
//mainscrollview is my scrollview that have inside it a linearlayout containing many child views.
Rect bounds = new Rect();
for(int xx=1;xx<=postslayoutindex;xx++)
{
//postslayoutindex is the index of how many posts are read.
//postslayoutchild is the main layout for the posts.
if(postslayoutchild[xx]!=null){
postslayoutchild[xx].getHitRect(bounds);
Rect scrollBounds = new Rect();
mainscrollview.getDrawingRect(scrollBounds);
if(Rect.intersects(scrollBounds, bounds))
{
vidPreview[xx].startPlaywithoutstoppping();
//I made my own custom video player using textureview and initialized it globally in the class as an array so I can access it from anywhere.
}
else
{
}
}
}
}
});