I implemented simple Listener to swipe actions on RelativeLayout. But I want to use my onClick method, and I don't know how to fire selecting Layout, I mean this Layout has
android:background="?android:attr/selectableItemBackground"
this background value to see that someone clicked on this layout.
This is my Listener
public interface SwipeListener<T> {
boolean swipeLeft(T itemData);
boolean swipeRight(T itemData);
void onClick(ViewHolder holder);
void onLongClick(ViewHolder holder);
}
I am getting reference to RelativeLayout form ViewHolder which I implemented, but I don't know how to fire selecting this layout in order to see ripple effect ?
Related
I have two rows of imageViews in a ScrollView. The imageViews all have onClickListener and when i want to scroll in this area it doesn't work. So i guess the Click listeners intercept the scrolling of the ScrollView. What's the best way to change this behaviour ?
My View hierarchy is like this:
<ScrollView>
<RelativeLayout>
<FrameLayout>
</FrameLayout>
<RelativeLayout>
</ScrollView>
in the FrameLayout i put a Fragment which has a LinearLayout in which i inflate other LinearLayouts like this:
productHolder.productLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
}
});
Maybe you could use an OnTouchListener instead of an OnClickListener.
In the OnTouchListener you can check if the action of the motion event was a click and consume the event (return true). Otherwise you can leave the event for other listeners (return false) to consume.
Note, that you might need to add some additional implementation for figuring out which image view was clicked, however without seeing your layout, it's hard to give you a hint how to do it.
I have an ImageView in my layout for the individual items of a list.
The ImageView's src is an XML file in the drawable folder that defines which images to use during the various states of pressing an item.
However, I've noticed when you click the list row (and not the ImageView itself) the selector assigned to the ImageView is activated. It doesn't actually hit the ImageView's onClick code, but the image toggles as if it has been clicked.
This is actually a desirable effect in some cases, but in this specific case it is not. Is there a way I can stop this from happening?
set android:duplicateParentState in child to true
I think you should read Cyril Mottier's blog. He has a post about this here.
In a word, you have to extends your Child views to Override the method setPressed(boolean) like this:
#Override
public void setPressed(boolean pressed) {
if (pressed && getParent() instanceof View && ((View) getParent()).isPressed()) {
return;
}
super.setPressed(pressed);
}
I'm using a custom view expanded from a XML layout in a horizontal scroll view as a sort of horizontal image list but I'm not sure how to get them to appear clickable/tappable (ie they highlight when tapped) or how to capture these events. I think I've tried setOnClickHandler without it working. I'm also trying to get a simple TextView to do the same. I've also tried setting android:clickable="true" but that hasn't helped either. Any ideas?
To take care of the visual feedback use an xml Selector, and set it as the View's background.
To handle click events use
mView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code here
}
});
I have a gallery. Each slide has a button inside.
I have a custom layout which with I'm inflating the slides.
Attached a click listener to the button and it works well, gallery also scrolls normally.
But when I press on the gallery outside of the button, the button is also pressed and turns blue. This doesn't trigger the onClickListener of the button. But it makes the button look like it was pressed and I don't want that.
Tried setting all listeners of the gallery to empty implementations but no effect...
Thanks in advance.
Edit: Attached an empty click listener to the custom layout and now the button doesn't become blue anymore but the gallery doesn't scroll because the event is consumed. Will see what can be done...
Found a workaround for this. It's horrible, but it works.
First of all, the part of Gallery's code responsible for my problem is this:
public boolean onDown(MotionEvent e) {
// Kill any existing fling/scroll
mFlingRunnable.stop(false);
// Get the item's view that was touched
mDownTouchPosition = pointToPosition((int) e.getX(), (int) e.getY());
if (mDownTouchPosition >= 0) {
mDownTouchView = getChildAt(mDownTouchPosition - mFirstPosition);
mDownTouchView.setPressed(true);
}
// Reset the multiple-scroll tracking state
mIsFirstScroll = true;
// Must return true to get matching events for this down event.
return true;
}
More exactly this line:
mDownTouchView.setPressed(true);
Here the layout of my slide is pressed and the default behaviour of setPressed of LinearLayout is dispatching it to all childrens so all children are pressed.
First I tried making a subclass of Gallery and override onDown. If I just returned false and nothing else, it worked but the slides got strange behaviour of jumping to the next slide when touched. That was because of this line:
mFlingRunnable.stop(false);
Which was not executed. Since this variable is private and related to everything else in Gallery class I didn't find a way to use it from the subclass. I tried also copying all the Gallery code, but it also didn't work, because it uses lot of things which only have package access... etc.
So I created a subclass of LinearLayout, which overrides onSetPressed:
public class LinearLayoutOnSetPressedDoNothing extends LinearLayout {
public LinearLayoutOnSetPressedDoNothing(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setPressed(boolean pressed) {
}
}
And use that in my layout instead of the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<com.test.LinearLayoutOnPressDoNothing
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- content -->
</com.test.LinearLayoutOnPressDoNothing>
And well, this works.
I have a ListView with custom list items. Each list item consists of 2 linear layouts one next to other.
LinearLayout 1 | LinearLayout 2 |
I've declared state list drawables for both LinearLayouts where in state_pressed I'm changing the background of the LinearLayout.
And here comes the issue - When the user taps on the LinearLayout2 only the background of LinearLayout2 should be changed, the background of LinearLayout1 should remain unchanged. On the other hand, when the user taps on LinearLayout1, only the background of LinearLayout1 should be changed. But now when the user taps on either of both LinearLayouts, both of them change their background.
The behaviour on tap on LinearLayout2 should be as onListItemClick() while when the user taps on LinearLayout1 a Dialog should appear (if this matters).
Any ideas how could I solve the background change issue? I've tried playing with focusable and clickable options. If i set clickable=true to both LinearLayouts, the children (TextViews) of LinearLayout2 do not change their colour (the TextViews should change their text colour).
Thank you!
This is because when using a list view you have to change some tags in XML to make the background transparent so that it will correctly work with your back ground.
Add this to your ListView XML code.
android:cacheColorHint="#00000000"
To set the ListView's background to transparent.
Well I think a single solution if you are using BaseAdapter as extends
First give unique Id to both those Layouts in you xml file and add
android:clickable="true"
In your method
public View getView(int position, View convertView, ViewGroup parent) {
when your are getting those views like
holder.layout1_name=(LinearLayout)view.findViewById(R.id.layout1);
holder.layout1_name.setOnClickListener( clicklayout1);
holder.layout2_name=(LinearLayout)view.findViewById(R.id.layout2);
holder.layout2_name.setOnClickListener( clicklayout2);
Add click listener on them
private OnClickListener clicklayout1 = new OnClickListener() {
public void onClick(View v) {
//Do what you want to do here
}
};
private OnClickListener clicklayout2 = new OnClickListener() {
public void onClick(View v) {
//Do what you want to do here
}
};
May be this may help you