当actionmode打开时,RecyclerView子项不会单击
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当actionmode打开时,RecyclerView子项不会单击相关的知识,希望对你有一定的参考价值。
我有一个苹果,梨和柠檬的项目。我使用ViewStub为苹果,梨和柠檬做了单独的布局。
我通过ItemView将模型直接连接到适配器。当我长按ItemView时,Activity actionMode正在打开,并且当actionMode关闭时单击没有问题。
我的问题是什么时候打开actionMode,ItemView项没有点击。请帮我。
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);
}
}
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);
}
答案
你试过这个qazxsw poi非常有用
以上是关于当actionmode打开时,RecyclerView子项不会单击的主要内容,如果未能解决你的问题,请参考以下文章
当组合框打开时,Silverlight Combobox 触发 KeyDown 事件
当 QFileDialog::getOpenFileName 的窗口打开时,程序意外完成