Auto Scroll a Android ViewGroup top to bottom and bottom to top - android

Here I'm trying to scroll a GridView automatically top to bottom and as it comes to last item scroll it back to top from bottom using smoothScrollToPosition but its very fast. The code is :
homeGridView.postDelayed(new Runnable() {
public void run() {
// ObjectAnimator.ofInt(homeGridView, "scrollY", homeGridView.getCount()-1).setDuration(1000).start();
homeGridView.smoothScrollToPosition(homeGridView.getCount() - 1);
homeGridView.postDelayed(new Runnable() {
public void run() {
// ObjectAnimator.ofInt(homeGridView, "scrollY", 0).setDuration(1000).start();
homeGridView.smoothScrollToPosition(0);
}
}, 1000);
}
}, 1000);
Now I just want to know that how can I decrease speed of this scroll or something better this.

Related

scrollview move section to top

I am new to Android Programming. I am using the scroll view for rendering a view.
My XML view as follows:
<ScrollView>
<LL1> <TV1> </LL1>
<LL2> <TV2> </LL2>
<LL3> <TV3> </LL3>
<LL4> <TV4> </LL4>
<LL5> <TV5> </LL5>
<LL6> <TV6> </LL6>
</ScrollView>
When I click on TV6 then LL6 layout should move to the top of the screen.
I tried following things but not working properly:
- TV6.getParent().requestChildFocus(TV6,TV6);
- scrollView.smoothScrollTo(0, 0);
Let me know how to achieve this.
Try this one, maybe this will work for you
scrollView.post(new Runnable() {
public void run() {
scrollView.fullScroll(scrollView.FOCUS_DOWN);
} });
or
scrollView.post(new Runnable() {
public void run() {
scrollView.scrollTo(0, scrollView.getBottom());
} });

How do I make an infinitely looping RecyclerView while also being able to scroll to an item programmatically?

I want to make a RecyclerView that scrolls infinitely while also being able to scroll to an item programmatically.
At the moment I've made the RecyclerView loop infinitely using this hacky method: https://stackoverflow.com/a/31254146/7443375
i.e. Overriding my adapter
#Override
public int getCount() {
return Integer.MAX_VALUE;
}
Getting position of item in my adapter like so:
int positionInList = position % fragmentList.size();
And then initializing my RecyclerView's scroll position like so:
recyclerView.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2);
However, in my Fragment that has the RecyclerView, I want to be able to scroll to a specific item in my list (i.e. item 3 out of a list of 10 items). When I call
recyclerView.getLayoutManager().scrollToPosition(2);
The list goes into an infinite scroll. I can't figure out how to go to the specific item itself.
Furthermore, how can I make sure that the specific item is centered in the screen? I am using LinearSnapHelper to snap the items in the center of the screen as I scroll, but LinearSnapHelper does not seem to work when setting positions programmatically.
Try this way..
recyclerView.post(new Runnable() {
#Override
public void run() {
new CountDownTimer(Integer.MAX_VALUE, 20 ) {
public void onTick(long millis) {
recyclerView.scrollBy(0, 20);
}
public void onFinish() {
}
}.start();
}
});

Randomly move ImageButton in a layout every 500ms

i have an image button and i want to move it randomly,
i have no idea how to do that, i also tried other questions but was not able to understand,
i want my image button to move randomly on screen and it should not move out,it should first be compatible with every device.
I also tried it by making buttons in a gridlayout and then individually making them visible and invisible for 1000ms,but it is not the efficient way...
any other way to do so?
public void display(int x){
String q=score.toString();
s.setText(q);
switch (x) {
case 1: {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
b1.setVisibility(View.VISIBLE);
b1.setEnabled(true);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score++;
}
});
}
}, 1000);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
disable();
int x=random();
display(x);
}
}, 2000);
}
Instead of creating multiple buttons, you can use 1 and change its position using setX and setY on click.
For example;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
b1.setX( Enter random number within your screen boundary );
b1.setY( Enter random number within your screen boundary );
}
}, 2000);
You'll need to calculate the boundaries of the screen and take into account that setX and setY position applies to the top left of the button's view.
For API lower than 8 you'll need to use LayoutParams as shown below, or you can also refer codes here (How can I dynamically set the position of view in Android?)
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //WRAP_CONTENT param can be FILL_PARENT
params.leftMargin = 206; //XCOORD
params.topMargin = 206; //YCOORD
childView.setLayoutParams(params);

Android setScroll position

I want to set scrollbar position becomes view visible.
I use
mView.setScrollY(150);
mView.setVisbility(View.VISIBLE);
But it's not worked. View stay visible but scroll position is 0.
Also i try to use a post :
mView.post(new Runnable(){
#Override
public void run() {
mView.setScrollY(150);
mView.setVisbility(View.VISIBLE);
}
});
But now view was blinked( i see a start scrolly - 0 and after such ms(blink) scrollY becomes 150);
Any Ideas?

Android smoothScrollToPosition up top

I have set up smoothScrollToPosition on a listview and it works perfect! However... I would rather it put the position at the top of the listview rather than just on the screen (mostly on the bottom if its needed to scroll down). Any ideas how i can accomplish this?
Use smoothScrollToPositionFromTop(). It places the item at the specified position a specified number of pixels from the top of the ListView (if possible). You should simply supply a small offset.
Note: requires API level 11+.
recycleviewlist.post(new Runnable() {
#Override
public void run() {
new CountDownTimer(totalScrollTime, scrollPeriod) {
public void onTick(long millisUntilFinished) {
recycleviewlist.smoothScrollBy( widthToScroll,0);
}
public void onFinish() {
// gridlayoutmanager
// recycleviewlist.scrollToPosition(0);
gridlayoutmanager1
.scrollToPosition(Integer.MAX_VALUE / 2);
}
}.start();
}
});

Categories

Resources