这是我的主要活动
private void setUpGUI() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
cardView = (CardView) findViewById(R.id.cardView);
recyclerView = (RecyclerView) findViewById(R.id.recylerView);
adapter = new ItemAdapter(new ItemAdapter.ItemClickListener() {
@Override
public void onItemClicked(TodoItem item) {
setUpSecondPopup(item);
Log.d(TAG, "on item click");
Uri uri = Uri.parse(TodoItemsContentProvider.CONTENT_URI + "/" + item.getId());
getContentResolver().update(uri, item.toContentValues(), null, null);
}
});
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAddItemPopup();
}
});
这是我的适配器类
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private static final String TAG = "item adapter";
private List<TodoItem> items = new ArrayList<>();
private ItemClickListener itemClickListener;
public ItemAdapter(ItemClickListener listener) {
itemClickListener = listener;
}
public void addItem(TodoItem item) {
if (!items.contains(item)) {
items.add(item);
Collections.sort(items);
notifyItemInserted(items.indexOf(item));
}
}
public void addItems(Collection<TodoItem> newItems) {
for (TodoItem item : newItems) {
if (!items.contains(item)) {
items.add(item);
}
}
Collections.sort(items);
notifyDataSetChanged();
}
public TodoItem getItem(int position) {
return items.get(position);
}
public void removeItem(int position) {
items.remove(position);
notifyItemRemoved(position);
}
// This listener listens for clicks to the viewholder. The viewholder passes back the position it is.
private final ClickListener clickListener = new ClickListener() {
@Override
public void onItemClicked(int position) {
Log.d(TAG, "on click adapter");
// Get the item that was clicked
TodoItem item = items.get(position);
// Set the completed state
item.setCompleted(!item.isCompleted());
//Update the UI
notifyItemChanged(position);
// Pass the item back the the itemclicklistener
itemClickListener.onItemClicked(item);
}
};
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_list_item, parent, false);
return new ViewHolder(v, clickListener);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
TodoItem item = items.get(position);
holder.textView.setText(item.getText());
holder.subTextViewA.setText(item.getSubText());
holder.checkBox.setChecked(item.isCompleted());
//holder.backgroundLayout.setBackgroundColor(Color.parseColor("#ec48e418"));
}
@Override
public int getItemCount() {
return items.size();
}
// The view holder listens to clicks on the
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// private RelativeLayout backgroundLayout;
final TextView textView, subTextViewA;
final CheckBox checkBox;
final ClickListener clickListener;
public ViewHolder(View itemView, ClickListener clickListener) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
subTextViewA = (TextView) itemView.findViewById(R.id.subTextViewA);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
//backgroundLayout = (RelativeLayout) itemView.findViewById(R.id.backgroundLayout);
itemView.setOnClickListener(this);
this.clickListener = clickListener;
}
@Override
public void onClick(View v) {
clickListener.onItemClicked(getAdapterPosition());
}
}
// Private listener fot the adapter to know about view clicks
private interface ClickListener {
void onItemClicked(int position);
}
// Public listener to pass the item back to the activity
public interface ItemClickListener {
void onItemClicked(TodoItem item);
}
}
答案 0 :(得分:0)
下面:
itemClickListener.onItemClicked(item);
未将点击监听器设置为RecyclerView
的任何视图,但使用onItemClicked
方法调用itemClickListener
方法,点击RecyclerView
时不会发生这种情况。
将onCreateViewHolder
方法中的Click Listener设置为:
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.todo_list_item, parent, false);
v.setOnClickListener(itemClickListener);
return new ViewHolder(v, clickListener);