打开 actionmode 时,RecyclerView 子项不会单击
RecyclerView children don't clicking when actionmode is opened
我有一件东西有苹果、梨和柠檬。我使用 ViewStub 为苹果、梨和柠檬制作了单独的布局。
我通过 ItemView 将模型直接连接到适配器。 Activity 长按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);
}
你试过这个吗Example非常有用
我有一件东西有苹果、梨和柠檬。我使用 ViewStub 为苹果、梨和柠檬制作了单独的布局。
我通过 ItemView 将模型直接连接到适配器。 Activity 长按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);
}
你试过这个吗Example非常有用