简体   繁体   中英

How to handle SELECTION of RecyclerView.Adapter in Android?

I need to handle when an item is SELECTED in RecyclerView.Adapter. Now I have a touch listener on the view, but I need "less sensible trigger". If list is scrolled, I do not want touch to enable. But I would avoid any "clever fix". Is it any standardized solution for it, like didSelectRowAtIndexPath , in iOS in UITableView itself has a event handler mechanism. Anything in Android?

Not it is:

boardUserHolder.imageButton.setOnTouchListener(new View.OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {

Try to implement onClick listener in the ViewHolder instead of onTouchListener. And you can get selected position using getAdapterPosition() method.

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  public TextView mTextView;

  private ViewHolder(View itemView, TextView textView) {
        super(itemView);
        itemView.setOnClickListener(this);
        mTextView = textView;
  }

  @Override
  public void onClick(View view) {
        Toast.makeText(view.getContext(), "position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
  }

}

lets say you want to doSmt() when you click on the Image .

1- make an Interface in your Adapter class :

public interface myInterface = {
      void doSmt();
 };

2- make a global variable in your Adapter class:

private myInterface _myInterface ;

3- make a setter function for _myInterface inside your Adapter class :

public void setInterface(myInterface m){
   this._myInterface = m l 
}

4- now we have an interface with method doSmt() , to use this method when you click on your imageView , simply do this :

@Override
        public boolean onTouch(View v, MotionEvent event)
        { _myInterface.doSmt(); }

5- if you run the code now , nothing will happen because doSmt() is empty , now , make your Activity (which use your RecyclerView inside of it) implement the interface myInterface , and you will have to implement doSmt() too and fill it with your action you want to do .

6- in your Activity , use the setter method which you create in step 3

myAdapter.setInterface(this);

now if you run the code and click on the ImageView , the code in doSmt() will run .

public static class ViewHolder extends RecyclerView.ViewHolder implements AdapterView.OnClickListener {
public TextView mTextView;

private ViewHolder(View itemView, TextView textView) {
    super(itemView);
    itemView.setOnClickListener(this);
    mTextView = textView;
}

@Override
 public void onClick(View view) {
    Toast.makeText(view.getContext(), "position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM