当我登录时更新。i(“adapterPos”,String.valueOf(adapter.adapterpo))
在onsweep中,它甚至在将其设置为-1之前返回-1。。。
我有一个
回收器视图
,以及一个自定义适配器
来显示有关练习的信息。
当用户点击recyclerView项目时,项目背景设置为绿色,“保存”按钮变为“更新”按钮,以便用户可以更新其现有录制的练习。
我还有一个 onSwiped 方法,它在我的片段中实现。当用户轻扫项目时,该项目将从数据库中删除。
当用户滑动删除一个项目时,我想将所有项目的背景设置回默认值(灰色)。
这怎么可能呢?
注意:如果选择了项目2并且用户决定滑动项目1,即使在项目1被删除后,项目2仍然是当前选择的。
记录练习片段(相关代码)
@Override
public void onExerciseClicked(int position) {
if (recyclerItemClicked == false) {
saveBtn.setText("Update");
clearBtn.setVisibility(View.GONE);
recyclerItemClicked = true;
double selectedWeight = adapter.getWeight(position);
String selectedWeightString = Double.toString(selectedWeight);
editTextWeight.setText(selectedWeightString);
int selectedReps = adapter.getReps(position);
String selectedRepsString = Integer.toString(selectedReps);
editTextReps.setText(selectedRepsString);
} else {
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");
recyclerItemClicked = false;
}
}
public void initRecyclerView() {
adapter = new CompletedExercisesListAdapter2(allExercises, this);
recyclerView.setAdapter(adapter);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
}
public void setExercises(List<Log_Entries> exercises) {
allExercises.clear();
allExercises.addAll(exercises);
}
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int logID = allExercises.get(viewHolder.getAdapterPosition()).getLog_id();
logViewModel.deleteByID(logID);
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");
Log.i("adapterPos", String.valueOf(adapter.adapterPos)); // this is always -1 for some reason
// backup the currently selected position
int selectedPos = adapter.adapterPos;
// change the currently selected position to -1
adapter.adapterPos = -1;
adapter.notifyItemChanged(selectedPos);
}
};
适配器
public class CompletedExercisesListAdapter2 extends RecyclerView.Adapter {
private OnExerciseClickListener onExerciseClickListener;
private List<Log_Entries> allCompletedExercises = new ArrayList<>();
public int adapterPos = -1;
public boolean isSelected = false;
public CompletedExercisesListAdapter2(ArrayList<Log_Entries> allCompletedExercises, OnExerciseClickListener onExerciseClickListener) {
this.allCompletedExercises = allCompletedExercises;
this.onExerciseClickListener = onExerciseClickListener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view;
if (viewType == 0) {
view = layoutInflater.inflate(R.layout.new_completed_exercise_item, parent, false);
return new ViewHolderOne(view, onExerciseClickListener);
}
view = layoutInflater.inflate(R.layout.completed_exercise_item, parent, false);
return new ViewHolderTwo(view, onExerciseClickListener);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == 0) {
ViewHolderOne viewHolderOne = (ViewHolderOne) holder;
if (adapterPos == position) {
viewHolderOne.relativeLayout.setBackgroundColor(Color.parseColor("#567845"));
}
else {
viewHolderOne.relativeLayout.setBackgroundResource(R.color.dark_grey);
}
viewHolderOne.textViewExerciseName.setText(String.valueOf(allCompletedExercises.get(position).getChildExerciseName()));
viewHolderOne.textViewSetNumber.setText(String.valueOf(viewHolderOne.getAdapterPosition() + 1));
viewHolderOne.textViewWeight.setText(String.valueOf(allCompletedExercises.get(position).getTotal_weight_lifted()));
viewHolderOne.textViewReps.setText(String.valueOf(allCompletedExercises.get(position).getReps()));
} else if (getItemViewType(position) == 1) {
ViewHolderTwo viewHolderTwo = (ViewHolderTwo) holder;
if (adapterPos == position) {
viewHolderTwo.relativeLayout.setBackgroundColor(Color.parseColor("#567845"));
} else {
viewHolderTwo.relativeLayout.setBackgroundResource(R.color.dark_grey);
}
viewHolderTwo.textViewSetNumber.setText(String.valueOf(viewHolderTwo.getAdapterPosition() + 1));
viewHolderTwo.textViewWeight.setText(String.valueOf(allCompletedExercises.get(position).getTotal_weight_lifted()));
viewHolderTwo.textViewReps.setText(String.valueOf(allCompletedExercises.get(position).getReps()));
}
}
@Override
public int getItemCount() {
return allCompletedExercises.size();
}
@Override
public int getItemViewType(int position) {
// if list is sorted chronologically
if (position == 0) {
return 0;
}
if (allCompletedExercises.get(position).getChildExerciseName().equals(allCompletedExercises.get(position - 1).getChildExerciseName())) {
return 1;
} else {
return 0;
}
}
public class ViewHolderOne extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textViewExerciseName;
private TextView textViewSetNumber;
private TextView textViewWeight;
private TextView textViewReps;
OnExerciseClickListener mOnExerciseClickListener;
private RelativeLayout relativeLayout;
public ViewHolderOne(@NonNull View itemView, OnExerciseClickListener onExerciseClickListener) {
super(itemView);
textViewExerciseName = itemView.findViewById(R.id.textView_ExerciseName3);
textViewSetNumber = itemView.findViewById(R.id.textView_Set_Number56);
textViewWeight = itemView.findViewById(R.id.textView_weight78);
textViewReps = itemView.findViewById(R.id.textView_repss0);
mOnExerciseClickListener = onExerciseClickListener;
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.exercise_item_relative);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
onExerciseClickListener.onExerciseClicked(getAdapterPosition());
if (isSelected) {
adapterPos = -1;
isSelected = false;
} else {
adapterPos = getAdapterPosition();
isSelected = true;
}
notifyDataSetChanged();
}
}
class ViewHolderTwo extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textViewSetNumber;
private TextView textViewWeight;
private TextView textViewReps;
OnExerciseClickListener mOnExerciseClickListener;
private RelativeLayout relativeLayout;
public ViewHolderTwo(@NonNull View itemView, OnExerciseClickListener onExerciseClickListener) {
super(itemView);
textViewSetNumber = itemView.findViewById(R.id.textView_Set_Number);
textViewWeight = itemView.findViewById(R.id.textView_weight);
textViewReps = itemView.findViewById(R.id.textView_repss);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.exercise_item_rel);
mOnExerciseClickListener = onExerciseClickListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
onExerciseClickListener.onExerciseClicked(getAdapterPosition());
if (!isSelected) {
adapterPos = getAdapterPosition();
isSelected = true;
} else {
adapterPos = -1;
isSelected = false;
}
notifyDataSetChanged();
}
}
public interface OnExerciseClickListener {
void onExerciseClicked(int position);
}
public double getWeight(int position) {
double weight = allCompletedExercises.get(position).getTotal_weight_lifted();
return weight;
}
public int getReps(int position) {
int reps = allCompletedExercises.get(position).getReps();
return reps;
}
}
我认为您唯一需要做的就是确保在设置adapatorpos= -1
后执行一次 notifyDataSetChanged()。
notifyDataSetChanged()
将针对整个适配器重新执行 onBindViewHolder,而不是 notifyItemChanged(
),这将导致所有项目都计算为灰色背景。
您实际上已经在视图持有者 onClick 方法中执行此操作,以防止多个项目同时具有绿色背景。因此,在滑动后再次执行该方法。因此,只需执行以下操作:
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int logID = allExercises.get(viewHolder.getAdapterPosition()).getLog_id();
logViewModel.deleteByID(logID);
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");//<-- why if no item is selected?
// backup the currently selected position
//int selectedPos = adapter.adapterPos;<-- why would you do this, nothing is not shown as selected
// change the currently selected position to -1
adapter.adapterPos = -1;
adapter.notifyDataSetChanged();//now everything will be gray
}
这肯定会起作用,但它会干扰您在滑动后可能使用的删除动画。根据前面的回答,另一个可能有效的代码如下:
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int copyOfAdapterPos=adapterPos;
adapterPos=-1;
notifyItemChanged(copyOfAdapterPos);
int logID = allExercises.get(viewHolder.getAdapterPosition()).getLog_id();
logViewModel.deleteByID(logID);
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");//<-- why if no item is selected?
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());//now everything will be gray
}
我没有测试它,但如果它的工作原理,它会看起来更好
在 onSwipe() 中
,只需将适配器
对象的 adapterPos
重置为 -1,然后在该位置调用 notifyItemChanged()。
// backup the currently selected position
int selectedPos = adapter.adapterPos;
// change the currently selected position to -1
adapter.adapterPos = -1;
adapter.isSelected = false;
adapter.notifyItemChanged(selectedPos);
注意:在完全滑动ViewHolder
之后调用onSware()
。
我有一个清单,我在回收商的视图中展示了这一点 一些项目有蓝色背景和其他项目有灰色背景 我想编辑所选项目背景(所选项目表示已单击的项目) 这是我的适配器类
所以在我的项目中,我只是想让背景文件有一个gif播放,让它看起来更好,但是我找到的每个解决方案都不起作用。我在JLabel上制作gif,然后将所有内容都添加到JLabel上的解决方案不起作用,项目运行了,但是屏幕上没有显示任何内容。我把gif叫做 我的主长这样 我在乞讨中扩展了JPanel。我添加了一堆盒子,这些盒子有一堆JButton 这有可能吗?
维护字体和颜色样式的最佳实践是什么。我制作了一个colors.xml文件,用于更改按钮等单独元素的颜色,但我不确定Android希望开发人员如何组织他们的样式。 例如,我希望所有屏幕都有相同的背景色。我该怎么做?我是否需要为每个活动布局xml指定一些内容?还是其他地方?我该如何完成它?
如何将背景颜色设置为透明
我试图将图像作为JavaFX场景中的背景,但我的代码不起作用。 我试图在java eclipse中制作一个战舰游戏程序,但我遇到了一个图形问题。 当我第一次尝试运行它时,它工作了,一个新的窗口打开了,中间有一个按钮,但bakcground是空白的。当我尝试在窗口中设置一个图像作为背景时,按下“开始”按钮,什么也没有发生…