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);
}
}
Related
I am trying to create a horizontal scroll view inside horizontal scroll view in android. First horizontal scrolling views are working fine. When i am trying to scroll second horizontal scrolling views are not working.
my layout file
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="250dp"
android:layout_height="100dp"
app:cardCornerRadius="20dp"
app:cardBackgroundColor="#color/colorPrimary"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:scrollbars="horizontal"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4"></Button>
</LinearLayout>
</HorizontalScrollView>
</androidx.cardview.widget.CardView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
can anybody share your knowledge how to achieve this one?
You can create Custom class for nested scroll view for support horizontal smooth scroll.
public class HorizontalNestedScrollView extends NestedScrollView {
private int slop;
private float mInitialMotionX;
private float mInitialMotionY;
public HorizontalNestedScrollView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
ViewConfiguration config = ViewConfiguration.get(context);
slop = config.getScaledEdgeSlop();
}
public HorizontalNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public HorizontalNestedScrollView(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);
}
}
You can read about the issue and the simple fix here: https://code.google.com/p/android/issues/detail?id=194398
Use this class in place of your nestedscrollview in the xml file, and the child lists should intercept and handle the touch events properly.
Hope this will help you.
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 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
I am working on an Android application using C# in Xamarin. In my axml file, I have a LinearLayout with a certain ID that I have assigned a onTouchListener to. The problem is that as long as the buttons I have in my design are on top of the linearLayout, the onTouchListener is never triggered. If I set one of the buttons to invisible, then it works.
Is it possible to still have the onTouchListener react even though there are Controls in the way?
This is my axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:layout_marginBottom="0.0dp"
android:background="#android:color/holo_green_light"
android:soundEffectsEnabled="false" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:background="#android:color/holo_blue_light" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1">
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
android:background="#android:color/holo_red_light" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:background="#android:color/holo_orange_light" />
</LinearLayout>
</LinearLayout>
This is how I set the onTouchlistener for the linearLayout:
LinearLayout linLay = FindViewById<LinearLayout>(Resource.Id.LinearLayout1);
linLay.SetOnTouchListener(new MyTouchListener(this));
This is a custom touch listener class:
public class MyTouchListener : Java.Lang.Object, View.IOnTouchListener
{
Context activityContext;
public MyTouchListener(Context ac)
{
activityContext = ac;
}
public bool OnTouch(View v, MotionEvent e)
{
float x = 0.0f;
float y = 0.0f;
if (e.Action == MotionEventActions.Down)
{
// do stuff
x = e.GetX();
y = e.GetY();
Toast.MakeText(activityContext, x.ToString(), ToastLength.Short).Show();
return true;
}
//if (e.Action == MotionEventActions.Up)
//{
// // do other stuff
// return true;
//}
return true;
}
}
And finally, this is how I set up the button click listener:
Button button = FindViewById<Button>(Resource.Id.button1);
button.Click += (s,e) =>
{
//int i = count % s.Count;
//if(i > 28)
// Toast.MakeText(this, button.GetX().ToString(), ToastLength.Short).Show();
button.Text = string.Format("{0}", count++);
};
You have to intercept the touch event. In order to do that you have to subclass LinearLayout and override onInterceptTouchEvent():
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(getContext(), "onTouch", Toast.LENGTH_SHORT).show();
return true;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
}
Your xml:
<?xml version="1.0" encoding="utf-8"?>
<com.your.package.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:layout_marginBottom="0.0dp"
android:background="#android:color/holo_green_light"
android:soundEffectsEnabled="false" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:visibility="invisible"
android:background="#android:color/holo_blue_light" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1">
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
android:background="#android:color/holo_red_light" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:background="#android:color/holo_orange_light" />
</LinearLayout>
</com.your.package.MyLinearLayout>
Now any touch event on the layout will be handled by your custom MyLinearLayout.
You need to extend (inherent) the parent linear layout so that you can intercept touch events from the nested children layouts. Override these two methods to receive touches from the children. Then in your xml you will add "myCustomLinearLayout" or whatever you name the your custom linear layout class. It should be pretty similar in C# / Xamarin.
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true; // With this i tell my layout to consume all the touch events from its childs
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Log.d(TAG, String.format("ACTION_DOWN | x:%s y:%s",
break;
case MotionEvent.ACTION_MOVE:
//Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s",
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
I'm using gallery view inside scoll view but gallery view not working properly.
My custom GalleryView
<com.divum.Adapter.CustomGallery
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="none"
android:spacing="10dp" />
Adapter Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/top_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="2dp"
android:orientation="vertical"
android:weightSum="100">
<TextView
android:id="#+id/txt_title1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:padding="5dp"
android:textColor="#color/black"
android:textSize="14dp" />
<ImageView
android:id="#+id/image1"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_marginTop="2dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_marginLeft="2dp" />
<ScrollView
android:scrollHorizontally="false"
android:fadingEdge="none"
android:scrollbars="none"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txt_details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:padding="5dp"
android:textColor="#color/black"
android:textSize="14dp" />
</ScrollView>
</LinearLayout>
My problem is scrollview(vertically) working fine. The galleryview(horizontally) swiping is not working...
This is well known bug: ScrollView intercept both horizontal and vertical touch events. You can use this custom scroll view instead of standard one. This intercept only vertical touches:
public class VerticalScrollView extends ScrollView {
private float xDistance, yDistance, lastX, lastY;
public VerticalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
lastX = ev.getX();
lastY = ev.getY();
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);
}
}