Android: Horizontal and vertical scroll for recyclerview - android

I am trying to implement horizontal and vertical scrolling for a Recycler view at the same time.I have to show a table of 8 columns, so I plan to implement horizontal and vertical scrolling at the same time.
I tried HorizontalScrollView in list row but it is scrolling horizontally in one row.
I also tried the HorizontalScrollView as the parent of recyclerview but its not working
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:textColor="#color/title"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaa"/>
<TextView
android:layout_toRightOf="#+id/title"
android:id="#+id/genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="bbb"/>
<TextView
android:layout_toRightOf="#+id/genre"
android:id="#+id/year"
android:textColor="#color/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content"
android:text="ccc"/>
</RelativeLayout>
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
can any one please tell me an idea.

The best solution i found so far to have horizontal and vertical scrolling on one view is to put it inside one FrameLayout. Then your view must implement OnTouch (you can do this in your activity to) and you just have to scroll it as the touch pointer moves.
Here is an example :
xml :
<FrameLayout
android:id="#+id/TwoWaysScrollableContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false">
<YourView
android:id="#+id/GridContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</FrameLayout>
C# (i use xamarin, but Java wouldn't have so many differencies i think)
:
public class MyActivity : Activity
{
private MyView _myView; //Can be GridView, relative layout or wathever
private float _startX, _startY,_prevDx, _prevDy, _dx,_dy;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_myView = FindViewById<MyView>(Resource.Id.GridContainer);
}
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
{
mode = Mode.DRAG;
startX = e.GetX() - prevDx;
startY = e.GetY() - prevDy;
break;
}
case MotionEventActions.Move:
{
/* you also could add logic to prevent your view from scrolling out of you grid bounds*/
if (mode == Mode.DRAG)
{
dx = startX - e.GetX();
dy = startY - e.GetY();
_myView.ScrollBy((int)(dx), (int)(dy));
startX = e.GetX();
startY = e.GetY();
}
break;
}
case MotionEventActions.Up:
{
mode = Mode.NONE;
prevDx = dx;
prevDy = dy;
break;
}
}
return true;
}
}
I know this doesn't answer your question directly ( i don't know much about recycler view), but i hope it can still help you. I used this for a tile engine and it did the trick very well.

Related

Setting ImageView and TextView at predefined XY coordinates - Xamarin Android

I have a Greeting Card activity where one can place Text and Image at desired locations by moving ImageView and TextView using Touch events. After moving to location I want to save its position and use same for other greetings. But neither view.GetX(), view.GetY() nor view.Left, view.Top is working.
I can capture the XY after moving but when using same XY coordinates on SetX & SetY or Left & Top the view is placed at weird locations.
This is my XML layout and using Touch even I move imgV1, txtName and txtSignature.
<RelativeLayout
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/layoutControl"
android:layout_below="#+id/toolbar1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/pBar"
android:layout_gravity="center_vertical" />
</LinearLayout>
<FrameLayout
android:orientation="vertical"
android:id="#+id/GreetingFrame"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/layoutPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_gravity="bottom|right|center_vertical|center_horizontal|center"
android:id="#+id/imgV1"
android:layout_width="135dp"
android:layout_height="135dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="65dp"
android:scaleType="matrix"/>
</FrameLayout>
<ImageView
android:id="#+id/frameImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/digicard"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"/>
<TextView
android:textSize="18sp"
android:textColor="#000000"
android:layout_gravity="bottom|right|center_vertical|center_horizontal|center"
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="35dp"
android:layout_marginBottom="55dp"
android:text="Your Name"/>
<TextView
android:textSize="22sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_gravity="bottom|right|center_vertical|center_horizontal|center"
android:id="#+id/txtSignature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="80dp"
android:layout_marginBottom="2dp"
android:text="9999999999"/>
</FrameLayout>
</RelativeLayout>
TextView touch event
private void TxtName_Touch(object s, TouchEventArgs e)
{
TextView v = (TextView)s;
if (e.Event.Action == MotionEventActions.Move)
{
v.SetX(e.Event.RawX - v.Width / 2.0f);
v.SetY(e.Event.RawY - v.Height / 2.0f);
}
}
Any suggestions ?
Modify your code as below ,it works fine on my side .
float dX, dY;
private void TextView_Touch(object sender, Android.Views.View.TouchEventArgs e)
{
TextView view = (TextView)sender;
int X = (int)e.Event.RawX;
int Y = (int)e.Event.RawY;
switch (e.Event.Action)
{
case MotionEventActions.Down:
dX = view.GetX() - X;
dY = view.GetY() - Y;
break;
case MotionEventActions.Move:
view.Animate().X(X + dX).Y(Y + dY).SetDuration(0).Start();
break;
}
}
Refer to https://stackoverflow.com/a/31094315/8187800.

Scrollable Pager Content Inside a NestedScrollView Issue

I have a viewpager inside of a nested scroll view that may or may not contain data. If it does contain data, it is a two column staggered grid view that paginates. I want to be able to scroll down far enough where the above view is completely hidden and I can scroll the recycler view full screen. The problem I have is once the data loads in, I can only scroll a little bit more. It looks like the nested scroll range is extended by the height of the first item that appears in the recycler view. Is there something that I am missing?
Here is what is currently happening:
Where The entire view is scrollable when the items in the recycler view load but the scroll range only expands to the height of the first Item. I want to be able to scroll to the top of the tabs once the recycler loads like this image:
I am just confused on exactly what is happening here.
Here is the xml for the view in question
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/comment_layout_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/card_background">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/comment_footer"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/image_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/collection_image"
android:layout_width="match_parent"
android:layout_height="#dimen/item_detail_container_height"
android:background="#color/color_black"
android:scaleType="centerCrop"
android:layout_centerInParent="true"
/>
<Button
style="#style/Button.LightPurple.Inverse"
android:id="#+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="#string/collection_edit"
android:visibility="gone"
/>
</RelativeLayout>
<include layout="#layout/item_detail_multi_nav_layout" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_list_divider" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/collection_title"
android:layout_width="397dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="#dimen/body_font_padding"
android:gravity="center_vertical"
android:text="#string/live_action_item_name"
android:textColor="#color/on_board_clubs_join_btn"
android:textSize="18sp" />
<TextView
android:id="#+id/collection_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="#dimen/body_font_padding"
android:autoLink="web"
android:gravity="center_vertical"
android:maxLines="3"
android:text="#string/medal_item_description_placeholder"
android:textColor="#color/color_dark_gray"
android:textSize="14sp" />
<TextView
android:id="#+id/collection_item_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="#dimen/body_font_padding"
android:gravity="center_vertical"
android:text="#string/item_collection_count"
android:textAppearance="#style/TextAppearance.Large.Black.MontserratSemiBold"
android:textColor="#color/search_follow_button"
android:textSize="12sp" />
<TextView
android:id="#+id/collection_uploadedby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="#dimen/body_font_padding"
android:layout_marginTop="15dp"
android:gravity="center_vertical"
android:text="#string/item_uploaded_by"
android:textAppearance="#style/TextAppearance.Large.Black.Montserrat" />
<include layout="#layout/action_feed_header_for_uploadedby" />
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tab_picker"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center"
android:elevation="0dp"
app:tabPaddingStart="5dp"
app:tabPaddingEnd="5dp"
app:tabMinWidth="0dp"
app:tabIndicatorColor="#color/colorAccent"
app:tabIndicatorHeight="4dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/bpDark_gray"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabTextColor="#color/bpWhite" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_list_divider" />
<com.gemr.android.widgets.misc.WrapContentViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/card_background"
android:nestedScrollingEnabled="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<LinearLayout
android:id="#+id/comment_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#color/card_background"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_list_divider" />
<LinearLayout
android:id="#+id/comment_box_layout"
style="#style/comment_box_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bpWhite"
android:orientation="horizontal">
<EditText
android:id="#+id/comment_box"
style="#style/comment_box_text"
android:layout_width="0dp"
android:layout_height="#dimen/comment_text_box_height"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="#drawable/edit_text_post"
android:gravity="center_vertical"
android:hint="#string/comment_box_hint"
android:inputType="textCapSentences|textMultiLine"
android:maxLines="3"
android:selectAllOnFocus="true" />
<TextView
android:id="#+id/comment_post"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/card_view_selector"
android:gravity="center_vertical"
android:padding="4dp"
android:text="#string/comment_post"
android:textAppearance="#style/TextAppearance.Large.Gray.Montserrat" />
</LinearLayout>
</LinearLayout>
And here is the RecyclerView that is set up with the pager:
<FrameLayout
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"
android:background="#color/card_background">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/feed_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="3"/>
</android.support.v4.widget.SwipeRefreshLayout>
<include layout="#layout/fab_layout" />
Am I overlooking something very simple here?
Try this may be this will help you
public class CustomNestedScrollView extends NestedScrollView {
private int slop;
private float mInitialMotionX;
private float mInitialMotionY;
public CustomNestedScrollView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
ViewConfiguration config = ViewConfiguration.get(context);
slop = config.getScaledEdgeSlop();
}
public CustomNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private float xDistance, yDistance, lastX, lastY;
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final float x = ev.getX();
final float y = ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
lastX = ev.getX();
lastY = ev.getY();
// This is very important line that fixes
computeScroll();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - lastX);
yDistance += Math.abs(curY - lastY);
lastX = curX;
lastY = curY;
if (xDistance > yDistance) {
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}
The problem is that the Recyclerview is set to match_parent. There for it's size will equal to the screen size minus the size of all other things in the NestedScrollView combined.
In a device which needs the whole screen to contain the other things in the NestedScrollView, the size of the Recyclerview will be even 0.
Step 1:
Change the height of the Recyclerview and its parents (SwipeRefreshLayoutand FrameLayout) to wrap_content.
Step 2:
In case this doesn't work as expected, set a fixed size to Recyclerview (only to Recyclerview and the parents should remain wrap_content).
Step 3:
If you want it to work exactly as the provided image, I think you need to calculate the size at run-time. In that case you can undo the step 2.
Why don't you try the combination of Coordinate Layout and AppBarLayout to acheive this. Its much more simpler. You can check this link for different scrolls.
https://github.com/codepath/android_guides/wiki/Handling-Scrolls-with-CoordinatorLayout
This link shows you exactly what you want to acheive
https://gist.github.com/iPaulPro/1468510f046cb10c51ea

On click on parent affect child also

Below is my code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:onclick="hello"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:background="#FFFFEE"
android:layout_centerVertical="true"
android:id="#+id/ll">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:id="#+id/title"/>
</LinearLayout>
<RelativeLayout>
However when I click on LinearLayout the function hello is called.
I want function hello should not be called when I click LinearLayout
any suggestion??
I'm not sure how accurate will be.
#Override
public boolean onTouchEvent(MotionEvent event) {
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics mertrics = new DisplayMetrics ();
display.getMetrics(metrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = metrics.heightPixels / density;
float y = (float)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if(y < dpHeight / 2 + 50 && y > dpHeight / 2 - 50){
//no hello() here
}
else
{
// call hello() here and remove it from layout
}
}
return false;
}
You put android:onclick="hello" in wrong place. Try this code and let me know the result
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onclick="hello"
android:layout_centerVertical="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:background="#FFFFEE"
android:layout_centerVertical="true"
android:clickable="false"
android:id="#+id/ll">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:id="#+id/title"/>
</LinearLayout>
<RelativeLayout>
Good luck :)

android onTouch event in listview items

I've searched around and has read this thread and this thread and so, but nothing helped me .
I have a listview that contains custom layouts. the layout has a custom seekbar. I've used the seekbar out of the listview and it works very well but when uses in list view the onTouch event does not called correctly and runs only when I'm start touching it.
this is a part of my code in getView:
newSimpleDim.layTouch1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if (y < 0)
y = 0;
if (y > dimHeight)
y = dimHeight;
G.log("Event : " + event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
setHeight(newSimpleDim.layGray1, y);
setHeight(newSimpleDim.layGreen1, dimHeight - y);
setPadding(newSimpleDim.imgGreen1, -y);
newSimpleDim.txtlbl1.setText("" + (100 - ((int) ((y / (double) dimHeight) * 100))));
break;
}
return true;
}
});
and this is list item layout :
<LinearLayout
android:id="#+id/dimmerOne"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layTouch1"
android:layout_width="wrap_content"
android:layout_height="202dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layGray1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgGray1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="#drawable/lay_component_dimmer_slider_off" />
</LinearLayout>
<LinearLayout
android:id="#+id/layGreen1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgGreen1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="#drawable/lay_component_dimmer_slider_on" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/txtlbl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextView"
android:typeface="monospace" />
</LinearLayout>
This problem seems similar to the one I was facing, I am has checkbox and on itemClickListener was not working, did the following to make itemClickListener work,
Added the following properties to the checkbox,
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
and ItemClickListner started working.
for you if onTouch is not getting invoked for the seek bar then add the above code to the layout.
For detailed example for checkbox clickable in listview you can go through the link, and you can apply the same concept to make your onTouch wor
http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/
Hope it helps Cheers!!

How disable ScrollView

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);
}
}

Categories

Resources