0

I have a RecyclerView in place. I have a longClick listener on some items in that RecyclerView. If this listener is called/activated, it should then listen for a click on anything but the item that had been longClicked. How should I go about this?

I've tried setting an onClickListener on the RecyclerView itself, but that didn't work.

Addition: Original problem

The longClick on an item is used to show an alternative view, similar to how notifications work in Android 6. I want to hide this alternative view again if something else is clicked.

Timmiej93
  • 1,328
  • 1
  • 16
  • 35
  • Please add relevant code. – Shadab Ansari May 02 '16 at 22:58
  • I don't see how code is relevant in this question. I'm not asking about an error my code is throwing, I'm asking for ideas on how to tackle this. If you need to see code, you'll need to tell me what you'll need. – Timmiej93 May 02 '16 at 23:00
  • I am not sure if this is even possible. An item receiving clicks for other items. And even if it is, it seems like a bad approach. I am sure whatever you are trying to do using this approach can be done in a better and easier way. If you can state your original problem (why you need to do this) then may be an approach can be decided. – varunkr May 02 '16 at 23:34
  • @varunkr I've added a description of the chain of events – Timmiej93 May 03 '16 at 00:01
  • Something else is clicked? You mean anywhere on screen or another item in list? Btw this seems like the problem to which I gave the answer. You basically want to hide this alternative view, so what you need to do is to edit and update your recyclerView item to the previous state !! – varunkr May 03 '16 at 00:07
  • For now either would be good. But again, you are talking about hiding or showing the view, but that's simply not what this question is about. I just want to listen for clicks on anything **but** the current item. All the hiding and showing I can do myself just fine. – Timmiej93 May 03 '16 at 00:09
  • Then just follow the updating recyclerView approach. When an item is clicked update that item to show the alternative view. You can keep track of thi position in some local variable. Now when another item is clicked edit the previous item using the edit(pos, item) method. Is there a problem with this approach ? – varunkr May 03 '16 at 00:12
  • You mean like keeping a variable with the index of the changed view, then registering a clicklistener to **every*" view, and check if the view that had been clicked is the current one, and if not, hide the current view? – Timmiej93 May 03 '16 at 00:14
  • If you just need to take care of the clicks on recyclerView in that case, yes. Why not? If you need to handle click on anywhere on the screen then you will probably have to approach it like this http://stackoverflow.com/questions/4165414/how-to-hide-soft-keyboard-on-android-after-clicking-outside-edittext I used it in my app recently. – varunkr May 03 '16 at 00:24
  • I'm worried about bogging down the app with that much listeners. It's probably no big deal, but I always prefer more elegant methods. Also, the link you provided gave me the idea to use `onInterceptTouchEvent()`, but that's even harder to do I think, since I don't have `viewGroup`'s set up. – Timmiej93 May 03 '16 at 10:43

1 Answers1

0

One way is to have a method editItem in your recyclerView adapter which basically edits an item. When another item is clicked just edit the previous item

public void editItem(int position, Item item) {
        itemList.remove(position);     // Removing the changed item
        itemList.add(position, item);  // Adding back your old item
        notifyItemChanged(position);   // Notifying
    }
varunkr
  • 5,364
  • 11
  • 50
  • 99
  • I think you misunderstood the question. I'm not asking how to change the item, I'm asking how I should listen for clicks on **anything but** the current item. – Timmiej93 May 02 '16 at 23:15
  • Oh, you mean you want that items which are changed should not receive clicks at all ? – varunkr May 02 '16 at 23:16
  • Well they should, but those are separate. The changed item is working fine, I just want to revert back to the original if something is clicked that is **not** the current item. – Timmiej93 May 02 '16 at 23:17
  • "I just want to revert back to the original if something is clicked that is not the current item" doesn't this mean that you want to change the changed item? Sorry but your question is a bit unclear. – varunkr May 02 '16 at 23:21
  • I have to agree on that, but combined with the title, it is not. I'll change it nevertheless. – Timmiej93 May 02 '16 at 23:22