在FirestoreRecyclerAdapter上使用界面实现onLongClick

时间:2019-09-07 12:35:47

标签: android android-recyclerview google-cloud-firestore firebaseui

我有一个自定义的FirestoreRecyclerAdapter,我需要通过“父”类的接口获取商品的Firestore ID。我想使用onLongClickListener在Recyclerview中选择多个项目并更改状态(选中/未选中)。我已经尝试在本教程的帮助下以与实现OnClickListener相同的方式来实现OnLongClickListener和接口:https://codinginflow.com/tutorials/android/firebaseui-firestorerecycleradapter/part-6-onitemclicklistener,但是它不起作用。

我应该在父类中实现它吗?

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;
                    }
                });
            }
        } }

2 个答案:

答案 0 :(得分:1)

您可以在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;
  }
}

OnClickListener

这将监听您的ItemView点击次数

编辑: 要将数据从适配器传递到父活动,您可以执行以下操作:

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
  }

}

然后在您的适配器中:

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;
  }

答案 1 :(得分:1)

@ xblaz3kx首先,您必须像这样定义接口。

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

然后在您的适配器中

   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);

            }
        } 
}

然后在您的活动中

public class AddToBBDActivity extends Activity implements LongKeyPressedEventListner {

 @Override
    public void longKeyPressed(int position){

// you will get the position of long press 
}

}