How to detect swipe gesture AND button click on ListView - android

I have an ExpandableListView and I am using a SwipeDetector to know when a row of this list is swiped.
The implementation of this dectetor comes from this post.
Everything works fine.
Then, I have added a Button on each row and I want to be able to know:
When the button is cliked
When the row is swiped
This is the layout used for each (except for headers) row of the list:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="wrap_content" >
<Button
android:text="Button"
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Now, the problem is that the swipe gesture is no more detected because the Button catches the event. Since they are lots of threads about this subject I tried several things but nothing works (at least I did not manage to make it work). Here is the different things I tried:
Set the android:focusable property to false on the Button
Set the android:descendantFocusability property to blocksDescendants on the RelativeLayout
Follow this post and override the onInterceptTouchEvent
Last points:
I can't set the SwipeDetector directly on the Button instead of the ListView's item because in some cases there will be no button (or many).
I can't use android:clickable="false" on the Button because I need to implement the onClick method.

Related

First click on a View is not working in Android

I have a LinearLayout which needs to be clickable inside a NestedScrollView inside a CoordinatorLayout and almost all the time the first time I click it it simply doesn't work, I must click it another time.
Clickable LinearLayout:
<LinearLayout
android:id="#+id/qr_code_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
tools:ignore="UseCompoundDrawables">
This is the NestedScrollView container where
the clickable item resides.
EDIT: Okay this is very weird, if I wait some seconds before trying to click the View, it works the first time. It only fails if I click it just after scrolling to the bottom.
I had the same problem. Inside the NestedScollView there was a RelativeLayout element needed to be clicked. As long I didn't scroll to the very bottom, the element could be clicked with a single click. If I would reach the bottom I needed two clicks, except if I waited a number of seconds.
The problem proved to be on the default layout_behavior of the AppBarLayout. I used the custom one suggested in the post below and the problem was solved.
onClick method not working properly after NestedScrollView scrolled

OnClick Listener not working for buttons which exists in a linearlayout for which the alignParentBottom is true

I am having an activity and want to create a like button at the end of the layout, So I created a layout file and in a LinearLayout I have set it's layout_alignParentBottom property to true and created button for Likes in it. Now I am including this layout file in some other layout file but when I am applying onClickListener to the button, it does nothing.
When I remove this layout_alignParentBottom from the LinearLayout properties, then OnclickListener start working.
Can you please help me here to resolve this issue?
Some other widget might be coming in its way. if there is something above that button, it wont take clickListener.
For Ex. if there is a list in that layout too,
<Button
android:layout_alignParentBottom="true"
android:layout_marginLeft="#dimen/viewSpace1"
android:layout_marginRight="#dimen/viewSpace1"
android:layout_marginBottom="#dimen/viewSpace1"
android:layout_width="fill_parent"
android:layout_height="#dimen/headerHeight_small"
android:id="#+id/btnShare"
style="#style/ButtonLogin"
android:text="Next" />
<ListView
android:layout_above="#id/btnShare"
android:id="#+id/list"
android:layout_below="#id/layoutHeader"
android:layout_marginLeft="#dimen/viewSpace3"
android:layout_marginRight="#dimen/viewSpace3"
android:layout_marginBottom="#dimen/viewSpace3"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
so your share button stays safe for clickability.
I have kept the list above btnShare. Just for my safety if it overlaps the button.If there is still problem, post your code so exact problem can be pin pointed.

Android: How to make Grid view with clickable grid items and nested views (buttons,Image) by remote(For Android TV)

How to make Grid view with click able grid items and nested views (buttons,Image) by remote(For Android TV)
I just want to click able image and button in grid view item should add to play my list if I navigate on button by Remote navigation not by touch
Thank you!!!
How to navigate through d pad in grid view sub items if grid view item have two sub items image button and another button
It is no difference between touch screen or remote control for methods like onClickListener or etc.
For remote control, you need to focus on the element first, than you can click the element.
I have not use the gridView on TV before, but I have used the recyclerView and scrollView. I think they have same situation.
I've try the default simpleAdapter to create the gridView, and it's no problem with the nested views that I can focus and click each grid items after I set the onItemClickListener to the gridView.
I guest your problem is that the grid item conflicts with the button in the grid item.
To avoid this situation, you need to set android:descendantFocusability="blocksDescendants" for the container of your custom grid item view. It will block the items statement in that parent layout.
If you want the button change in the different statement, you can give the button this parameter: android:duplicateParentState="true".
That will let the button follow it's parent's statement.
By the way, don't forget to set some changes to different statement for the grid items (background changes or etc), or you will not know which grid item you have focused :P.
Hope this will help you :)
PS. The following is the sample code for the custom grid item layout. Maybe the example can help you to understand what I mentioned easily.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:duplicateParentState="true"/>
</LinearLayout>
2015.01.06 23:11 update:
How to navigate through d pad in grid view sub items if grid view item
have two sub items image button and another button
For this case you should not use OnItemClickListener. Instead, you should make your grid item unfocusable and unclickable, and than set OnClickListener for both two buttons.
You can set the parameters in your customize adapter for the gridView. In getView method you can set each of the grid item view just like convertView.focusable(false) and convertView.clickable(false).
After that, you can set the click listener and give the method you want to do for the buttons in adapter.
Don't care about the D-pad action. Actually, D-pad will work automatically if there are elements can be focused.
The key point for this question is the conflict between grid item and its child elements. You could have just one part to be focused: grid item (the parent view), or the buttons (the child views).

a textview in a listview' s row cannot click after setting descendantFocusability="blocksDescendants"

I write a customized item layout for a listview. The layout has many widgets and some will have its own clicklistener.
When I click the row, sometimes the listview' s onListItemClick work, but sometimes not.
After I spent some time searching, I find a way, setting android:descendantFocusability="blocksDescendants" in the layout' s root. It works, but only one textview cannot work,(the clickable, fucusable attr have been tried). In the list' s adapter, I set the textview' s onClickListener, it works. But that' s not nature, does anyone know how to solve it?
btw, is there a way to distinguish diffderent widget' click? Now, no matter what I click(except that textview), the whole row' s background selector got changed.
many thanks!
// my list row layout root
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:descendantFocusability="blocksDescendants"
android:padding="#dimen/medium"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
// the awful textview
<TextView
android:id="#+id/text"
android:textColor="#android:color/secondary_text_light"
android:textColorLink="#android:color/holo_blue_dark"
android:layout_marginTop="#dimen/small"
android:textAppearance="#android:style/TextAppearance.DeviceDefault"
android:layout_below="#id/nick"
android:layout_alignLeft="#id/nick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
update
the textview uses Linkify to provide some links functionalities.
After spenting some time trying, I solved both.
the first. if your customized listview' s layout has a textview which has been set autolink or Linkify in the code, the click event in the textview won' t affect the list' s row. You have to extends your own TextView and override onTouchEvent method. please see it here
the second. just set in the customized listview' s root node: android:descendantFocusability="blocksDescendants", and then, in your widget which will have its own onClickListener, set android:clickable="true", it works for me.
hope it useful for those you encounter the same:-)

Adding CheckBox to ListView row view makes the row not clickable

I am trying to create a ListView similar to the Gmail app, where each row has a CheckBox on the left to facilitate multiple selection and actions via the contextual action bar, while at the same time, allow clicking each row to transition to another activity.
Originally, before adding the CheckBox the rows would indicate their selection with a light blue background color from the Holo theme I'm using and call onListItemClick in my ListFragment when clicked and onItemLongClick in my OnItemLongClickListener when long clicked.
When I add the CheckBox to my layout .xml file for the row view, the background no longer changes color and I no longer receive click or long click events. If I add android:longClickable="true" to the top ViewGroup in my view .xml then I start getting long click events again. But android:clickable="true" does not get me click events. And neither allows the background blue selection indication to return. Only the CheckBox itself provides visual touch indication with a blue background when touched.
What do I need to do to get the normal visual selection indication as well as click events on the ListView rows with the CheckBox in the view?
Here's the relevant portion of my view .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:longClickable="true"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingRight="12dp"
android:paddingTop="5dp" >
...
</LinearLayout>
</LinearLayout>
I wrote a blog post on this subject awhile back that you may find helpful. The problem is, in fact, that adding focusable elements (like CheckBox or Button) disables the ability to click the overall list item and their focusability has to be disabled. You should not add any clickable flags to the main layout either.
While adding checkbox in CAB please make it focusable="false", this will solved the problem of only one time called onItemCheckedStateChanged.

Categories

Resources