RecyclerView scroll speed with offset - android

I want to scroll my RecyclerView to a specific position with offset. Now I am using scrollToPositionWithOffset(position, offset) and this method works fine except the scroll happens too fast and I cant control the speed of the scroll. I've tried using smoothScrollToPosition() but I need the offset. Can someone tell me how can I control the speed of scrolling when using scrollToPositionWithOffset()?

If you need to control fling speed , you need to implement your own recyclerview implementaion .
public class CustomRecyclerView extends RecyclerView {
Context context;
public CustomRecyclerView(Context context) {
super(context);
this.context = context;
}
public CustomRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean fling(int velocityX, int velocityY) {
velocityY *= 0.7;
// velocityX *= 0.7; for Horizontal recycler view. comment velocityY line not require for Horizontal Mode.
return super.fling(velocityX, velocityY);
}
}

put your recyclerview in nestedScrollview it will scroll smoothly.

Related

How to make the RecyclerView forbid Scroll

In some cases, we need to make the RecyclerView cannot scroll, so play to your strengths for this questions.
I have some solutions for this problem, hope it can help you.See below for details:
void setLayoutFrozen (boolean frozen);
I strongly recommend if we only want to inhabit scroll for this scheme for its simplicity and convenience; if you want to know deeply
When we use LayoutManager(LinearManager or GridLayoutManager), we can use this.
`
LinearLayoutManager linearLayoutManager = new
LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false){
#Override
public boolean canScrollVertically() {
return false;
}
#Override
public boolean canScrollHorizontally() {
return super.canScrollHorizontally();
}
};`
GridLayoutManager is alike.
From above second item, we can extend corresponding LayoutManager.
`
public class MyGridLayoutManager extends GridLayoutManager {
private boolean isScrollEnabled = true;
public MyGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public MyGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public MyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
#Override
public boolean canScrollVertically() {
return isScrollEnabled && super.canScrollVertically();
}
}
`
4. We can handle the RecyclerView TouchEvent to achieve the effect.
From above, I believe you can make yourself. Good Luck...

How to disable the overscroll in scrollview but only in the top for android?

I know the we can turn off the overscroll via overscroll mode. But how do i control it in a way the that bottom has the overscroll effect while the top doesn't has the overscroll effect in a scroll view?
There are one method in ScrollView class called getBottomFadingEdgeStrength() so you can create a Class that extends ScrollView and override the above method. like
/**
* Created by droidwithme on 23/05/16.
*/
public class MyScrollView extends ScrollView {
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected float getBottomFadingEdgeStrength() {
return 0.0f;
}
}
and return a 0 to this method. this may help you.

Effective use of touch/hover co-ordinates on a View

On Android phone, I get the information of co-ordinates that, user touch on his/her phone screen or device. How do i then, use/pass these X-Y coordinates to run a specific function of mine, and use it in my code
reference from the post you can get the co-ordinates if the user touched the screen on your app. After you implement that on touch listener then in the if statement MotionEvent.ACTION_UP put this there
float myX = event.getX();
float myY = event.getY();
// now you can then pass myX,myY in your method or function as parameters
if you happen to be targeting Api 14+ then you can use onhover with the same approach as to the first one from that post, instead of using setOnTouchListener usegetWindow().getDecorView().setOnHoverListener() that's about it.
A side note is using the firs one is better because onHover can not be trusted to work,actually it doesn't work to me, thats just btw
hope i answered your question.
You can create custom view by extending android view and overriding onTouchEvent method. For example,
public class NotTouchableWebView extends WebView {
private Context context;
public NotTouchableWebView(Context context) {
super(context);
this.context=context;
}
public NotTouchableWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
}
public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context=context;
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context=context;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (some condition){
//your specific function
return false;
}
return super.onTouchEvent(event);
}
}

Find height of parent view

I am creating a custom imageview and I am trying to find the height of the parent. The only detail I know about the parent is that it would potentially scroll. The reason I need the parent's height is because I am trying to figure out the imageview's position on the screen. I have made an equation that works for accurately calculating its position, but it only works when I manually enter in the parents height. Is there any way to retrieve this information or is there another way to get my imageview's position on the screen every time it changes?
Try this way
public class MyImageView extends ImageView {
int parentHeight;
int parentWidth;
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyImageView(Context context) {
super(context);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(((View)this.getParent()).getMeasuredWidth()!=0){
parentHeight = ((View)this.getParent()).getMeasuredHeight();
parentWidth = ((View)this.getParent()).getMeasuredWidth();
}
}
}

Scroll offset of GridView

Is there a way to determine the current scroll offset or scroll position of a GridView?
View.getScrollY() // Underlying var is not updated during a scroll.
I have tried setting an OnScrollListener but the onScroll callback is not fine grained enough for my purposes.
Here is the how I'm attempting to determine the scroll offset using an OnScrollListener.
private int getScrollY() {
int top = 0;
if (mGridView.getChildCount() > 0) {
final View firstView = mGridView.getChildAt(0);
top = firstView.getTop();
}
return top;
}
The issue with this code is that the returned y offset is inaccurate when scrolling upwards; the top view is recycled and hence, the y offset seems to jump;
Is there a nice way of calculating the scroll offset of a GridView? I can't seem to find a good solution.
Use this.
public class CustomGridView extends GridView {
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/* ADD THIS */
#Override
public int computeVerticalScrollOffset() {
return super.computeVerticalScrollOffset();
}
}
Returns an int value when called
You can use GridView.getFirstVisiblePosition().
http://developer.android.com/reference/android/widget/AdapterView.html#getFirstVisiblePosition()

Categories

Resources