xblaz3kx
xblaz3kx

Reputation: 371

Implementing onLongClick with interface on FirestoreRecyclerAdapter

I have a custom FirestoreRecyclerAdapter and I need to get the item's Firestore ID through an interface to the "parent" class. I want to select multiple items in Recyclerview using onLongClickListener and change states (selected/unselected). I've tried implementing the OnLongClickListener and interface the same way I already implemented the OnClickListener with the help of this tutorial: https://codinginflow.com/tutorials/android/firebaseui-firestorerecycleradapter/part-6-onitemclicklistener but It doesn't work.

Should I implement this in the parent class?

public class Adapter extends FirestoreRecyclerAdapter<someClass, Adapter.Holder> {


        public Adapter(@NonNull FirestoreRecyclerOptions<someClass> options) {
            super(options);
        }

        @Override
        protected void onBindViewHolder(@NonNull Adapter.Holder Holder, int i, @NonNull someClass classVar) {
            Holder.content.setText(classVar.getContent());
            Holder.title.setText(classVar.getTitle());
        }

        publicAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.some_layout, parent, false);
            return new Adapter.Holder(v);
        }


        class Holder extends RecyclerView.ViewHolder {
            TextView Title, Content;

            public Holder(@NonNull final View itemView) {
                super(itemView);
                Title = itemView.findViewById(R.id.Title);
                Content = itemView.findViewById(R.id.Content);
                itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        getAdapterPosition();
                        return false;
                    }
                });
            }
        } }

Upvotes: 0

Views: 285

Answers (2)

Muazzam A.
Muazzam A.

Reputation: 645

@xblaz3kx First you have to define your interface like this.

public interface LongKeyPressedEventListner {
    void longKeyPressed(int position);
}

Then in your adapter

   public class Adapter extends FirestoreRecyclerAdapter<someClass, Adapter.Holder> {

        LongKeyPressedEventListner longKeyPressedEventListner

        public Adapter(@NonNull FirestoreRecyclerOptions<someClass> options,LongKeyPressedEventListner longKeyPressedEventListner) {
            this.longKeyPressedEventListner = longKeyPressedEventListner;
            super(options);
        }

 @Override
        protected void onBindViewHolder(@NonNull Adapter.Holder Holder, int i, @NonNull someClass classVar) {
            Holder.content.setText(classVar.getContent());
            Holder.title.setText(classVar.getTitle());

            Holder.layout.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                         int pos = getAdapterPosition();
                         longKeyPressedEventListner.longKeyPressed(pos); // call interface method
                        return false;
                    }
                });

        }

        publicAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.some_layout, parent, false);
            return new Adapter.Holder(v);
        }


class Holder extends RecyclerView.ViewHolder {
            TextView Title, Content;
            RelativeLayout layout;

            public Holder(@NonNull final View itemView) {
                super(itemView);
                Title = itemView.findViewById(R.id.Title);
                Content = itemView.findViewById(R.id.Content);
                layout = (RelativeLayout)itemView.findViewById(R.id.main_layout);

            }
        } 
}

Then in your Activity

public class AddToBBDActivity extends Activity implements LongKeyPressedEventListner {

 @Override
    public void longKeyPressed(int position){

// you will get the position of long press 
}

}

Upvotes: 1

Shahin
Shahin

Reputation: 397

You can implement click listeners on your viewHolder :

class Holder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
  TextView Title, Content;

  public Holder(@NonNull final View itemView) {
    super(itemView);
    Title = itemView.findViewById(R.id.Title);
    Content = itemView.findViewById(R.id.Content);





    itemView.setOnLongClickListener(this);
  }

  @Override
  public void onLongClick(View v) {
    getAdapterPosition();
    return false;
  }
}

Or OnClickListener

This will listen to your ItemView clicks

EDIT: to pass data from Adapter to Parent Activity you can do something like below:

public class MyActivity extends AppCompatActivity {

  private static MyActivity instance;
  public static MyActivity getInstance() {
    return instance;
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_request_now);
    instance = this;
  }
  
  public void doSomething(int passingValue){
  // use passingValue
  }

}

And then in your Adapter :

public Holder(@NonNull final View itemView) {
    super(itemView);
    Title = itemView.findViewById(R.id.Title);
    Content = itemView.findViewById(R.id.Content);
    itemView.setOnLongClickListener(this);
  }

  @Override
  public void onLongClick(View v) {
    
    MyActivity.getInstance().doSomething(getAdapterPosition());
    // just Make sure your activity isn't somehow Null
    
    return false;
  }

Upvotes: 1

Related Questions