xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frame_living_root_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SurfaceView
android:id="#+id/player_surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<android.opengl.GLSurfaceView
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:visibility="gone" />
<com.netease.nimlib.sdk.avchat.model.AVChatSurfaceViewRenderer
android:id="#+id/video_render"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<com.netease.nimlib.sdk.avchat.model.AVChatSurfaceViewRenderer
android:id="#+id/bypass_video_render"
android:layout_width="110dip"
android:layout_height="150dip"
android:layout_gravity="right|end|bottom"
android:layout_marginBottom="70dip"
android:layout_marginEnd="10dip"
android:layout_marginRight="10dip"
android:visibility="gone" />
<!--root view is FrameLayout with 40dip layout_margin and is a little complex-->
<include layout="#layout/layout_live_top" />
<!-- the content of this view will be scroll by touch event-->
<com.example.SimpleSlideView
android:id="#+id/slide_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- yellow background-->
<ImageView
android:id="#+id/yellow_child_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0" />
</com.example.SimpleSlideView>
</FrameLayout>
SimpleSlideView:
public class SimpleSlideView extends FrameLayout {
public int screenWidth;
public int screenHeight;
private float lastY;
public SimpleSlideView(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
initScreenSize(context);
scrollTo(0, screenHeight);
}
private void initScreenSize(#NonNull Context context) {
DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
float rawY = ev.getRawY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
lastY = rawY;
break;
case MotionEvent.ACTION_MOVE:
final int delta = (int) (rawY - lastY);
scrollTo(0, getScrollY() - (delta));
lastY = rawY;
break;
case MotionEvent.ACTION_UP:
scrollTo(0, screenHeight);
break;
}
return true;
}
}
I want to write a SimpleSlideView that can scroll it content down by pull down on it SimpleSlideView works as expected in lightweight XML but a little weird in complex XML.
#layout/layout_live_top contain a FrameLayout root view with 40dip layout_margin and layout_live_top is a little complex.
SimpleSlideView is a MatchParent view without padding and margin, same with it child #+id/yellow_child_view which has a yellow background.
Something weird is happen when quickly pulldown:
The yellow area has 40dip margin left and right at the beginning of the pull and then it become match parent.SimpleSlideView seems to be affected by the margin(40dip) of other view, it should be matched parent while pulling down.I don't know why this happened.It is because of my complex XML?
Related
I have a horizontal recycler-view within motion layout. Sometimes when I do a horizontal swipe, the page starts scrolling in the vertical direction. This happens when we do a diagonal fling.
I want something like Google Play where horizontal recycler-views can defend slightly diagonal vertical swipes. BTW setting nestedscrollEnabled doesn't work.
There is quite a nice article regarding all the RecycleView scroll behavior and the issue itself. Checkout RĂºben Sousa article. Solution is quite easy, try to use adopted RecyclerView:
/**
* A RecyclerView that only handles scroll events with the same orientation of its LayoutManager.
* Avoids situations where nested recyclerviews don't receive touch events properly:
*/
public class OrientationAwareRecyclerView extends RecyclerView {
private float lastX = 0.0f;
private float lastY = 0.0f;
private boolean scrolling = false;
public OrientationAwareRecyclerView(#NonNull Context context) {
this(context, null);
}
public OrientationAwareRecyclerView(#NonNull Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public OrientationAwareRecyclerView(#NonNull Context context, #Nullable AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
addOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
scrolling = newState != RecyclerView.SCROLL_STATE_IDLE;
}
});
}
#Override
public boolean onInterceptTouchEvent(MotionEvent e) {
final LayoutManager lm = getLayoutManager();
if (lm == null) {
return super.onInterceptTouchEvent(e);
}
boolean allowScroll = true;
switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
lastX = e.getX();
lastY = e.getY();
// If we were scrolling, stop now by faking a touch release
if (scrolling) {
MotionEvent newEvent = MotionEvent.obtain(e);
newEvent.setAction(MotionEvent.ACTION_UP);
return super.onInterceptTouchEvent(newEvent);
}
break;
}
case MotionEvent.ACTION_MOVE: {
// We're moving, so check if we're trying
// to scroll vertically or horizontally so we don't intercept the wrong event.
float currentX = e.getX();
float currentY = e.getY();
float dx = Math.abs(currentX - lastX);
float dy = Math.abs(currentY - lastY);
allowScroll = dy > dx ? lm.canScrollVertically() : lm.canScrollHorizontally();
break;
}
}
if (!allowScroll) {
return false;
}
return super.onInterceptTouchEvent(e);
}
}
This Work perfect for me.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="vertical"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp">
<TextView
android:id="#+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#+id/btnMore"
android:text="Sample title"
android:textColor="#android:color/black"
android:textSize="18sp" />
<Button
android:id="#+id/btnMore"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:theme="#style/MyButton"
android:text="more"
android:textColor="#FFF" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal" />
</LinearLayout>
list_single_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:background="?android:selectableItemBackground"
android:orientation="vertical">
<ImageView
android:id="#+id/itemImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:src="#drawable/android" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/itemImage"
android:gravity="center"
android:padding="5dp"
android:text="Sample title"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Try this:
RecyclerView rv = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, true);
rv.setLayoutManager(layoutManager);
rv.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow RecyclerView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow RecyclerView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
v.onTouchEvent(event);
return true;
}
});
That code prevent scroll vertically when you scroll with your horizontal recyclerview.
I am trying to implement vertical view pager like "Happn" app
Here is my custom ViewPager class
VerticalViewPager.java
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class VerticalViewPager extends ViewPager {
public VerticalViewPager(Context context) {
super(context);
init();
}
public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// The majority of the magic happens here
setPageTransformer(true, new VerticalPageTransformer());
// The easiest way to get rid of the overscroll drawing that happens on the left and right
setOverScrollMode(OVER_SCROLL_NEVER);
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
#Override
public void transformPage(View view, float position) {
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
view.setAlpha(1);
// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
//set Y position to swipe in from top
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();
float newX = (ev.getY() / height) * width;
float newY = (ev.getX() / width) * height;
ev.setLocation(newX, newY);
return ev;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev){
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev); // return touch coordinates to original reference frame for any child views
return intercepted;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}
}
I am able to show vertical view pager. But I am not able to maintain page margin on top, So that adjacent items also can partially visible. Below setPageMargin() method is working for horizontal viewpager. I want similar gaping in vertical view pager. Please help me, I am really struck.
mPager.setPageMargin(-100);
Try this below code
viewPager.setClipToPadding(false);
viewPager.setPadding(70, 30, 170, 30);//left,top,right,bottom in px
viewPager.setPageMargin(30);
This will set the padding as desired, this one is working for me.
Try it and do let me know if it worked for you
**my viewpager item xml**
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#color/dim_white"
android:layout_height="match_parent">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/card_view"
style="#style/CardStyle.ContactList"
android:layout_width="match_parent"
android:layout_marginBottom="60dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="22dp"
android:layout_marginRight="22dp"
app:cardCornerRadius="20dp"
android:layout_height="match_parent">
<ImageView
android:id="#+id/profile_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#drawable/logo_3_1" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:gravity="center_horizontal"
android:text="Sardar"
android:textColor="#color/white"
android:textSize="#dimen/letter_large" />
</android.support.v7.widget.CardView>
</FrameLayout>
**My parent layout :**
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
">
<RelativeLayout
android:id="#+id/empty_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dim_white"
android:visibility="gone">
<ImageView
android:id="#+id/add_photo"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:src="#drawable/favpholder"
android:textSize="#dimen/verify_phone_number_text_size" />
<TextView
android:id="#+id/no_fav_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/add_photo"
android:layout_marginLeft="#dimen/spacing_24_dp"
android:layout_marginTop="#dimen/spacing_medium"
android:layout_marginRight="#dimen/spacing_24_dp"
android:gravity="center"
android:text="#string/no_followers"
android:textAlignment="center"
android:textAppearance="#style/TextStyle.Medium"
android:textColor="#color/slate"
android:textSize="#dimen/letter_large" />
</RelativeLayout>
<android.support.v7.widget.CardView
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/search_bar_height"
android:layout_below="#id/empty_layout"
android:layout_margin="#dimen/spacing_medium">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/search_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_small"
android:src="#drawable/ic_search_black_24dp" />
<TextView
android:id="#+id/search_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="#dimen/spacing_xsmall"
android:layout_toEndOf="#id/search_icon"
android:gravity="center"
android:text="#string/search_contacts" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<custom.VerticalViewPager
android:layout_below="#id/search_bar"
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
EDIT: This question was solved!
i'm facing with a, hopefully, simple problem. What i need it's to show/add image on onTouch position when the user click on the Image/Area.
This is the layout:
activity_test_diff.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestDiff"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageView
android:id="#+id/img_rectangle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:visibility="invisible"/>
<ImageView
android:id="#+id/img_test_diff1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
<ImageView
android:id="#+id/click_check_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/check_green_48dp"/>
<ImageView
android:id="#+id/click_check_ko"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="#drawable/close_red_48dp"/>
</RelativeLayout>
<ImageView
android:id="#+id/img_test_diff2"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:scaleType="fitCenter"/>
</LinearLayout>
The parent group of the Relative Layout it's a LinearLayout, but i don't care if someone touch outside of RelativeLayout. This is the code to handle the touch:
TestDiff.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_diff);
RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative_layout);
relative.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent ev) {
final int action = ev.getAction();
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
int x = (int) ev.getX();
int y = (int) ev.getY();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView iv = new ImageView(getApplicationContext());
lp.setMargins(x, y, 0, 0);
iv.setLayoutParams(lp);
iv.setImageDrawable(getResources().getDrawable(
R.drawable.check_green_48dp));
((ViewGroup) v).addView(iv);
return true;
case MotionEvent.ACTION_UP:
// On the UP, we do the click action.
// The hidden image (img_rectangle) has three different hotspots on it.
// The colors are red, blue, and yellow.
// Use img_rectangle to determine which region the user
return true;
default:
return false;
}
}
And this is the coming error:
Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
Mean someone can figure out what's happening!
Thanks
on the xml layout you have :
<RelativeLayout
android:id="#+id/relative_layout"
in the java code you have
RelativeLayout relative = (RelativeLayout) findViewById(R.id.layout_relative);
you reversed relative & layout words
At the moment only my GridView is scrollable, but I need my ViewFlipper to be scrollable as well. I need help making ViewFlipper scroll vertically
Here is my layout at the moment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_grids"
android:layout_width="match_parent"
android:overScrollMode="always"
android:layout_height="match_parent">
<ViewFlipper
android:layout_marginTop="56dp"
android:layout_width="match_parent"
android:layout_height="150dp"
android:autoStart="true"
android:id="#+id/vf1z"
android:flipInterval="3000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="#drawable/front"
android:id="#+id/imageViewz"/>
</ViewFlipper>
<GridView
android:numColumns="3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/grid"
android:layout_marginTop="208dp"/>
<include layout="#layout/search"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/include">
</include>
</RelativeLayout>
You can add all the elements in your layout in a ScrollView like this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_grids"
android:layout_width="match_parent"
android:overScrollMode="always"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ViewFlipper
android:layout_marginTop="56dp"
android:layout_width="match_parent"
android:layout_height="150dp"
android:autoStart="true"
android:id="#+id/vf1z"
android:flipInterval="3000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="#drawable/front"
android:id="#+id/imageViewz"
/>
</ViewFlipper>
<GridView
android:numColumns="3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/grid"
android:layout_marginTop="208dp"
/>
</ScrollView>
<include layout="#layout/search"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/include">
</include>
</RelativeLayout>
Or if you only want your ViewFlipper to be scrollable you can just add the ViewFlipper in a ScrollView
You can use VerticalViewPager its custom implementation
/**
* Uses a combination of a PageTransformer and swapping X & Y coordinates
* of touch events to create the illusion of a vertically scrolling ViewPager.
*
* Requires API 11+
*
*/
public class VerticalViewPager extends ViewPager {
public VerticalViewPager(Context context) {
super(context);
init();
}
public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// The majority of the magic happens here
setPageTransformer(true, new VerticalPageTransformer());
// The easiest way to get rid of the overscroll drawing that happens on the left and right
setOverScrollMode(OVER_SCROLL_NEVER);
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
#Override
public void transformPage(View view, float position) {
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
view.setAlpha(1);
// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
//set Y position to swipe in from top
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
/**
* Swaps the X and Y coordinates of your touch event.
*/
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();
float newX = (ev.getY() / height) * width;
float newY = (ev.getX() / width) * height;
ev.setLocation(newX, newY);
return ev;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev){
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev); // return touch coordinates to original reference frame for any child views
return intercepted;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}
}
See previous answers given by #Brett
Android: Vertical ViewPager
I have a layout where I have a ScrollView and some stuffs. It looks like:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/welcome_scrol_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#drawable/bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bg"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="vertical"
android:weightSum="100"
android:background="#drawable/specialmapholder">
<android.support.v4.view.ViewPager
android:id="#+id/welcomeViewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_weight="90"
android:clickable="true" />
<LinearLayout
android:id="#+id/welcome_indicatorLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:gravity="center" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<TextView
android:id="#+id/welcome_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Large Text"
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="26dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/welcome_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Medium Text"
android:textColor="#color/white"
android:textSize="14dp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</ScrollView>
As you can see I have a Viewpager in the ScrollView. I want to disable scroll when I touch ViewPager, because when I want to change picture page is scrolled. How can I disable the scroll function when I touch the ViewPager?
In Android, parents can consume child's TouchEvents. In this case, the ScrollView consumes the TouchEvents of your ViewPager component. To avoid this behaviour, Android framework provides the onInterceptTouchEvent(MotionEvent).
In your case, you have to subclass ScrollView and change the onInterceptTouchEvent(MotionEvent) method. You should change the onInterceptTouchEvent(MotionEvent), so that the ScrollView consumes the child's event only when the user scrolls the ScrollView vertically.
example implementation for ListView:
public class VerticalListView extends ListView {
private float mLastX;
private float mLastY;
private float mDiffX;
private float mDiffY;
public VerticalListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalListView(Context context) {
super(context);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// reset difference values
mDiffX = 0;
mDiffY = 0;
mLastX = ev.getX();
mLastY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
mDiffX += Math.abs(curX - mLastX);
mDiffY += Math.abs(curY - mLastY);
mLastX = curX;
mLastY = curY;
// don't intercept event, when user tries to scroll horizontally
if (mDiffX > mDiffY) {
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}