Infinitely scroll image inside a scroll view in android - android

So basically i have a an imageview inside a scrollview, what i want to do is be to be able to scroll infinitely without ever reaching the bottom of the scrollview.
Can this be done in any way ? the image of course will have to be repeated .. but any other method is appreciated

You can use a ListView and an Adapter that is modified slightly to have "infinite" scroll.The change required in your Adapter is something like this:
#Override
public int getCount()
{
return Integer.MAX_VALUE;
}
The trick here is to tell it that the count is MAX_INT.However by doing this, it will make scroll downwards infinitely but not backwards.
In order to make the list go backwards, as in an infinite carrousel, you can trick the system by setting the default element to be something like Integer.MAX_VALUE/2 by calling:
listView.setSelection( Integer.MAX_VALUE/2 );
By doing this, it makes the effect of infinite carrousel in both directions.I think presence of ImageView will not be a problem and it should work then too.Hope this helps.
Edit
For this, as you need to learn how to make a custom ListView and a Adatper, i suggest you to follow this easy and short tutorial.You can also download the entire project from that link.After running that project, add the above mentioned code.

Related

ListView with ArrayAdapter showing only 1 item for height wrap_content

I'm having a issue where my ListView is displaying only one row even though ArrayList containing data has 2 items in it.
For clarification, YES, I'm using ListView inside a ScrollView but to balance it out I'm using custom ExpandableHeightListView (retrieved from one of the answers on SO) which is useful for exactly this purpose. This ListView is not scrollable. It has fixed height.
Also, within the same ScrollView, couple of other ListView are working fine but this one ListView is not working properly.
Going further, when I logged the position variable value inside getView() method, it's always 0 for this ListView while for other ListView, it's increasing sequentially.
I've compared both Adapter's code and they are identical but still current ListView is not working.
I tried with overriding getView() method, but to no avail.
Any help appreciated.
EDIT
If I give fixed height to ListView like 250dp, both the rows are getting displayed but it shows only 1 row with height set to wrap_content
As I know that ListView within a scrollView does not worked properly.
Because both widget has scroll so they does not worked well.
So I recommend to delete the ScrollView
Thanks..
I know this is not what you want to hear, but seriously, whatever you are programming, consider displaying it on tabs or similar, such that you don't need the scrollview.
If you really must have so much data on one screen, consider using something like a LinearLayout with an adapter to display your data.
Listviews are not designed to work inside a scrollview because they are scrollable themselves. You can "hack" your way around it, but it's not nice. If you really need the features that listviews provide, consider splitting up your screens into tabs, displaying a listview or two in each tab.
This solution worked for me and also adjusts the layout automatically. On the top-level ListView adapter, inside the getView() method, you will create a view that references a layout with another ListView. Assuming that "inner" ListView is declared as "listView" with its adapter called "adapter", try using the code below. Also, ensure that the inner ListView layout is set to WRAP_CONTENT, so that even if the height is correct, it is not being overridden by the layout constraints.
listView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
listView.getLayoutParams().height = adapter.getCount() * {whatever your list item height is} OR do something else to add up all of the heights;
listView.requestLayout();
listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
);

How can I make bounce effect to listview as in ios

I need to make bounce effect to my listview in android.ie when I scroll down listview has to scroll beyond its starting point and the come back to its original position
Please help..
as you not posted your code(I think it is also not required as per your question).
So I am giving a Custom Widget
It is custom widget of listview (not that much complicated, just animation is added into it)use the library for your application.
and also from that in PullToRefreshActivity there is
mListItems.addFirst("Added after refresh..."); statement just remove it if you want..
(don't worry about activity name or class name here target is you got your result)
i.e bounce effect to Listview
If you satisfy..then Please accept the answer..
By the looks of things in the ScrollView API you should be able to override the onOverScrolled() method if you create a custom view that extends the ScrollView class.

Infinite scroll in LinearLayout/ScrollView?

I know it's somehow possible to make a ListView that loads more data when the user has reached the bottom of the list. However, I'm working with a ScrollView, which I have a LinearLayout in, and these two components works great with the scrolling and so. But I don't know how I'm supposed to do so it gets an infinite scroll.
I suppose I need to add something that reads what is shown of the LinearLayout on the screen, and when it calculates that it is the bottom of the LinearLayout that is being shown (by using the current position and the height of the View), it triggers an event.
But as I said, I don't know how to accomplish this. So, if anyone can give me some help I would be very grateful.
EDIT: I found this post here on StackOverflow How to trigger an event when scrollView reach the bottom with Android?, but I don't know what to do with the answer:
Given the requirements, you'll likely be extending BaseAdapter (as opposed to CursorAdapter which utilizes a different mechanism).
Here's a snippet for that:
public View getView(int position, View convertView, ViewGroup parent) {
if (position == backingLinkedList.size()) {
//get more items and add them to the backingLinkedList in a background thread
notifyDataSetChanged();
}
}
If you can trigger an event when you reach the bottom of a scrollview, You could try creating your own type of view to hold your data. When you reach the bottom of the view load the next data into your view class and add it into the layout of the scroll view.
So basically create a class that holds a section of what your showing. Add the first one into the scrollview. When you hit the bottom create another view holding your next data and use scrollviews_layout.addview(view_holding_the_new_data)
I know this was solved a long time ago, but I wanted to share my solution for future infinite scrollers.
I used the onScroll method within the OnScrollListener to trigger a background thread to grab more data and adapter.notifyDataSetChanged to notify the ListView of more data to load and create an infinite scroll. For some reason, onScrollStateChanged wasn't triggering, but onScroll did what I needed so I didn't need to bother.
For the longest time I didn't understand how to keep the ListView from reloading and placing me back at the top of the list, but then I realized I only need to initialize a new adapter and call setListAdapter once, and every time after that, only call notifyDataSetChanged. Before, I was also calling setListAdapter, thereby placing me back at the top.

Android ListView scrolling to top

I have a ListView with custom rows. When any of these rows is
clicked, the ListView's data is regenerated. I'd like the list to
scroll back to the top when this happens.
I initially tried using setSelection(0) in each row's OnClickListener
to achieve this but was unsuccessful (I believe because the ListView
loses its scroll position when its data is invalidated - so my call to
setSelection is undone. I still don't understand how the ListView
decides where to scroll to after invalidation, though).
The only working solution I know of was given by Romain Guy here:
http://groups.google.com/group/android-developers/browse_thread/thread/127ca57414035301
It involves (View.post)ing the call to _listView.setSelection(0). I
found this to perform quite poorly.
The newly generated list shows up with its scroll location unchanged
and there is a considerable delay before it scrolls back to the top.
Is there any better way to achieve this functionality?
Any help would be much appreciated.
Thanks!
call listView.setSelectionAfterHeaderView(); to scroll to top
I have tried lot but this one worked for me
list.smoothScrollToPosition(0);
I simply use listview.setSelection(0);
Works fine for me.
If you need instant scroll just after ListView adapter's data was changed, pay attention that it might not be yet populated. In this case you should post() your setSelection() or setSelectionAfterHeaderView() via Handler so it will be called later in the queue.
listView.Handler.post(new Runnable() {
#Override
public void run() {
listView.setSelectionAfterHeaderView();
}
});
This worked for me.
Personally, I recommend you find a different UI pattern. It is possible that users will find your current "click, and the list changes in situ" approach intuitive, but I am skeptical.
You could try subclassing ListView and overriding layoutChildren() to chain to the superclass, then call setSelection(0) in the case where that is needed. If the "considerable delay" is due to just the post() call, this should clear it up.
as a workaround, you can create a new adapter containing the new regenerated data, then call ListView.setAdapter. after that call ListView.setSelection(n).
btw, the solution provided by commonsware is worked.
On some different requirement.
If you want to scroll up just like while chatting.
mListView.smoothScrollToPosition(mAdapter.getCount());
This one worked fine when you want to focus the edittext from listview header
listview.setSelectionFromTop(0,0);
If you want to select the particular index view from listview then
listview.setSelection(index); // o for top

Animation for expandableListView

Is it possible to apply an expand or collapse animation for expandableListView?
It can be done using a simple ListView that contains an initially hidden view and a custom class that extends Animation.
The basic idea is to start with View.GONE then gradually re-size the margin from a negative value to the required size while setting visibility to View.VISIBLE.
See:
https://github.com/tjerkw/Android-SlideExpandableListView
Android Animation: Hide/Show Menu
How do I animate View.setVisibility(GONE)
..and finally
Cool toolbar for ListView items + source
The last example contains all the code you need. It looks a bit hackish to me, especially the fact that you must initially set view.bottomMargin = -50 or more, otherwise the animation does not work properly the first time, but so far I did not find any viable alternative (apart from using a ScrollView with your own container items instead of a ListView).
And finally, this app includes the above example, among lots of other useful examples with links to sources:
https://market.android.com/details?id=com.groidify.uipatterns
Update: Google removed the app from play store allegedly for intellectual property violation (although it only contained demos and links to open source projects), the author now made the apk available for direct download from http://goo.gl/ihcgs
For more details see https://plus.google.com/108176685096570584154/posts. NB: I'm not affiliated with the author.
I have done a similar job for a simple list view.For doing that I overrode the getView method and applied translate up( or down) animation on each list item.The degree of translation was decided by the position of the list item.
I've found a possible (partial) workaround for this problem.
first you need to store the scroll state of the ExpnadableListView :
#Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
this.mScrollState = scrollState;
}
public int getScrollState() {
return this.mScrollState;
}
for the listView itself, you need to store which group was clicked, so that only its children will get animated:
mListView.setOnGroupClickListener(...
#Override
public boolean onGroupClick(...){
mGroupPosition=groupPosition;
now, in the getChildView() method, you check the state of the scrolling , and if it's idle, you start the animation, for example:
public View getChildView(...) {
// <=prepare rootView and return it later
if (groupPosition==mGroupPosition&&getScrollState() == OnScrollListener.SCROLL_STATE_IDLE)
rootView.setAnimation(...)
this will set an animation for the child views each time you expand the group.
the drawback of this are:
only for the expanded child views. you will need to think of extra logic to animate them when collapsing the group.
all animations start at once . you will need to add multiple animations one after another if you wish that it would work otherwise.

Categories

Resources