在回收站视图中处理Click Button

时间:2016-06-30 12:00:49

标签: android button android-recyclerview

我有OnItemTouchListener的回收者视图,这个回收者的每一行都有一个带onclick事件的按钮。

我想点击按钮事件后工作,但OnItemTouchListener工作。

请帮忙。我的代码。

public class RecyclerViewTouchListener  implements RecyclerView.OnItemTouchListener {


private GestureDetector gestureDetector;
private ClickListener clickListener;

public RecyclerViewTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
    this.clickListener=clickListener;
    gestureDetector=new GestureDetector(new GestureDetector.SimpleOnGestureListener(){

        @SuppressWarnings("deprecation")
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (child!=null && clickListener!=null){
                clickListener.OnClick(child,recyclerView.getChildPosition(child));
            }
            return true;
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    View child = rv.findChildViewUnder(e.getX(), e.getY());
    if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
        //noinspection deprecation
        clickListener.OnClick(child, rv.getChildPosition(child));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}


public static interface ClickListener {
    void OnClick(View view,int position);

}

}

此视图中的代码

deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            callbacks.removePosition(getAdapterPosition());
        }
    });

1 个答案:

答案 0 :(得分:6)

对不起我的直率,但首先删除此代码。然后,您需要创建自己的RecyclerAdapter,它将包含以下代码片段:

npm install -g graceful-fs graceful-fs@latest

然后在使用RecyclerView(Activity,Fragment,自定义视图,自定义Wrapper for View)的地方,您应该执行以下操作:

View.OnClickListener clickListener;

public void setOnItemClickListener(View.OnClickListener clickListener) {
    this.clickListener = clickListener;
}

@Override
public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (onClickListener != null) {
        holder.itemView.setOnClickListener(clickListener);
    }
}

关于点击任何项目内的按钮的问题旁边。 定义您自己的Holder,为您项目中的View组件保留参考:

List<SomeItem> someItems = getSomeItems();//Receive items for adapter from some place
MyRecyclerAdapter adapter = new MyRecyclerAdapter(someItems);
adapter.setOnItemClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = orderListView.getChildAdapterPosition(v);
            SomeItem item = someItems.get(position);
            Log.v("CLICKED", "Clicking on item(" + position + ", " + item + ")");
        }
    });
recyclerView.setAdapter(adapter);

然后定义自己的界面以定义删除按钮的点击次数。例如:

public static class MyHolder extends RecyclerView.ViewHolder {
    private final Button deleteButton;

    public MyHolder(View itemView) {
        super(itemView);
        deleteButton = (Button) itemView.findViewById(R.id.delete_button);
    }

    public Button getDeleteButton() {
        return deleteButton;
    }
}

然后将您的界面附加到适配器:

public interface OnDeleteButtonItemClickListener {
    void onDeleteIsClick(View button, int position); 
}

然后更新您使用RecyclerView的地方:

View.OnClickListener clickListener;
OnDeleteButtonItemClickListener deleteButtonListener;

public void setOnItemClickListener(View.OnClickListener clickListener) {
    this.clickListener = clickListener;
}

public void setDeleteButtonListener(OnDeleteButtonItemClickListener  deleteButtonListener) {
    this.deleteButtonListener= deleteButtonListener;
}

@Override
public final void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if (clickListener!= null) {
        holder.itemView.setOnClickListener(clickListener);
    }

    if (deleteButtonListener!= null) {
        MyHolder myHolder = (MyHolder) holder;
        myHolder.getDeleteButton().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteButtonListener.onDeleteIsClick(v, position)
        }
    });;
    }
}