如何在android中的recyclerview中制作点击监听器?

时间:2017-04-26 04:18:07

标签: android

我有一个回收站视图,其中包含列表中的一些项目,并且单个项目中包含一个按钮。我想对按钮单击特定项目执行一些操作我实现了Recycler视图适配器但不想在按钮单击适配器中实现代码我想在fragmenr中执行包含recylcer视图实例的操作。我该怎么做

适配器代码

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

回收商视图的代码

 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*/


}

2 个答案:

答案 0 :(得分:1)

在这些情况下,您必须使用侦听器或回调并通过构造函数将其发送到您的回收器视图适配器。

首先创建一个新接口用作回调。然后在片段中实现该接口,然后将片段(this)作为侦听器发送到适配器。然后,您可以从回收站视图实例中调用该操作。

答案 1 :(得分:0)

通过此Reference,我已在activity课程中的回收者视图项目中实施,而非fragment

  1. 接口类:

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

    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. 现在,在fragmentactivity类内,您必须设置回收站视图激活:

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