选择了RecyclerView项目以及如何禁用Touch事件

时间:2016-03-08 00:32:37

标签: android touch android-recyclerview adapter

关于如何进行简单的RecyclerView点击事件处理程序,有各种各样的材料,但我无法找到这个基本所需功能的帮助。我希望能够long tap对某个项目进行选择(请参阅我如何更改高程和颜色以使其显示为已选中)。然后,如果在我的Fragment我想根据点击的RecyclerView删除按钮从Toolbar删除一个项目,那么如果Google建议您这样做,我该怎么做?我们不应该在班级之外的适配器上保留一个位置实例。

我在23.2.0支持库中实现了BottomSheet,它显示在我的RecyclerView上方,但点击经过,就像我控制RecyclerView本身一样我还有一个BottomSheet。我怎样才能真正禁用'它?

我已尝试:使用interface,将clickable=true设置为顶部布局视图。

我的RecyclerAdapter

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.EventViewHolder> {
private List<Event> eventsList;
private int selected_position = -1;
private RecyclerView recyclerView;



Typeface font;
Typeface fontBold;

public RecyclerAdapter(List<Event> eventsList, RecyclerView recyclerView) {
    this.eventsList = eventsList;
    this.recyclerView = recyclerView;
}

@Override
public RecyclerAdapter.EventViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.
            from(parent.getContext()).
            inflate(R.layout.events_cardview, parent, false);

    font = Typeface.createFromAsset(itemView.getContext().getAssets(), "ubuntu-l.ttf");
    fontBold = Typeface.createFromAsset(itemView.getContext().getAssets(), "ubuntu-b.ttf");

    return new EventViewHolder(itemView);
}


@Override
public void onBindViewHolder(RecyclerAdapter.EventViewHolder eventViewHolder,final int position) {


    if(selected_position == position){
        // Here I am just highlighting the background
        eventViewHolder.cardView.setCardBackgroundColor(ContextCompat.getColor(MyApplication.getAppContext(), R.color.tealfifty));
        if (Build.VERSION.SDK_INT >= 21) {
            eventViewHolder.itemView.setElevation(10f);
            eventViewHolder.itemView.setTranslationZ(10f);
        }
    }else{
        if (Build.VERSION.SDK_INT >= 21) {
            eventViewHolder.itemView.setElevation(2f);
            eventViewHolder.itemView.setTranslationZ(2f);
        }
        eventViewHolder.cardView.setCardBackgroundColor(ContextCompat.getColor(MyApplication.getAppContext(), R.color.white));
    }

    eventViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();

            // Updating old as well as new positions
            notifyItemChanged(selected_position);
            selected_position = position;
            notifyItemChanged(selected_position);
            lm.scrollToPosition(selected_position);
        }
    });

    Event event = eventsList.get(position);
    eventViewHolder.itemView.setTag(R.integer.EVENT_OBJECT_TAG, event);
....

1 个答案:

答案 0 :(得分:1)

试试这个。

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            return true;
        }

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

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    });

我在onInterceptTouchEvent返回true,在触摸项目回收者视图后无效。