-4

I have a recycler view which contain some items in list and in individual items contain a button. i want to perform some action on button click on particular item i implemented Recycler view adapter but don't want to implement code in button click in adapter i want to perform action in fragmenr which contain recylcer view impelementation. How do i do that

code for adapter

 public class ViewHolder extends RecyclerView.ViewHolder {
    TextView m_premiumText, m_getDealText;

    public ViewHolder(View itemView) {
        super(itemView);
        m_premiumText = (TextView) itemView.findViewById(R.id.tv_premiun_deals);
        m_getDealText = (TextView) itemView.findViewById(R.id.tv_get_deals);

        m_getDealText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"clicked"+getAdapterPosition(),Toast.LENGTH_LONG).show();
            }
        });
    }
}

code for recycler view

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mContext = getActivity();
    main = inflater.inflate(R.layout.reward_premium_deals, container, false);
    findViewById();
    initControls();
    return main;
}


public void findViewById() {
    m_premiumList = (RecyclerView) main.findViewById(R.id.premium_list);
    m_layoutManager = new LinearLayoutManager(mContext);
    m_premiumList.setLayoutManager(m_layoutManager);

}

public void initControls() {
    premiumNames = getResources().getStringArray(R.array.premium_deals_title);
    m_premiumDealsAdapter = new CPremiumDealsAdapter(mContext, premiumNames);
    m_premiumList.setAdapter(m_premiumDealsAdapter);

    /*here i want button click action*/


}
niraj kumar
  • 103
  • 2
  • 2
  • 9
  • There are million resources already regarding this. If you have specific question regarding the implementation, create that. – Nabin Apr 26 '17 at 04:20
  • http://stackoverflow.com/questions/24885223/why-doesnt-recyclerview-have-onitemclicklistener-and-how-recyclerview-is-dif – Nabin Apr 26 '17 at 04:20
  • http://stackoverflow.com/questions/24471109/recyclerview-onclick – Nabin Apr 26 '17 at 04:21
  • 1
    Possible duplicate of [RecyclerView onClick](http://stackoverflow.com/questions/24471109/recyclerview-onclick) – Nabin Apr 26 '17 at 04:21

2 Answers2

1

in these cases you must use an listener or callback and send it to your recycler view adapter through constructor.

firstly create a new interface to use as the callback. then implement that interface in your fragment and then send your fragment (this) as the listener to your adapter. then you can call that action from your recycler view instance.

Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
0

From this Reference, I have implemented in recycler view item in my activity class not in fragment.

  1. Interface Class:

    public interface RecyclerViewClickListener {
        void onClick(View view, int position);
        void onLongClick(View view, int position);
    }
    
  2. RecyclerViewTouchListener Class:

    public class RecyclerViewTouchListener implements RecyclerView.OnItemTouchListener {
    private GestureDetector gestureDetector;
    private RecyclerViewClickListener recyclerViewClickListener;
    
    public RecyclerViewTouchListener(Context context, final RecyclerView recyclerView, final RecyclerViewClickListener recyclerViewClickListener1) {
        recyclerViewClickListener = recyclerViewClickListener1;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
    
            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && recyclerViewClickListener1 != null){
                    recyclerViewClickListener.onLongClick(child, recyclerView.getChildLayoutPosition(child));
                }
            }
        });
    }
    
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && recyclerViewClickListener != null && gestureDetector.onTouchEvent(e)){
            recyclerViewClickListener.onClick(child, rv.getChildLayoutPosition(child));
        }
        return false;
    }
    
    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    
    }
    
    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    
    }
    }
    
  3. Now, inside fragment or activity class where you have to setup you recycler view activties:

    recyclerView.setAdapter(your_adapter_class_object);
    recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(this, recyclerView, new RecyclerViewClickListener() {
        @Override
        public void onClick(View view, int position) {
            Loge("Item","Click");
        }
    
        @Override
        public void onLongClick(View view, int position) {
            Loge("Item","Long Click");
        }
    }));
    
Community
  • 1
  • 1
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53