【发布时间】:2020-12-15 13:33:48
【问题描述】:
当用户点击另一个项目时,我想在我的 RecyclerView 中更新之前的 TextView 项目:
例如,如果用户点击“lemanju”TextView,它会将字体颜色变为橙色,当用户点击“Lightning”TextView时,lemanju的字体颜色应该变回黑色,而Lighning的字体颜色必须变为橙色,如下所示。
Lightning being selected (How I expect it to work)
Lightning being selected (What is happening now -> not updating lemanju's font color back to black)
我在我的适配器类中尝试了以下代码来实现这个结果:
// set previous item TextColor to black.
holder.itemView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.BaseColor_1));
holder.adapter.notifyItemChanged(previousSamplePosition);
// set actual item TextColor to orange.
holder.itemView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.PrimaryColor_1));
不幸的是,当我点击 Lightning 时,我无法将 lemanju 的字体颜色改回黑色...
有谁知道如何使用 notifyItemChanged 更改特定的 TextView?或者有其他方法可以实现吗?
我的适配器类中的 onCreateViewHolder 方法,您可以在下面看到:
/**
* Sets the contents of an item at a given position in the RecyclerView.
* Called by RecyclerView to display the data at a specificed position.
*
* @param holder The view holder for that position in the RecyclerView.
* @param samplePosition The position of the item in the RecyclerView.
* <p>
*/
@Override
public void onBindViewHolder(@NonNull SampleViewHolder holder, int samplePosition) {
if (sampleList != null) {
Sample currentSample = sampleListFiltered.getCurrentList().get(samplePosition);
// Add the data to the view holder.
holder.sampleView.setText(currentSample.getName());
holder.bind(currentSample);
if (sampleNameSearched != null) {
String sampleName = Normalizer.normalize(sampleListFiltered.getCurrentList().get(samplePosition).getName(), Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "").toLowerCase();
Pattern sampleQuery = Pattern.compile(sampleNameSearched);
Matcher sampleMatcher = sampleQuery.matcher(sampleName);
SpannableStringBuilder spanString = new SpannableStringBuilder(sampleListFiltered.getCurrentList().get(samplePosition).getName());
while (sampleMatcher.find()) {
spanString.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), sampleMatcher.start(), sampleMatcher.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
holder.sampleView.setText(spanString);
} else {
// This solved the problem
holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.BaseColor_1));
}
}
Bundle sampleBundle = new Bundle();
holder.sampleView.setOnClickListener(view -> {
sampleBundle.putLong("Sample", sampleListFiltered.getCurrentList().get(samplePosition).getId());
SampleCollectorListNavigatorFragment.addScreenToStack(0);
SampleCollectorListNavigatorFragment.setLastSurveyIdAccessed(sampleListFiltered.getCurrentList().get(samplePosition).getSurveyId());
Navigation.findNavController(view).navigate(R.id.action_sampleCollectorListNavigator_to_sampleCollectorInspectSampleFragment, sampleBundle);
});
holder.sampleView.setOnLongClickListener(view -> {
sampleBundle.putLong("Sample", sampleListFiltered.getCurrentList().get(samplePosition).getId());
if (sampleID == 0) {
// Access the view of the previous screen
((Activity) inflater.getContext()).findViewById(R.id.ButtonPanel).setVisibility(View.VISIBLE);
holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.PrimaryColor_1));
} else if (sampleID == sampleListFiltered.getCurrentList().get(samplePosition).getId()) {
if (((Activity) inflater.getContext()).findViewById(R.id.ButtonPanel).getVisibility() == View.VISIBLE) {
((Activity) inflater.getContext()).findViewById(R.id.ButtonPanel).setVisibility(View.GONE);
holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.BaseColor_1));
} else {
((Activity) inflater.getContext()).findViewById(R.id.ButtonPanel).setVisibility(View.VISIBLE);
holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.PrimaryColor_1));
}
} else if (sampleID != sampleListFiltered.getCurrentList().get(samplePosition).getId()) {
holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.BaseColor_1));
notifyItemChanged(previousSamplePosition);
holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.PrimaryColor_1));
((Activity) inflater.getContext()).findViewById(R.id.ButtonPanel).setVisibility(View.VISIBLE);
}
sampleID = sampleListFiltered.getCurrentList().get(samplePosition).getId();
previousSamplePosition = samplePosition;
return true;
});
}
}
更新 -> 如何解决这个问题?
为了解决我的代码中的问题,我必须在我的搜索过滤器中将字符串更新回黑色:
// This solved the problem
holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.BaseColor_1));
但是如果你没有和我一样的结构,或者你只是想更新回收器中之前的 TextView 项,你可以使用这部分代码:
holder.sampleView.setTextColor(ContextCompat.getColor(inflater.getContext(), R.color.BaseColor_1));
notifyItemChanged(previousSamplePosition);
notifyItemChanged(previousSamplePosition) 可以解决问题。
【问题讨论】:
标签: android android-layout android-recyclerview