在RecyclerView适配器中,如何修改活动内的onClickListener

时间:2017-11-07 07:39:12

标签: android callback android-recyclerview recycler-adapter

在我的主要活动(CreatePhotostoryActivity)中,我有这个方法:

public void showEditField(Moment oldMoment, final int index){
    listRecycler.setVisibility(View.GONE);
    editCaption.setVisibility(View.VISIBLE);
    saveCancelNavbar.setVisibility(View.VISIBLE);
    momentView.setVisibility(View.GONE);
    photoStoryNavbar.setVisibility(View.GONE);
    header.setVisibility(View.GONE);
    Picasso.with(getApplicationContext()).load(oldMoment.photoUri).into(displayPhoto);

    imageCaption.setText("");


    imageCaption.setText(oldMoment.caption);

    backIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            closeEditMoment();
        }
    });

    saveText.setText("Save Moment");

    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            updateMoment(imageCaption.getText().toString(), index);
        }
    });

    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            closeEditMoment();
        }
    });
}

我在我的适配器的showEditField方法中调用onBindViewHolder,如下所示:

final RecyclerView.ViewHolder holder= (RecyclerView.ViewHolder)momentViewHolder;
((MomentViewHolder) momentViewHolder).momentCaption.setText(moments.get(i).caption);
Picasso.with(mContext).load(moments.get(i).photoUri).into(((MomentViewHolder) momentViewHolder).displayPhoto);

((RecyclerView.ViewHolder) momentViewHolder).itemView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        if (mContext instanceof CreatePhotostoryActivity) {
            ((CreatePhotostoryActivity) mContext).showEditField(moments.get(position), position);

            // this is what I want to implement
            saveButton.setOnClickListener(new View.OnClickListener () {
                holder.momentCaption.setText(moments.get(position).caption);
                holder.momentPlaceholder.setVisibility(View.GONE);
                holder.momentCaption.setVisibility(View.VISIBLE);
            });

            cancelButton.setOnClickListener(new View.OnClickListener () {
                holder.momentPlaceholder.setVisibility(View.VISIBLE);
                holder.momentCaption.setVisibility(View.GONE);
            })
            notifyDataSetChanged();
        }
    }
});

当然,saveButton中的cancelButtononBindViewHolder onClickListeners无法正常工作,而这正是我尝试做的事情。我还想在showEditField方法中保留实际onClickListeners中调用的方法。基本上,我想"添加"来自适配器的saveButtoncancelButton onClickListeners,同时在活动中保留其原始功能。

修改:saveButtoncancelButton仅在活动中定义,但不在适配器中定义。

2 个答案:

答案 0 :(得分:0)

您可以创建一个RecyclerViewItemOnClickListener来实现RecyclerView.OnItemTouchListener 请查看以下代码段 -

import android.content.Context;  
import android.support.v7.widget.RecyclerView;  
import android.view.GestureDetector;  
import android.view.MotionEvent;  
import android.view.View;

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {  
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }
}

在代码中使用它非常简单。只需将此类的项目触摸侦听器添加到RecyclerView:

recyclerView.addOnItemTouchListener(  
    new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
      @Override public void onItemClick(View view, int position) {
        // TODO Handle item click
      }
    })
);

答案 1 :(得分:0)

你可以这样做

在适配器中创建一个界面

public interface customButtonListener {
     void onButtonClickListner(int position, View view,int pos);
}

和方法

public void setCustomButtonListner(customButtonListener listener) {
        this.customListner = listener;
    }

然后在适配器中声明一个变量

private customButtonListener customListner;

然后为适配器

中的按钮设置单击侦听器
 holder.saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (customListner != null) {
                    int pos=1;
                    customListner.onButtonClickListner(position, view,pos);
                }

            }
        });
 holder.cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (customListner != null) {
                    int pos=2;
                    customListner.onButtonClickListner(position, view, pos);
                }

            }
        });

然后在CreatePhotostoryActivity工具YourAdapter.customButtonListener中 然后执行以下操作:

yourAdapterobj.setCustomButtonListner(CreatePhotostoryActivity.this);
 @Override
    public void onButtonClickListner(int position, View view, int pos) {
       if(pos==1) {
          //code for save button

       }
        else if(pos==2){
         //code for cancel button

       }

    }