androidclicklong-clickviewstubactionmode

RecyclerView children don't clicking when actionmode is opened


I have an item which has apple, pear and lemon. I made separate layouts for apples, pears and lemons by using ViewStub.

I connect the model directly to the adapter via ItemView. Activity actionMode is opening when I doing long click on ItemView and there is no problem in clicking when actionMode is closed.

My problem is when actionMode is opened, ItemView items not clicking. Please help me.

ItemView:

public class ItemView extends LinearLayout implements BindingModel{

    private final int apple = 1, lemon = 2, pear = 3;

    private Stub<AppleView> appleViewStub;
    private Stub<LemonView> lemonViewStub;
    private Stub<PearView> pearViewStub;

    private Model model;

    private
    @NonNull
    LinkedHashSet<Model> batchSelected = new LinkedHashSet<>();

    public ItemView(Context context) {
        this(context, null);
    }

    public ItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.views, this);

        this.appleViewStub = new Stub<>((ViewStub) findViewById(R.id.apple_view_stub));
        this.lemonViewStub = new Stub<>((ViewStub) findViewById(R.id.lemon_view_stub));
        this.pearViewStub = new Stub<>((ViewStub) findViewById(R.id.pear_view_stub));

    }

    @Override
    public void bind(Model model) {

        this.model = model;
        int type = model.getType();

        bindElements(type);
        setClickable(type);

    }

    private void setClickable(int type) {

    if(type == apple){
    appleViewStub.get().setFocusable(batchSelectedIsEmpty());
    appleViewStub.get().setClickable(batchSelectedIsEmpty());
    appleViewStub.get().setEnabled(batchSelectedIsEmpty());
    appleViewStub.get().setLongClickable(batchSelectedIsEmpty());

    return;
    }

    if(type == lemon){
    lemonViewStub.get().setFocusable(batchSelectedIsEmpty());
    lemonViewStub.get().setClickable(batchSelectedIsEmpty());
    lemonViewStub.get().setEnabled(batchSelectedIsEmpty());
    lemonViewStub.get().setLongClickable(batchSelectedIsEmpty());

    return;
    }

    pearViewStub.get().setFocusable(batchSelectedIsEmpty());
    pearViewStub.get().setClickable(batchSelectedIsEmpty());
    pearViewStub.get().setEnabled(batchSelectedIsEmpty());
    pearViewStub.get().setLongClickable(batchSelectedIsEmpty());
    }

    public void bindElements(int type){

    if (type == lemon) {castLemon(); return;} 
    if (type == apple ) {castApple(); return;} 
    if (type == pear) {castPear(); return;} 

    }

    public void castLemon() {

     lemonViewStub.get().setVisibility(View.VISIBLE);
     appleViewStub.get().setVisibility(View.GONE);
     pearViewStub.get().setVisibility(View.GONE);

     }

    public void castApple() {

     lemonViewStub.get().setVisibility(View.GONE);
     appleViewStub.get().setVisibility(View.VISIBLE);
     pearViewStub.get().setVisibility(View.GONE);

     }

    public void castPear() {

     lemonViewStub.get().setVisibility(View.GONE);
     appleViewStub.get().setVisibility(View.GONE);
     pearViewStub.get().setVisibility(View.VISIBLE);
     }
}

My actionMode click listener:

   public class MyActivity extends Activity {

    private MyAdapter myAdapter;
    private ActionMode actionMode;
    private final MyAdapter.ItemClickListener selectionClickListener = new itemClickListener();

    private void initializeListAdapter() {

        myAdapter = new MyAdapter<>(selectionClickListener);
        recyclerView.setAdapter(myAdapter);
    }

    private class itemClickListener implements MyAdapter.ItemClickListener 
    {

            @Override
            public void onItemClick(Model item) {

                if (actionMode != null) {

                    myAdapter.toggleSelection(item);
                    myAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onItemLongClick(Model item) {

                if (actionMode == null) {

                    myAdapter.toggleSelection(item);
                    myAdapter.notifyDataSetChanged();

                    actionMode = startSupportActionMode(actionModeCallback);
                }
            }
        }

MyAdapter has clickListener:

    public class MyAdapter(){

    private final LinkedHashSet<Model> batchSelected = new LinkedHashSet<>();

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        Model model = getItem(position);
        viewHolder.getView().bind(model);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

              View itemView 
     = viewInflater.inflate(R.layout.model_item, parent, false);

                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (clickListener != null) {

                            Item item = ((ItemView) itemView).getItem();

                            if (item != null) {
                                clickListener.onItemClick((Model) itemView);
                                } 
                            }
                        }
                    }
                });

                itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {

                        if (clickListener != null && batchSelected.isEmpty()) {

                           Item item = ((ItemView) itemView).getItem();

                            if (item != null){
                             clickListener.onItemLongClick((Model) itemView); }
                                }
                            }

                        return true;
                    }
                });

                return new ViewHolder(itemView);

            }}

     public void toggleSelection(Model model) {

        if (!batchSelected.remove(model)) {
            batchSelected.add(model);
        }
    }

LemonView:

public class LemonView extends FrameLayout {

    private ImageView imageView;

    public LemonView(Context context) {
        this(context, null);
    }

    public LemonView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LemonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.lemon_view, this);

        imageView = findViewById(R.id.image);
    }

    @Override
    public void setFocusable(boolean focusable) {
        super.setFocusable(focusable);
        this.imageView.setFocusable(focusable);
    }

    @Override
    public void setClickable(boolean clickable) {
        super.setClickable(clickable);

        this.imageView.setClickable(clickable);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        this.imageView.setEnabled(enabled);
    }

Solution

  • Have you tried this Example Very usefull