Rigsby
Rigsby

Reputation: 190

Hide view from Recycler view

I need suggestion, I am new in android, I am trying to implement emojis like facebook in my feed using this link FBReaction I am able to show the emojis on click of my Like button But I am not able to remove it from my view once the emojis got selected.

Here is my code in Adapter file to show emojis view

@Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_social, parent, false);
        RelativeLayout rl = (RelativeLayout)view.findViewById(R.id.btReaction);
        final CardView root = (CardView)view.findViewById(R.id.card_view);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ReactionView rvl = new ReactionView(mContext);
                root.addView(rvl);
            }
        });
        return new ViewHolder(view);
    }

ReactionView rvl = new ReactionView(mContext); root.addView(rvl);

Above line is used to show the emojis view.

RelativeLayout rl is my layout on click of this layout I am showing my emojis list. like below

enter image description here

same as above I have multiple feeds where I have like button, on click of those button I am able to show emojis view but couldn't remove it once it is selected.

I have a different class (ReactionView) to show the emojis List which is available in Git Repo

now My problem is how I remove the view once the emojis got selected.

Upvotes: 1

Views: 48

Answers (2)

Zain
Zain

Reputation: 40830

You need to modify the custom view ReactionView class a little bit, if you notice the onTouchEvent() of that class it registers different MotionEvent's, and particularity, when the MotionEvent.ACTION_UP event is triggered, this means that the user has left their finger of the ReactionView emojis, at this time you need to remove the ReactionView from your layout.

So as you can remove your view when the onDeselect() is over, so to that, there is a couple of methods:

Method 1:

Remove the focus from the ReactionView, and implement OnFocusChangeListener on the ReactionView

public class ReactionView extends View {

    ... 

    private void onDeselect() {
        deselectAnimation.prepare();
        startAnimation(deselectAnimation);

        // Hide the view and remove the focus from it.
        setVisibility(GONE);
        setFocusableInTouchMode(false);
        setFocusable(false);
        setFocusableInTouchMode(true);

    }

In activity

rvl.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus)
            root.removeView(rvl);
    }
});

Method 2:

Create a listener to the ReactionView, trigger its callback in onDeselect() and implement it on your activity

public class ReactionView extends View {

    ... 

    public interface DismissListener {
        void onDismiss();
    }

    public void setOnDismissListener(DismissListener onDismissListener) {
        mOnDismissListener = onDismissListener;
    }

    DismissListener mOnDismissListener;

    private void onDeselect() {
        deselectAnimation.prepare();
        startAnimation(deselectAnimation);

        if (mOnDismissListener != null)
            mOnDismissListener.onDismiss();

    }

In activity

rvl.setOnDismissListener(new ReactionView.DismissListener() {
    @Override
    public void onDismiss(int selectedIndex) {
        root.removeView(rvl);
    }
});

Upvotes: 1

Miruna Radu
Miruna Radu

Reputation: 452

You can make this with a listener.

In your ReactionView class add :

interface ReactionListener{

    public void onReactionClicked();
}

Then add to the constructor of ReactionView (Context context, ReactionListener: listener). I assume you want to hide this view when the user is clicking on an emoji, so where you set click listener for that, also call listener.onReactionClicked()

Then in your adapter create the listener object and remove the view:

private ReactionListener reactionListener = new ReactionView.ReactionListener(){
    @override 
    public void onReactionClicked(){
       root.removeView(rvl);
    }

Upvotes: 0

Related Questions