当我单击“回收者视图”项目时,我正在尝试更改其背景。数据实际上已更改,但UI并未更改。我已经尝试了很多尝试不同的方法,但是没有任何效果。虽然我正在调用notifyItemChanged(),但是它没有用。
任何使该功能正常工作的解决方案都将非常有帮助。
**我的代码:**
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
customNoteView = notesList.get(i);
notesText = viewHolder.notesWriteArea;
notesImage = viewHolder.notesImage;
notesAudio = viewHolder.notesAudio;
notesAudioPlayButton = viewHolder.notesAudioPlayButton;
notesAudioSeekBar = viewHolder.notesAudioSeekBar;
notesAudioDeleteButton = viewHolder.notesAudioDeleteButton;
if(customNoteView.getNoteText()!=null){
editTextList.add(notesText);
notesText.setVisibility(View.VISIBLE);
notesImage.setVisibility(View.GONE);
notesAudio.setVisibility(View.GONE);
notesText.requestFocus();
focussedEditText = notesText;
notesText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b){
focussedEditText = ((CustomEditText)view);
//Toast.makeText(context, ((CustomEditText) view).getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
else if(customNoteView.getImageUri()!=0){
notesImage.setVisibility(View.VISIBLE);
notesText.setVisibility(View.GONE);
notesAudio.setVisibility(View.GONE);
Glide.with(notesImage.getContext())
.load(notesImage.getContext().getDrawable(customNoteView.getImageUri()))
.into(notesImage);
notesImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, FullScreen.class);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context,
notesImage,
ViewCompat.getTransitionName(notesImage)
);
context.startActivity(intent, options.toBundle());
}
});
}
else if(customNoteView.getAudioUri()!=null){
notesAudio.setVisibility(View.VISIBLE);
notesImage.setVisibility(View.GONE);
notesText.setVisibility(View.GONE);
notesAudioPlayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notesAudioPlayButton.setBackground(context.getDrawable(R.drawable.ic_pause));
notesAudio.setBackground(context.getDrawable(R.drawable.gray_rectangular_background));
notifyItemChanged(viewHolder.getAdapterPosition());
Toast.makeText(context, ""+viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:1)
最后,我设法找到了自己的问题的答案,并且没有更改UI的根本原因。
错误:
我实际上是在onBindViewHolder(...)
方法中设置点击侦听器的。
解决方案:
如果您想在点击事件上更新回收站视图时避免使用notifyItemChanged(position)
和其他内容,则应在viewHolder类而不是onBindViewHolder
PS: 应用此更改对我来说效果很好。@ Blind Kai的解决方案也有效,但需要刷新视图。我很确定这是一个更简单,更有效的方法。不过,我感谢他的努力。
答案 1 :(得分:0)
如果您想要获得这种类型的行为,则可以尝试在类中添加字段,并在每次将onBindViewHolder
方法中的项目绑定时进行检查:
if (current.isClicked) {
viewHolder.itemView.setBackgroundColor(*needed_color*);
} else {
viewHolder.itemView.setBackgroundColor(*default_color*);
}
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
current.isClicked = !current.isClicked;
notifyItemChanged(viewHolder.getAdapterPosition());
}
});
current
-您列表中i
位置上的项目; itemView
-项目的根视图;
如果您需要更多详细信息,请随时提出评论。 (答案已被编辑并清理了几次)
答案 2 :(得分:0)
尝试以下代码:
1)MainActivity.class:
public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
List<String> data = new ArrayList<>();
List<Boolean> selection_states = new ArrayList<>();
for (int i = 0; i < 10; i++) {
data.add("Item " + (i + 1));
selection_states.add(false);
}
rv.setAdapter(new MyRecyclerViewAdapter(MainActivity.this, data, selection_states));
}
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private List<String> mData;
private List<Boolean> mSelectionStates;
private LayoutInflater mInflater;
MyRecyclerViewAdapter(Context context, List<String> data, List<Boolean> selection_states) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.mSelectionStates = selection_states;
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recycler_view_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.tv.setText(mData.get(position));
holder.ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int previous_selected = -1;
for (int i = 0; i < getSelectionStates().size(); i++) {
if (getSelectionStates().get(i)) {
previous_selected = i;
break;
}
}
if (previous_selected == -1) { // nothing is selected
setSelectionState(position, true);
notifyItemChanged(position);
} else if (previous_selected != position) { // selected (other than current selected)
setSelectionState(previous_selected, false);
setSelectionState(position, true);
notifyItemChanged(previous_selected);
notifyItemChanged(position);
}
}
});
if (mSelectionStates.get(position)) {
//Log.i(TAG, "Select " + position);
holder.ll.setBackgroundColor(ContextCompat.getColor(MainActivity.this, android.R.color.holo_red_dark));
} else {
//Log.i(TAG, "De-Select " + position);
holder.ll.setBackgroundColor(ContextCompat.getColor(MainActivity.this, android.R.color.white));
}
}
private List<Boolean> getSelectionStates() {
return mSelectionStates;
}
private void setSelectionState(int position, boolean state) {
mSelectionStates.set(position, state);
}
@Override
public int getItemCount() {
return mData.size();
}
private class ViewHolder extends RecyclerView.ViewHolder {
final LinearLayout ll;
final TextView tv;
ViewHolder(View itemView) {
super(itemView);
ll = (LinearLayout) itemView.findViewById(R.id.ll);
tv = (TextView) itemView.findViewById(R.id.tv);
}
}
}
}
2)activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
3)recycler_view_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="20dp"
android:id="@+id/ll"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:gravity="center"
android:textSize="20sp"/>
</LinearLayout>
4)结果: