1

リンゴ、ナシ、レモンの商品があります。ViewStub を使用して、リンゴ、ナシ、レモンのレイアウトを個別に作成しました。

ItemView を介してモデルをアダプターに直接接続します。ItemView を長押しするとアクティビティ actionMode が開き、actionMode が閉じているときにクリックしても問題ありません。

私の問題は、actionMode が開かれたとき、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);
     }
}

私のactionModeクリックリスナー:

   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 には 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);
        }
    }

レモンビュー:

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

1 に答える 1