Prevent onFocusChanged firing when ScrollView is scrolled - android

I have a problem where I have several TextView objects in my Activity. More than can fit on the screen view so I have placed them inside a ScrollView.
Now each TextView item is placed into a RelativeLayout before placing onto the ScrollView.
Each RelativeLayout object then has onClickListener and onFocusChangedListeners assigned.
Each Listener implements a method that pops up a dialog to enable the user to edit the value stored by the TextView.
The problem that is occuring is that when the screen is scrolled the onFocusChangedListener event is fired causing the dialog to appear for several items at a time.
Is it possible to detect if the Scrollview is scrolling and prevent the onFocusedChanged event firing?
I can post code if required but its quite large as the items added to the page are done so dynamically. This also means there is no xml for the layout.
I looked at using the ScrollView onTouchListener to add a flag ignoreFocusChange but it appears this would not work as the event was hit sveral times for one swipe/fling on the screen.
Please help
Regards,
Iain

After spending some time on this I found that the best solution for me was to remove the onFocusChangedListener and replace it with onTouchListener

Related

childview in scrollview click delay after scroll

I have a webview inside a scrollview with javascript listening click event.
It is no problem to listen it in normal case but cannot be listen just after scroll. I need to have a first click to stop the scroll and click one or two times to trigger the click event. Or wait a while until the scrollbar fade out.
I have done a research and know it is normal behavior of scrollview. I have try to set a ontouchlistener on webview to requestDisallowInterceptTouchEvent(true) but just make it cannot able to scroll anymore. Is it there are any approach to cancel the scrolling and perform a click at the same time with no delay in a scrollview?
Finally, I have solved it by reading the document. I just put android:descendantFocusability="blocksDescendants"
in the main child view of scrollview and then solve my problem.
For Gopal Singh Sirvi asking, I think you need to put a webview in a scrollview in some cases. Let say the data of one section contains html code. I guess the content page of gmail is also like this. (After open show layout bounds, the content seems inside a webview but others not)
Hope this help!

TouchEvent cancelled in TextView in ScrollView

I have a layout with a TextView in a ScrollView because scrolling in the TextView doesn't work well and I have a custom ActionMode in addition to the default txt ActionMode and scrolling in the textview directly for some reason destroys my ActionMode.
However, my Problem:
In my app you can make selection in the text and edit those selections later, I implemented these selections myself since the 'normal' selection mode doesn't work (no handles on selections that were set programatically...!?) When I edit a selection it works as long as the text is not scrolled (i.e. the top line is on top of the scrollview). When the text is scrolled, the selection works as long as I stay in the same line. Once I go up or down, the TextView's onTouch gets a 'cancel' event and the OnTouchListener of the ScrollView is called...
1) Why does the TextView's MotionEvent get cancelled?
2) Is there a way I can prevent it? Maybe tell the ScrollView to ignore everything for the time I'm editing the selection?
3) Is there a way I can map the coordinates that I get in the ScrollView's onTouch to coordinates in the TextView?? (So I could at least dispatch the event to the txt view and 'pretend' as the 'cancel' never happened....
Any ideas???
Thanks
Ok I found a solution.
I implemented my own ScrollView where I overwrite onInterceptTouchEvent and return false while the textView is being edited...

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!

TabHost inside a ScrollView forces it it to scroll to the bottom

My question is very similar to this unanswered one, with some small differences that I will explain:
TabHost inside of a Scrollview: always scrolls down when a Tab is clicked
My ScrollView initiates scrolled to the bottom, showing the TabHost content instead of the ones on top of it (you can see the screenshots in the linked question, it's very similar).
If I manually scroll up, clicking in different tabs doesn't affect the ScrollView, the problem is only in the first time it's shown. This is the difference between the other question.
Things I tried with no success:
Make the top components inside the ScrollView focusable, so they would get the focus instead of the tab.
Force the ScrollView to scroll to the top in onResume() method with sv.fullScroll(View.FOCUS_UP); or sv.scrollTo(0, 0);. Also, no luck in the initialization, but subsequent calls to onResume() effectively scrolls it to the top.
Any ideas on why this is happening? Tips on how to further investigate are also very welcome.
Probably too late to help you Pedro, but I did figure out a solution that worked for me. I noticed in the TabHost source that it requests Focus if nothing else in the view has focus. So I made sure that the first component in the Scroll view was focusable and requested focus for it.
// Get the first component and make sure it is focusable. Note you have to use setFocusableInTouchMode and not setFocusable for this to work.
TextView v = (TextView) view.findViewById(R.id.first_component_in_view);
v.setFocusableInTouchMode(true);
v.requestFocus();
Hopefully this will help someone.
I have a similar problem and haven't solved it. However, to force the scroll view to scroll you need to update it's progress through a separate thread to post on the UI. e.g.
quickScroll.post(new Runnable() {
#Override
public void run() {
quickScroll.scrollTo(0, 54);
}
});
Where quickScroll is my ScrollView. I believe the reason for this is that you have diagrammatically defined your layout, or inflated it from a custom viewgroup. So setContentView doesn't hold a reference to the scrollView, so you have to force the UI to update off the variable allocated to the ScrollView.

When do Android EditText's requestFocus()

I have a number of EditTexts inside a scrollview. In the background these fields are linked to a number of other fields and regularly (once a second or so) the layout has it's textfields updated depending on the values in the EditText.
The problem is if one EditText has focus at the top of the ScrollView and the user scrolls the view done (i.e. so the focused EditText is off screen), but if the update is made to the views, the scroll view moves up to focus on the EditText at the top of the view.
The closest I have got to remedying this is to make the scroll view change focus when scrolling however this is still has the same behaviour but at a reduced scale.
Does anyone have an idea of how I can stop this from happening?
Cheers,
Matt
You can try using EditText#setFocusable around your code that updates the value. I haven't tested this but if the problem is that it is getting focus when you use setText() it may help.

Categories

Resources