Collapsing element view while scrolling a listview (Android) - android

I am trying to implement sort of collpasing toolbar - but without the toolbar(instead of the toolbar it would be a dropdown element, which is essentially a RelativeLayout that has LinearLayout right beneath (the expanded items in the dropdown) that is hovering over the whole layout once the dropdown is pressed.
I thought about implementing a collapsing toolbar and putting the drop down inside the toolbar, but that probably won't be a good idea since a drop down is not a static component like an imageview for example.
I also use ActionBar in my app, so migrating to Toolbar will most likely be very time-consuming, not to mention all the technical issues that come with it.
Aside from that, I thought about detecting a movement/scrolling direction in the listview, and then hide/show the drop down, but it is very buggy when I simply hold my finger on the list view ( it goes mad and switches from up to down and vice versa very very quickly and doesn't stop until I lift the finger).
Are there any other options to implement this sort of behavior?

I'm very curious about what others propose, because I fight with that some time ago. Finnaly I'm detecting movement/scrolling direction in the listview, and then hide/show the header (as you mention). I did it like this (and I don't have bugs):
listView.setOnScrollListener(OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
final int currentFirstVisibleItem = view.getFirstVisiblePosition();
if (currentFirstVisibleItem > mLastFirstVisibleItem) header.setVisibility(View.GONE);
else if (currentFirstVisibleItem < mLastFirstVisibleItem) header.setVisibility(View.VISIBLE);
mLastFirstVisibleItem = currentFirstVisibleItem;
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
I the end I made header to show/hide with animation.

ListView has a default scrollListener. You can use it to know if it's scrolling or not. then hide the dropDown!
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView absListView, int i) {
//detect stop. have a counter then view the dropDown
}
#Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
//Hide dropDown
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(mLastFirstVisibleItem<firstVisibleItem)
{
Log.i("SCROLLING DOWN","TRUE");
}
if(mLastFirstVisibleItem>firstVisibleItem)
{
Log.i("SCROLLING UP","TRUE");
}
mLastFirstVisibleItem=firstVisibleItem;
}
}
});

Related

Transparent to Solid effect for Android Lollipop Toolbar

In Android Lollipop 5.0, I am trying to make the toolbar for a fragment have an effect where it goes from a Transparent to a Solid color when the listview under it scrolls up. I have see a lot of implementations of this before lollipop. Has someone done this for a fragment in lollipop?
You can do. Now it even easier - just change alpha channel depends on position of ListView. Kickoff example:
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
#Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int i1, int i2) {
//invert your color changing policy
toolbar.setBackgroundColor(Color.argb(255 - firstVisibleItem, 30, 30, 30));
}
});

Can't scroll in a ListView in a swipeRefreshLayout

I'm having an issue with the SwipeRefreshLayout. I have a list view within the layout and every time I scroll upward in the list view the swipe to refresh layout is tiggered and you can never scroll back to the top of the layout. Anyone have any ideas?
I found an answer to my question. I am manually enabling the swipeRefreshLayout when the first child in the listview is at the top of the list.
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView listView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int topRowVerticalPosition = (listView == null || listView.getChildCount() == 0) ?
0 : expandableListview.getChildAt(0).getTop();
refresh.setEnabled((topRowVerticalPosition >= 0));
}
});
https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
According to Android docs at the above link,
This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child.
If the ListView is the 1st direct child of the SwipeRefreshLayout, refresh won't be triggered before scrolling back to the top of the ListView. You don't need to do anything in onScroll().

google play 2014 scroll to hide top image

I´m trying to create an app inspired in the google play app.
I have my
RelativeLayout
- RelativeLayout: * Header(this is the one that will be hidden when scrolling).
- RelativeLayout
* ListView
When I scroll down I want the header to be pushed up the screen and then my listview will occupy the whole screen and when I scroll up the header will return to its original position.
I am currently using this code, but it flickers when I´m somewhere at the top of the list.
// Hide top relative layout
list.setOnScrollListener(new AbsListView.OnScrollListener() {
int mPosition=0;
int mOffset=0;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
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
overridePendingTransition(R.anim.slide_up, R.anim.slide_down);
headerLayout.setVisibility(View.GONE);
} else {
// Scrolled down
headerLayout.setVisibility(View.VISIBLE);
}
}
});
Can anyone please help me? It will be appreciate. I don't know if I'm using the right layouts.
Thanks in advanced!

How do I change the list item's background after smooth scrolling to it?

I'm having an issue fetching the view of the list item when it's off screen.
First I'm smooth scrolling to it
//locationsList is a listview
locationsList.post(new Runnable() {
#Override
public void run() {
locationsList.smoothScrollToPosition(k);
}
});
Then I want to do
View listItem = locationsList.getChildAt(k);
listItem.setBackgroundColor(getResources().getColor(R.color.highlight));
How do I call this after it's done scrolling?
Set a unique tag to listitem view. Access this list item with this tag whenever you want to change the scrolling.
If all you want to know is when your scrolling did complete, you need to add an onScrollListener to your listview:
locationsList.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState==OnScrollListener.SCROLL_STATE_IDLE){
View listItem = locationsList.getChildAt(k);
listItem.setBackgroundColor(getResources().getColor(R.color.highlight));
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
Of course the above needs a logic to update the background ONLY if scroll has before initiated by you, so you should probably use a boolean flag and set it to true on the locationsList.smoothScrollToPosition(k); execution, then if this flag is true, run the code in the scrollListener and set the flag back to false

get next image on scroll in android

I have an ImageView inside a RelativeLayout.It gets images from database.I want to add scroll-event to it such that when image is scrolled I should get next and previous image based on direction of scroll.
How can I do that in android?
OnScrollListener
Interface definition for a callback to be invoked when the list or grid has been scrolled.
Example for implementing the onScrollListner as per below code -
//Here is where the magic happens
this.getListView().setOnScrollListener(new OnScrollListener(){
//useless here, skip!
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
//dumdumdum
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
{
//what is the bottom iten that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
//is the bottom item visible & not loading more already ? Load more !
if((lastInScreen == totalItemCount) && !(loadingMore)){
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
}
}
});
Have a look at this example and you can also use this Lazy load of images in ListView also

Categories

Resources