我有一个RecyclerView,我只选择我在onLongPress时选择行,但我想要的是第一个onLongClick将启动突出显示,然后onClick on the row将选择并取消选择特定的行而不是onLongClick。取消选择最后选择的行后,onLongClick应再次启动突出显示。我尝试用布尔变量来做。但它不起作用。我感到很困惑。有什么想法吗?
这就是我一直以来的目标。
if(onLongPressReceived) {
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!onClickAnotherPressReceived) {
onClickPressReceived = true;
mItems.get(holder.getAdapterPosition()).setmIsChecked(true);
notifyDataSetChanged();
onClickAnotherPressReceived = true;
}
}
});
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(mItems.get(position).getmIsChecked());
holder.mDeleteRow.setVisibility(View.VISIBLE);
if(mItems.get(holder.getAdapterPosition()).getmIsChecked()){
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_selected));
}else {
//holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.checkboxHolder.setVisibility(View.INVISIBLE);
holder.mDeleteRow.setVisibility(View.INVISIBLE);
}
if(onClickPressReceived && !onLongAnotherPressReceived)
{
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(mItems.get(position).getmIsChecked());
holder.mDeleteRow.setVisibility(View.VISIBLE);
if(mItems.get(holder.getAdapterPosition()).getmIsChecked()){
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_selected));
}else {
//holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.checkboxHolder.setVisibility(View.INVISIBLE);
holder.mDeleteRow.setVisibility(View.INVISIBLE);
}
}
//Checking whether a particular view is clicked or not
if(onClickAnotherPressReceived && !onLongAnotherPressReceived)
{
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_unselected));
holder.mDeleteRow.setVisibility(View.INVISIBLE);
if (mItems.get(position).getmIsChecked()) {
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(true);
} else {
holder.checkboxHolder.setVisibility(View.GONE);
holder.mCheckBox.setChecked(false);
}
}
}
//Checking whether a particular view is clicked or not
else{
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_unselected));
holder.mDeleteRow.setVisibility(View.INVISIBLE);
if (mItems.get(position).getmIsChecked()) {
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(true);
} else {
holder.checkboxHolder.setVisibility(View.GONE);
holder.mCheckBox.setChecked(false);
}
}
//Calls the interface method in Activity to respond to CheckBox changes
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
updateMainClass.updateListBackground(holder.getAdapterPosition(), b);
}
});
/**
* <p>Responds to long press made on any row</p>
* <p>Checks the item on which long press is made
* sets the onLongPressReceived status to true and notify the adapter to refresh the list.</p>
*/
holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if(onLongAnotherPressReceived) {
onLongPressReceived = true;
mItems.get(holder.getAdapterPosition()).setmIsChecked(true);
notifyDataSetChanged();
onLongAnotherPressReceived = false;
}
return true;
}
});
//Calls the interface in Activity to remove the item from the List.
holder.mDeleteRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateMainClass.updateItemList(holder.getAdapterPosition());
}
});
}
答案 0 :(得分:0)
我通常这样做:
使用onBindViewHolder
课程的bindView
ViewHolder
方法:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
((ViewHolder) viewHolder).bindView(getItem(position));
}
然后在你的viewHolder类中保留实体的实例:
public class ViewHolder extends RecyclerView.ViewHolder {
YOUR_OBJECT entity;
public ViewHolder(View itemView) {
super(itemView);
//initialize view here
}
public void bindView(ChannelEntity entity) {
this.entity = entity; // use this entity for longClickListener
//setOnClickListener || OnLongClickListener Here
}
}