FastAdapter icon indicating copy to clipboard operation
FastAdapter copied to clipboard

How to remove an item from FastItemAdapter without click event?

Open echthard opened this issue 9 years ago • 6 comments

How can I remove/update an item from the adapter which is initiated by something else than a click or touch? Should I use the CustomEventHook? Do you have an example of how to use the CustomEventHook?

Example: I now have this in the bindView() of the AbstractItem, because I do not know how to hook these events in the Fragment that has a FastItemAdapter with this AbstractItem.

    holder.editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
                // make async call
        }
    });

    holder.editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // make async call
                return false;
            }
            return false;
        }
    });

    private void onResponse(JSONObject response) {
                // update item and notify item changed in adapter, but how?
    }

echthard avatar Jan 26 '17 14:01 echthard

@echthard I am not 100% sure about your question. Do you want to remove an item / modify it in the list?

Or do you want a click listener on something else than the whole itemView?

mikepenz avatar Jan 26 '17 15:01 mikepenz

Do you want to remove an item / modify it in the list?

Yes

Or do you want a click listener on something else than the whole itemView?

No, I don't have a click event. I have an EditText per row in the list. In other words, I have an EditText per item in the adapter. Let's say it is a shopping list and the EditText holds the quantity. When the quantity is 0, the item should be removed. If the quantity is changed, a different view that also holds the quantity shoud be updated with the new value that comes from the EditText.

The EditText becomes visible when I click on an ImageView, when the EditText becomes invisible, then the event of updating the item will take place.

This event could be a Timer that fires after 3 seconds, but will ignore and restart if the soft keyboard is visible. The event can be when I scroll, then I will hide the EditText. Or the event can be when I click the DONE button on the soft keyboard.

echthard avatar Jan 26 '17 19:01 echthard

@echthard ok I see. It is a question in 2 parts :D

So first. Deleting an item is very easy. You just call the remove method on the ItemAdapter which hosts your elements: https://github.com/mikepenz/FastAdapter/blob/develop/library-core/src/main/java/com/mikepenz/fastadapter/adapters/ItemAdapter.java#L428 this will take care of the notifying for you.

as for the second part. Basically you have to think of the EventHooks to be normal listeners which you set on a view. But as the RecyclerView is reusing views this had to be solved a bit more clever, so we don't spawn many many unnecessary listeners in the bindView method. And to allow the programmer to have this logic in his activity / fragment I provide the EventHook

So for your case you will need to have a CustomEventHook and a ClickEventHook

As the sample item's event hooks do not need any reference to the activity they were implemented as static classes inside the item. But you can also have it as anonymous class in the activity. So that's how you provide a eventHook to your FAstAdapter; https://github.com/mikepenz/FastAdapter/blob/7bc4b381dde839eb230dbaf2c1e4ec61b6579d1c/app/src/main/java/com/mikepenz/fastadapter/app/CheckBoxSampleActivity.java#L69 And the EventHook itself looks like this: https://github.com/mikepenz/FastAdapter/blob/a060aca5105eda89d02cced1a7da6e20077dba2f/app/src/main/java/com/mikepenz/fastadapter/app/items/CheckBoxSampleItem.java#L143

It basically needs to know when it's bound to the view and what will happen in onClick

As there is no TextWatcherEventHook you will have to use the CustomEventHook class.

It basically works the same, you tell it in onBind when he should use the EventHook and in onEvent you have the view in this case which allows you to set the TextWatcher on it

mikepenz avatar Jan 26 '17 20:01 mikepenz

Well it's not really 2 parts, I understand how to update/remove an item in the adapter. I already have ClickEventHooks working, but I now have the other "event" handled in the bindView() of the item. There is obviously no adapter there in the item, so I can't update/remove items in the adapter.

I tried to implement the CustomEventHook, but I still don't understand what to do in the onBind.

Let my try to rephrase my example. The trigger/event is when a ViewGroup's visibility becomes View.GONE. Is it possible to create a CustomHookEvent on that?

FYI: The ViewGroup will be hidden when the Timer runs, when I scroll in the Recyclerview or when the DONE button on the soft keyboard will be tapped. So 3 ways to hide the ViewGroup.

You said

you tell it in onBind when he should use the EventHook

But how can I do that? In your ClickEventHook example it is like:

@Override
        public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
            if (viewHolder instanceof CheckBoxSampleItem.ViewHolder) {
                return ((CheckBoxSampleItem.ViewHolder) viewHolder).checkBox;
            }
            return null;
        }

But how would this look like for a CustomHookEvent? If i pass the ViewGroup instead of checkBox, then it's not mentioned that it should only occur when the ViewGroup becomes hidden.

Maybe it's not possible to hook an event on a view's visibilty change. Then I should probably hook the same event on the 3 ways that initiates that change.

echthard avatar Jan 27 '17 16:01 echthard

@echthard but some logic will adjust the visibility of this view somewhere. Something must tell the view next time it will be hidden. So there must exist some logic somewhere And this logic should already handle this. There is only a evenHook for UI interaction like, touch, click, longclick.

Everything else is triggered by something else and you can interact with it at this point.

So you wrote there are 3 ways this happens. so you have to write your logic and hook it into those 3 places.

There shouldn't be the logic anyways directly inside the Item, as this will cause huge troubles during scroll. You should do this outside the item, outside the adapter

mikepenz avatar Feb 19 '17 17:02 mikepenz

If this is still an issue report back to re-open

mikepenz avatar Feb 24 '17 15:02 mikepenz