我正在回收。
这是我的适配器类:
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.ViewHolder> {
private static List<Notes> mNotes;
private Context mContext;
public NotesAdapter(Context context, List<Notes> notes) {
mNotes = notes;
mContext = context;
}
// Usually involves inflating a layout from XML and returning the holder
@Override
public NotesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View notesView = inflater.inflate(R.layout.items_notes, parent, false);
// Return a new holder instance
final ViewHolder viewHolder = new ViewHolder(notesView);
notesView.setLongClickable(true);
notesView.setClickable(true);
return viewHolder;
}
// Easy access to the context object in the recyclerview
private Context getContext() {
return mContext;
}
@Override
public void onBindViewHolder(NotesAdapter.ViewHolder viewHolder, final int position) {
// Get the data model based on position
Notes notes = mNotes.get(position);
viewHolder.itemView.setSelected(mNotes.contains(position));
// Set item views based on your views and data model
TextView textView = viewHolder.preTitle;
textView.setText(notes.getTitle());
TextView textView1 = viewHolder.preText;
textView1.setText(notes.getText());
String color=notes.getColor();
CardView preCard=viewHolder.preCard;
preCard.setBackgroundColor(Color.parseColor(color));
ImageView img = viewHolder.preImage;
img.setVisibility(View.GONE);
}
// Returns the total count of items in the list
@Override
public int getItemCount() {
return mNotes.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public RobotoTextView preTitle, preText;
public ImageView preImage;
public CardView preCard;
public MenuItem delete;
public ViewHolder(final View itemView) {
super(itemView);
itemView.setClickable(true);
preTitle = (RobotoTextView) itemView.findViewById(R.id.preTitle);
preText = (RobotoTextView) itemView.findViewById(R.id.preText);
preImage=(ImageView) itemView.findViewById(R.id.preImage);
preCard=(CardView) itemView.findViewById(R.id.preCard);
delete=(MenuItem) itemView.findViewById(R.id.delete);
itemView.setOnLongClickListener(new View.OnLongClickListener() {
int selected=0;
@Override
public boolean onLongClick(View view) {
int p=getLayoutPosition();
System.out.println("LongClick: "+p);
return true;
}
});
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int p=getLayoutPosition();
Notes notes = mNotes.get(p);
Intent intent = new Intent(view.getContext(),EditNote.class);
Bundle bundle = new Bundle();
bundle.putSerializable("DATA",notes);
intent.putExtras(bundle);
itemView.getContext().startActivity(intent);
// Toast.makeText(getContext(), "Recycle Click" + mNotes.get(viewHolder.getPosition())+" ", Toast.LENGTH_SHORT).show();
System.out.println("Click: "+p);
}
});
}
}
}
在这里,点击监听器工作正常。
然后长按我能够获得点击和长按的项目位置。
但现在我想从此类访问菜单项。
在长按侦听器方法中。我想编写代码,以访问菜单项,该项目的ID为R.id.delete。
长按我想将该项设置为可见。
但。
delete.setVisible(true);
给我空对象引用错误。
任何想法?或建议?
最佳答案
在您的Activity类中编写以下代码,
private Menu mMenu = null;
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
mMenu = menu;
return super.onPrepareOptionsMenu(menu);
}
public Menu getMenu()
{
return mMenu;
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mThis = this;
}
private static MainActivity mThis = null;
public MainActivity getThis()
{
return mThis;
}
在Adapter LongClick方法中,调用getMenu()方法。像下面。
@Override
public boolean onLongClick(View view) {
Menu menu = MainActivity.getThis().getMenu();
menu.findItem(R.d.Delete).setVisible(true);
return true;
}
关于android - 从ViewHolder访问菜单项-android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40303856/