I am developing an app which it has a vertical ViewPager within ScrollView. The thing is ViewPager only recognize touch events when I scroll horizontally, otherwise ScrollView will get focused and perform the scroll.
I think this could work:
viewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
scrollView.requestDisallowInterceptTouchEvent(true);
int action = event.getActionMasked();
switch(action){
case MotionEvent.ACTION_UP:
scrollView.requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
But because of ViewPager does not recognize touch events when I perform vertical scroll this piece of code does nothing.
What I want to achieve is give focus to ViewPager when I touch it and give focus to ScrollView when I'm not touching the ViewPager. Thanks in advance.
This is some code I use for a scrollview that has problems with textviews focusing. It might help you. It might not.
ScrollView sv = (ScrollView) view.findViewById(R.id.svRecord);
sv.setOnTouchListener(this);
sv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
sv.setFocusable(true);
sv.setFocusableInTouchMode(true);
Related
i have integrated listView inside the scrollView. im facing issue while vertical scrolling. how to fix this issue ?
ListView inside the scrollView is an bad programming practice. ListView aleary has its own scrolling functionality and you are again embedding it into the scrollView that why it is creating an jerk.
Don't use listView inside the scrollView.
If you want to scroll let's say listView and not whole scrollView you could try this code. However, it is not good idea to place listView inside of scrollview. Your users may find it uncomfortable.
yourListView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.yourListView) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
I have a vertical custom list and each item of vertical list contains a horizontal list-view. When I scroll horizontally then list also moves little vertically. Which makes it less user friendly. Can I disable vertical scrolling while scrolling horizontally. I am using
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/hlistview"
android:layout_width="match_parent"
android:layout_height="155dp"
android:layout_margin="6dp"
android:background="#fff"
android:fitsSystemWindows="true"
/>
for horizontal list view
The idea is to disable the parent listview to intercept touch event. This might work :
HorizontalListView hv = (HorizontalListView)findViewById(R.id.hlistview);
hv.setOnTouchListener(new ListView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ListView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ListView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle HorizontalScrollView touch events.
v.onTouchEvent(event);
return true;
}
});
Reference : https://stackoverflow.com/a/22609646/1239966
Best way to do this.
put given code on your horizontal listview onScroll method it work perfact
ViewParent view_parent = getParent();
if (view_parent != null)
{
view_parent.requestDisallowInterceptTouchEvent(true);
}
I am developing an android application which has a scrollview. I have overriden my view from ScrollView and have implemented the onScrollChanged method. As the scrollview scrolls and reaches the bottom, it loads more products and add them to scrollvew. I am having a strange issue. First time when products loads and I try to drag up the scrollview, onScrollChanged never happens. Then I googled and found something:
scrollView.setOnTouchListener(new ListView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
After adding this, when I try to drag the scrollview from empty area of scrollview, it starts to call onScrollChanged and afterwards I start dragging from anywhere else, onScrollChanged is being called. My question is, why it is behaving like that? It should call onScrollChanged from whatever the touch event starts. I hope, I explained my point well. Any solution?
Why don't you use endless adapter with a view like ListView or GridView to achieve the desired behavior instead
The question explains everything I want.
HorizontalScrollView seems to be the only widget for which setOnClickListener() does'nt trigger.
NOTE: I can't use onTouch event because on every touch it is triggered 4-5 times.
I also can't use onTouch on its parent view because parent view has many buttons with different functionality.
The following description is not important:
But still, these are the existing links I've searched(none helped):
horizontalscrollview onclicklistener doesn't get called
How to implement on click listener for horizontalscrollview
Click event of HorizontalScrollView
Android click event of items in HorizontalScrollView not respond after scroll
These are the links which I posted(none got answered completely):
Insert views in HorizontalScrollView programatically
One of the ImageButton not clicking & make HorizontalScrollView clickable
I asked multiple questions in these links, 1 of which was "onClickListener for HorizontalScrollView". That part of my question was never answered.
Hence an all exclusive question.
I came up with two ways of doing it.
The simpler (but less ideal) one:
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
scrollView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// Do stuff
}
return false;
}
});
This will get called only once when you click, drag or whatever you do with your finger. However, it will also react to all kinds of gestures, so if you want to detect only tap/click events, you need to further investigate the "event" object and filter out events you don't need. This may be more work than you would like, so you should better use a GestureDetector to do it for you.
This leads to method 2:
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
final GestureDetector detector = new GestureDetector(this, new OnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
// Do stuff.
return false;
}
// Note that there are more methods which will appear here
// (which you probably don't need).
});
scrollView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return false;
}
});
Well it happened to me now and I came here for solution, but in my case it was just easier to listen to onClick to its child view instead...
Hi in my application the screen is so large which contains a gridview with images and listview with lot of list items and also so many text views existed in my screen. so that i placed this content in scrollview to view on the mobile screen. When I placed the content in the scrollview then the gridview/listview scroll is not working.
Is there any way to solve this issue. I referred so many links but i could not get the solution.
The links are
Grid of images inside ScrollView
How can I put a ListView into a ScrollView without it collapsing?
etc
You can solve it using following code, but I suggest please change UI, it is not according android guideline. it' easily scrolled in iPhone interface.
final ScrollView childScroll = (ScrollView)
view.findViewById(R.id.resourceactivity_sv_classes_detail);
parentScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
//Log.v(TAG,"PARENT TOUCH");
childScroll.getParent().requestDisallowInterceptTouchEvent
(false); return false; } }); childScroll.setOnTouchListener(new
View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
//Log.v(TAG,"CHILD TOUCH");
v.getParent().requestDisallowInterceptTouchEvent(true); return
false; } });
You can modify gridview instead of child scrollview.