Skip to content Skip to sidebar Skip to footer

Passing Values From Recycler Adapter To Fragment In Android

I've a recycler adapter where I've added setOnClickListener for items in adapter. Code is as given below: @Override public void onBindViewHolder(final FiltersAdapter.MyViewHo

Solution 1:

make interface to handle click event into recyclerview .. used below code for handling click event.

onItemClickListner onItemClickListner;

publicvoidsetOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

publicinterface onItemClickListner{
    voidonClick(String str);//pass your object types.
}
   @OverridepublicvoidonBindViewHolder(ItemViewHolder holder, int position) {
    String data=mStringList.get(position); // if you pass object of class then create that class object.
    holder.textView.setText(data);
    holder.itemView.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View view) {
            onItemClickListner.onClick(data);
        }
    });
 }

then after when you bind adapter into RecyclerView call below code..

mRvData.setLayoutManager(newLinearLayoutManager(this));
mRvData.setAdapter(recyclerViewAdpater);
recyclerViewAdpater.notifyDataSetChanged();
recyclerViewAdpater.setOnItemClickListner(newRecyclerViewAdpater.onItemClickListner() {
    @OverridepublicvoidonClick(String str) {
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
    }
});

Solution 2:

Create an interface in your adapter

publicinterfaceOnItemClickListener {
  voidonItemClicked(int position, Object object);
}

in your adapter call

Adapter adapter = newAdapter(context, list, newAdapter.OnItemClickListener() {
            @OverridepublicvoidonItemClicked(int position, Objectobject) {
                  // Handle Object of list item here
            }
        });

on your adapter

private OnItemClickListener onItemClickListener; // Global scope 

in constructor call:

this.onItemClickListener = onItemClickListener;

on your item clicked event :

itemView.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View view) {
      onItemClickListener.onItemClicked(position, _list.get(position));
    }
});

Solution 3:

Please use an interface like below, pass it in the adapter's constructor, and than use it in your activity / Fragment

publicMyAdapter(Context context, List<Object> data, MyAdapterListener myAdapterListener) {
             this.context = context;
             this.data = data;
              this.myAdapterListener = myAdapterListener;
        }

        publicinterfaceMyAdapterListener {

    voidonContainerClick(View view, int position); 
    }

     }

    publicclassViewHolderItemextendsRecyclerView.ViewHolder implementsView.OnClickListener {
        public LinearLayout container;

        public ImageView poster;

        publicViewHolderItem(View v) {
            super(v);

            container = (LinearLayout) v.findViewById(R.id.container);

            poster = (ImageView) v.findViewById(R.id.poster);

            container =setOnClickListener(this); /// this line must include
        }

        @OverridepublicvoidonClick(View v) {
            intid= v.getId();

            if (id == R.id.container) {
               if (myAdapterListener != null) {
    myAdapterListener.onContainerClick(v,getAdapterPosition());
                }
            }
        }
    }

Use it like this in your fragment/Activity

mAdapter = newMyAdapter(getApplicationContext() , data, newMyAdapter.MyAdapterListener() {
                    @OverridepublicvoidonContainerClick(View v, int position) {
                        Log.d(TAG, "iconTextViewOnClick at position "+position);
                    }

                });
mRecycler.setAdapter(mAdapter);

Solution 4:

I thing this is work for you

Add in you adapter

publicinterfaceClickEvent {
        voidclickEventItem(int position,String value);
    }

    ClickEvent clickevent;


    publicvoidsetClickEvent(ClickEvent event) {
        this.clickevent = event;
    }

   itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        publicvoidonClick(View view) {
            clickevent.clickEventItem(position, list.get(position));
        }
    });

in your fragment set interface

adapter.setClickEvent(your class context);

implement ClickEvent intereface and generate @Override method

@OverridepublicvoidclickEventItem(int position,String value) {
       //write your code here
  }

Post a Comment for "Passing Values From Recycler Adapter To Fragment In Android"