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));
}
});
Related
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;
}
}
});
I am building an application like techcrunch.
I am fetching data from server in JSON format and displaying the data in list view like article title,author name and image.
I have applied pagination means when user scroll more articles load in a list view. My pagination works fine but there is an issue in the scroll function as the fresh or new data loads the scroll dose not aligns with the data.
To clarify more in simple words my scroll-er goes at the top of the page when i am actually scrolling down
this is my code :
listView.setOnScrollListener(new AbsListView.OnScrollListener()
{
#Override
public void onScrollStateChanged(AbsListView absListView, int i)
{
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount){
if (mPreLast != lastItem)
{
mPreLast = lastItem;
onStart();
}
}
}
});`
You can use:
listView.setSelectionFromTop(newPosition, 0);
With this method you can set the position to a ListView, using the first parameter as the new position on the list, and the second parameter can be used to set the distance from the top
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
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
A GridView has BaseAdapter derived class responsible for populating its children. It works fine.
I want to do something when the GridView is refreshed. In other words, I wish there was a method "addOnRefreshListner()".
It needs to accommodate API Level as low as 8 (Froyo).
What if you override BaseAdapter.notifyDataSetChanged() in your custom adapter?
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
// A change has happened and caused the GridView to refresh
}
notifyDataSetChanged() is the solution to the absence of your addOnRefreshListener().
Try implementing a View.OnLayoutChangeListener() for API 11 or higher.
View myView = findViewById(R.id.my_view);
myView.addOnLayoutChangedListener(new OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
// Do what you need to do with the height/width since they are now set
}
});