Making child ListView scroll - android

Hello I am trying to make a terminal type app that is wider than the actual android screen. A listview is great except that I don't want the text to wrap. If the text is wider than the screen I want it to print the full text and then the user can scroll over horizontally if they want to see what was printed off the screen. Just FYI this is for ascii type tables. I have followed this example:
http://nan-technicalblog.blogspot.com/2008/12/android-how-to-use-horizontal-scrolling.html
It works great as far as horizontal text goes it does exactly what I want but I am unable to get the ListView inside the custom LinearLayout to actually scroll veritcally. I have tried passing the Y coordinates from the onScroll method to mListView such as:
mListView.scrollBy(0, distanceY);
but that seems to move the custom LinearLayout window instead of just scrolling the ListView. I can tell that the ListView is not scrolling because the window moves without the vertical scroll bar of the ListView moving. Everything just slides around. Any help would be much appreciated.

Okay this one also took me about a week in my spare time to figure out. I had to change the supplied code for dispatchTouchEvent to this:
public boolean dispatchTouchEvent(MotionEvent ev){
mGestureDetector.onTouchEvent(ev);
mListView.onTouchEvent(ev);
return true;
}
Also changed onScroll to return false just for good measure.

Related

How can I scroll item to middle of screen in scrollView when I press them

My project need to insert lots of data,I use scrollview to put them in one page.
Now I want it can scroll the item I press(primary Edittext) to the middle of screen.
I tried to use onFocusChangeListener()+scrollTo() with edittexts,but doesn't work very well.
Is there any better way to do it?
Have an onTouchListener for the scrollview.
When the user touches the screen, grab the x and y coordinates and then simply scroll to that point. So scrollview.scrollTo(x,y).
See here for more info

ListView with footer attached, smoothScrollToPosition not functioning correctly

I'm working with a rather large layout that includes a ListView and a LinearLayout beneath it as a footer. I first tried to wrap this in a ScrollView to make the whole layout scrollable, but ran into problems due to both the ListView and the ScrollView being incompatible with each other since they both have scrollable features. So, a workaround was to include the LinearLayout as a footer to the ListView.
Now, in the LinearLayout, I have buttons at various places that the user can click to return to the top of the page. The behaviour I am getting from this is odd, to say the least.
If I have not scrolled down too far, the buttons function normally. However, if I scroll down a bit too far, then clicking the button (even the same buttons that previously worked) will result in the layout scrolling up to roughly half of the way up the listview instead of the top.
Here is the method that does the scrolling, it's rather simple:
public void backToTop(View view) {
lv = (ListView)findViewById(R.id.listview);
lv.smoothScrollToPosition(0);
}
This method is triggered when any of the buttons are clicked.
I have also tried to use the "scrollTo(0, 0)" function, but that failed to do anything.
Any help would be appreciated!
**edit: After testing some more, it appears as though the point where scrolling does not seem to function properly anymore is when the listview is no longer visible on the page. As soon as I scroll past it, the buttons begin to function incorrectly.
Edit 2: SOLVED. My solution: I changed the line
lv.smoothScrollToPosition(0);
to:
lv.setSelected(0);
This seems to give the correct behaviour for all my buttons at any position that the user has placed their screen. Using setSelected does not seem to have the side-effect that I was expecting of automatically triggering the click-event. Hooray!
My solution: I changed the second line of my backToTop method:
lv.smoothScrollToPosition(0);
to:
lv.setSelected(0);
This seems to give the correct behaviour for all my buttons at any position that the user has placed their screen. Using setSelected does not seem to have the side-effect that I was expecting of automatically triggering the click-event. Hooray!

Get the position of scrollbars(not the position item) in a ListView

I am implementing a timeline UI, which is basically a ListView. The tricky part is when the user scrolls the ListView, it will display a text box(shows the time slot, say 9:00 am ) next to the scrollbar. It is on the left side, adjacent and will move with the scrollbar.
I am using the OnScrollListener to detect the scroll event. But I don't know how to get the exact position of the scroll bar (top, mid point, bottom), so that I can put the text box at the right place. There are many posts about getting the position of the top item but seems not for the scrollbar itself.
Any thoughts?
BTW, sorry for not attaching a screenshot of the UI. Stackoverflow doesn't allow me to do that since I am a new user.
But I don't know how to get the exact position of the scroll bar (top,
mid point, bottom), so that I can put the text box at the right place
You probably want something like the Path application. This tutorial it what you're looking for(probably, if you didn't see it yet).
Basically, the ListView has some methods regarding the scrollbars position(also size) that are used but those methods are not available from outside as they are declared as protected, so you need to extend the ListView class. Those methods are ListView.computeVerticalScrollExtent(), ListView.computeVerticalScrollOffset() and ListView.computeVerticalScrollRange().
Using those methods you should be able to figure where the scrollbars are placed at a particular moment in your listener.
getListView().getLastVisiblePosition()
listview.post(new Runnable() {
public void run() {
listview.getLastVisiblePosition();
}
});
View.getScrollY()
getScrollY:
Return the scrolled top position of this view. This is the top edge of the displayed part of your view. You do not need to draw any pixels above it, since those are outside of the frame of your view on screen.
Returns
The top edge of the displayed part of your view, in pixels.
Also refer : http://eliasbland.wordpress.com/2011/07/28/how-to-save-the-position-of-a-scrollview-when-the-orientation-changes-in-android/

DirectionalViewPager with 30 buttons in every page

I am trying to use DirectionalViewPager for displaying 30 buttons in every page. Normally this viewpager is working perfectly. But, if I try this pager after adding buttons then the touch is not sensible or smoother. I observe lag and irregular touch sense when there are some clickable items on the fragment.
When I try dragging the pages by dragging using non-clickable items, then its working perfectly.
Can any one assist me to handle this issue?
This issue occurred due to the Child buttons in the gridview. Because If we override onTouch method for the Button then the X and Y points are given with respect to that particular button only. So if you are passing these values to the parent then you should change the values according to the parent or else you can directly pass the Button's X,Y points which will improve the Touch sensitivity more.

How to control ListView's vertical scroll

I am in a team which is building an app for a tablet which reacts like a magazine.
We're using ListView to display one article having multiple pages.
Each row contains a lot of data, and is used to represent a page of a magazine.
One row takes almost whole of the screen in portrait mode, so one row == one screen
As of now, when a user scrolls this whole ListView, it behaves the way a normal ListView would -
if you flick fast enough, it will scroll fast
if you scroll slowly, scrolling stops as soon as you stop vertical motion of the finger
What I want-
No matter how fast or slow a user scrolls/flicks (makes a finger movement in vertical direction), only one single row should be scrolled.
Is there a way I can control how the ListView is being scrolled?
I searched a lot, quite a number of articles suggesting how to scroll to top or to a particular position, but no one actually tells how the scrolling happens so that I can control it.
Am I missing something?
Well your question is limited specifically to ListView, which I think can be achieved by calculating screen dimensions and thereby acting upon the behaviour but since I'm not sure about ListView technicallity. I do can guide you if I would have implemented I would have taken use of:
1) ViewPager: Which I think is more apt for your current requirements, for which you can find details here -
http://android-developers.blogspot.in/2011/08/horizontal-view-swiping-with-viewpager.html
or
take help of more custom requirements as mentioned on this page -
How to make an Android view that flips between views on swipe/fling
2) Inflating and using animations: Other thing I could have used is to inflate pages at runtime and react upon a particular gesture and also integrating animations on flipping.
But the use of ViewPager will be better over ListViews, AFAIT, if using ListViews is not mandatory for your requirements.
ListView uses a Scroller (OverScroller past GingerBread 2.3) which has private access. Not sure if this will help you out but it can point you in the right direction. To get the behaviour you want you can try the following(I have only tested this on a ScrollView):
//Replace the scroller with your own
Field mScroller;
try {
mScroller = ListView.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
scroll = new OverScroller(mContext, new DecelerateInterpolator(DECELERATE_AMOUNT));
mScroller.set(yourListView, scroll);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Then add an ontouchlistener to uour ListView and scroll on touch up:
mScroller.startScroll(0, startY, 0, distanceToNextPage, 500);
An alternative to the solution above you can try the following:
Add an ontouchListener to your ListView
on touch up:
yourListView.smoothScrollToPosition(LIST_ITEM_INDEX);

Categories

Resources